Esempio n. 1
0
        /// <inheritdoc cref="IOwinRequestMapper.MapAsync"/>
        public async Task <RequestMessage> MapAsync(IRequest request, IWireMockMiddlewareOptions options)
        {
            (UrlDetails urldetails, string clientIP) = ParseRequest(request);

            string method = request.Method;

            Dictionary <string, string[]> headers = null;
            IEnumerable <string>          contentEncodingHeader = null;

            if (request.Headers.Any())
            {
                headers = new Dictionary <string, string[]>();
                foreach (var header in request.Headers)
                {
                    headers.Add(header.Key, header.Value);

                    if (string.Equals(header.Key, HttpKnownHeaderNames.ContentEncoding, StringComparison.OrdinalIgnoreCase))
                    {
                        contentEncodingHeader = header.Value;
                    }
                }
            }

            IDictionary <string, string> cookies = null;

            if (request.Cookies.Any())
            {
                cookies = new Dictionary <string, string>();
                foreach (var cookie in request.Cookies)
                {
                    cookies.Add(cookie.Key, cookie.Value);
                }
            }

            IBodyData body = null;

            if (request.Body != null && BodyParser.ShouldParseBody(method, options.AllowBodyForAllHttpMethods == true))
            {
                var bodyParserSettings = new BodyParserSettings
                {
                    Stream                   = request.Body,
                    ContentType              = request.ContentType,
                    DeserializeJson          = !options.DisableJsonBodyParsing.GetValueOrDefault(false),
                    ContentEncoding          = contentEncodingHeader?.FirstOrDefault(),
                    DecompressGZipAndDeflate = !options.DisableRequestBodyDecompressing.GetValueOrDefault(false)
                };

                body = await BodyParser.Parse(bodyParserSettings);
            }

            return(new RequestMessage(urldetails, method, clientIP, body, headers, cookies)
            {
                DateTime = DateTime.UtcNow
            });
        }
Esempio n. 2
0
        public (IBodyData BodyData, IDictionary <string, WireMockList <string> > Headers) Transform(RequestMessage originalRequestMessage, ResponseMessage originalResponseMessage, IBodyData bodyData, IDictionary <string, WireMockList <string> > headers)
        {
            var transformerContext = _factory.Create();

            var model = new
            {
                request  = originalRequestMessage,
                response = originalResponseMessage
            };

            IBodyData newBodyData = null;

            if (bodyData?.DetectedBodyType != null)
            {
                newBodyData = TransformBodyData(transformerContext, model, bodyData, false);
            }

            return(newBodyData, TransformHeaders(transformerContext, model, headers));
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RequestMessage"/> class.
        /// </summary>
        /// <param name="urlDetails">The original url details.</param>
        /// <param name="method">The HTTP method.</param>
        /// <param name="clientIP">The client IP Address.</param>
        /// <param name="bodyData">The BodyData.</param>
        /// <param name="headers">The headers.</param>
        /// <param name="cookies">The cookies.</param>
        public RequestMessage([NotNull] UrlDetails urlDetails, [NotNull] string method, [NotNull] string clientIP, [CanBeNull] IBodyData bodyData = null, [CanBeNull] IDictionary <string, string[]> headers = null, [CanBeNull] IDictionary <string, string> cookies = null)
        {
            Check.NotNull(urlDetails, nameof(urlDetails));
            Check.NotNull(method, nameof(method));
            Check.NotNull(clientIP, nameof(clientIP));

            AbsoluteUrl = urlDetails.AbsoluteUrl.ToString();
            Url         = urlDetails.Url.ToString();
            Protocol    = urlDetails.Url.Scheme;
            Host        = urlDetails.Url.Host;
            Port        = urlDetails.Url.Port;
            Origin      = $"{Protocol}://{Host}:{Port}";

            AbsolutePath         = WebUtility.UrlDecode(urlDetails.AbsoluteUrl.AbsolutePath);
            Path                 = WebUtility.UrlDecode(urlDetails.Url.AbsolutePath);
            PathSegments         = Path.Split('/').Skip(1).ToArray();
            AbsolutePathSegments = AbsolutePath.Split('/').Skip(1).ToArray();

            Method   = method;
            ClientIP = clientIP;

            BodyData = bodyData;

            // Convenience getters for e.g. Handlebars
            Body             = BodyData?.BodyAsString;
            BodyAsJson       = BodyData?.BodyAsJson;
            BodyAsBytes      = BodyData?.BodyAsBytes;
            DetectedBodyType = BodyData?.DetectedBodyType.ToString();
            DetectedBodyTypeFromContentType = BodyData?.DetectedBodyTypeFromContentType.ToString();
            DetectedCompression             = BodyData?.DetectedCompression;

            Headers  = headers?.ToDictionary(header => header.Key, header => new WireMockList <string>(header.Value));
            Cookies  = cookies;
            RawQuery = urlDetails.Url.Query;
            Query    = QueryStringParser.Parse(RawQuery);
        }
Esempio n. 4
0
        private static IBodyData TransformBodyData(ITransformerContext transformerContext, object model, IBodyData original, bool useTransformerForBodyAsFile)
        {
            switch (original?.DetectedBodyType)
            {
            case BodyType.Json:
                return(TransformBodyAsJson(transformerContext, model, original));

            case BodyType.File:
                return(TransformBodyAsFile(transformerContext, model, original, useTransformerForBodyAsFile));

            case BodyType.String:
                return(TransformBodyAsString(transformerContext, model, original));

            default:
                return(null);
            }
        }
Esempio n. 5
0
        private static IBodyData TransformBodyAsFile(ITransformerContext handlebarsContext, object model, IBodyData original, bool useTransformerForBodyAsFile)
        {
            string transformedBodyAsFilename = handlebarsContext.ParseAndRender(original.BodyAsFile, model);

            if (!useTransformerForBodyAsFile)
            {
                return(new BodyData
                {
                    DetectedBodyType = original.DetectedBodyType,
                    DetectedBodyTypeFromContentType = original.DetectedBodyTypeFromContentType,
                    BodyAsFile = transformedBodyAsFilename
                });
            }
            else
            {
                string text = handlebarsContext.FileSystemHandler.ReadResponseBodyAsString(transformedBodyAsFilename);

                return(new BodyData
                {
                    DetectedBodyType = BodyType.String,
                    DetectedBodyTypeFromContentType = original.DetectedBodyTypeFromContentType,
                    BodyAsString = handlebarsContext.ParseAndRender(text, model),
                    BodyAsFile = transformedBodyAsFilename
                });
            }
        }
Esempio n. 6
0
 private static IBodyData TransformBodyAsString(ITransformerContext handlebarsContext, object model, IBodyData original)
 {
     return(new BodyData
     {
         Encoding = original.Encoding,
         DetectedBodyType = original.DetectedBodyType,
         DetectedBodyTypeFromContentType = original.DetectedBodyTypeFromContentType,
         BodyAsString = handlebarsContext.ParseAndRender(original.BodyAsString, model)
     });
 }
Esempio n. 7
0
        private static IBodyData TransformBodyAsJson(ITransformerContext handlebarsContext, object model, IBodyData original)
        {
            JToken jToken;

            switch (original.BodyAsJson)
            {
            case JObject bodyAsJObject:
                jToken = bodyAsJObject.DeepClone();
                WalkNode(handlebarsContext, jToken, model);
                break;

            case Array bodyAsArray:
                jToken = JArray.FromObject(bodyAsArray);
                WalkNode(handlebarsContext, jToken, model);
                break;

            case string bodyAsString:
                jToken = ReplaceSingleNode(handlebarsContext, bodyAsString, model);
                break;

            default:
                jToken = JObject.FromObject(original.BodyAsJson);
                WalkNode(handlebarsContext, jToken, model);
                break;
            }

            return(new BodyData
            {
                Encoding = original.Encoding,
                DetectedBodyType = original.DetectedBodyType,
                DetectedBodyTypeFromContentType = original.DetectedBodyTypeFromContentType,
                BodyAsJson = jToken
            });
        }