public void Convert_WithHeader_HeaderIsAddedToHttpRequestMessage()
        {
            var request = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "X-Custom", "Tester" }
                }
            };

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            _mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(new HttpBodyContent(content: Encoding.UTF8.GetBytes(String.Empty), contentType: new MediaTypeHeaderValue("text/plain")
            {
                CharSet = "utf-8"
            }));

            var result = mapper.Convert(request);

            Assert.Equal(request.Headers.First().Key, result.Headers.First().Key);
            Assert.Equal(request.Headers.First().Value, result.Headers.First().Value.First());
        }
        public ProviderServiceRequest Convert(Request from)
        {
            if (from == null)
            {
                return(null);
            }

            var httpVerb = _httpVerbMapper.Convert(from.Method.ToUpper());

            var to = new ProviderServiceRequest
            {
                Method = httpVerb,
                Path   = from.Path,
                Query  = !String.IsNullOrEmpty(from.Url.Query) ? from.Url.Query.TrimStart('?') : null
            };

            if (from.Headers != null && from.Headers.Any())
            {
                var fromHeaders = from.Headers.ToDictionary(x => x.Key, x => String.Join(", ", x.Value));
                to.Headers = fromHeaders;
            }

            if (from.Body != null && from.Body.Length > 0)
            {
                var content         = ConvertStreamToString(from.Body);
                var httpBodyContent = _httpBodyContentMapper.Convert(content, to.Headers);

                to.Body = httpBodyContent.Body;
            }

            return(to);
        }
        public ProviderServiceResponse Convert(HttpResponseMessage from)
        {
            if (from == null)
            {
                return(null);
            }

            var to = new ProviderServiceResponse
            {
                Status  = (int)from.StatusCode,
                Headers = ConvertHeaders(from.Headers, from.Content)
            };

            if (from.Content != null)
            {
                var responseContent = from.Content.ReadAsByteArrayAsync().RunSync();
                if (responseContent != null)
                {
                    var httpBodyContent = _httpBodyContentMapper.Convert(content: responseContent, headers: to.Headers);

                    to.Body = httpBodyContent.Body;
                }
            }

            return(to);
        }
        public Response Convert(ProviderServiceResponse from)
        {
            if (from == null)
            {
                return(null);
            }

            var to = new Response
            {
                StatusCode = (HttpStatusCode)from.Status,
                Headers    = from.Headers ?? new Dictionary <string, string>()
            };

            if (from.Body != null)
            {
                HttpBodyContent bodyContent = _httpBodyContentMapper.Convert(body: from.Body, headers: from.Headers);
                to.ContentType = bodyContent.ContentType;

                to.Contents = s =>
                {
                    byte[] bytes = bodyContent.ContentBytes;
                    s.Write(bytes, 0, bytes.Length);
                    s.Flush();
                };
            }

            return(to);
        }
        public void Convert1_WithBodyAndNullHeaders_ReturnsBodyWithUtf8EncodingAndPlainTextContentType()
        {
            const string           body   = "This is my content";
            IHttpBodyContentMapper mapper = GetSubject();

            var result = mapper.Convert(body: body, headers: null);

            Assert.Equal(body, result.Content);
            Assert.Equal(Encoding.UTF8, result.Encoding);
            Assert.Equal("text/plain", result.ContentType.MediaType);
        }
        public HttpRequestMessage Convert(ProviderServiceRequest from)
        {
            if (from == null)
            {
                return(null);
            }

            var requestHttpMethod = _httpMethodMapper.Convert(from.Method);
            var requestPath       = from.PathWithQuery();

            var to = new HttpRequestMessage(requestHttpMethod, requestPath);

            var contentRelatedHeaders = new Dictionary <string, string>();

            if (from.Headers != null && from.Headers.Any())
            {
                foreach (var requestHeader in from.Headers)
                {
                    //Strip any Content- headers as they need to be attached to Request content when using a HttpRequestMessage
                    if (requestHeader.Key.IndexOf("Content-", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        contentRelatedHeaders.Add(requestHeader.Key, requestHeader.Value);
                        continue;
                    }

                    to.Headers.Add(requestHeader.Key, requestHeader.Value);
                }
            }

            if (from.Body != null)
            {
                HttpBodyContent bodyContent = _httpBodyContentMapper.Convert(body: from.Body, headers: from.Headers);
                var             httpContent = _httpContentMapper.Convert(bodyContent);

                //Set the content related headers
                if (httpContent != null && contentRelatedHeaders.Any())
                {
                    foreach (var contentHeader in contentRelatedHeaders)
                    {
                        if (contentHeader.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase) &&
                            httpContent.Headers.Any(x => x.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase)))
                        {
                            continue;
                        }

                        httpContent.Headers.Add(contentHeader.Key, contentHeader.Value);
                    }
                }

                to.Content = httpContent;
            }

            return(to);
        }
Esempio n. 7
0
        public void Convert_WithHeader_HeaderIsAddedToHttpRequestMessage()
        {
            var request = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "X-Custom", "Tester" }
                }
            };

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            _mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(new HttpBodyContent(String.Empty, null, null));

            var result = mapper.Convert(request);

            Assert.Equal(request.Headers.First().Key, result.Headers.First().Key);
            Assert.Equal(request.Headers.First().Value, result.Headers.First().Value.First());
        }
        public void Convert1_WithJsonBodyAndDifferentCasings_ReturnsUnchangedCasingJsonBodyWithUtf8EncodingAndApplicationJsonContentType()
        {
            var body = new
            {
                Test  = "testeR",
                tesT2 = 1
            };
            var headers = new Dictionary <string, string>
            {
                { "content-type", "Application/Json" }
            };
            const string           jsonBody = "{\"Test\":\"testeR\",\"tesT2\":1}";
            IHttpBodyContentMapper mapper   = GetSubject();

            var result = mapper.Convert(body: body, headers: headers);

            Assert.Equal(jsonBody, result.Content);
            Assert.Equal(Encoding.UTF8, result.Encoding);
            Assert.Equal("Application/Json", result.ContentType.MediaType);
        }
        public HttpRequestMessage Convert(ProviderServiceRequest from)
        {
            if (from == null)
            {
                return(null);
            }

            var requestHttpMethod = _httpMethodMapper.Convert(from.Method);
            var requestPath       = from.PathWithQuery();

            var to = new HttpRequestMessage(requestHttpMethod, requestPath);

            if (from.Headers != null && from.Headers.Any())
            {
                foreach (var requestHeader in from.Headers)
                {
                    //TODO: Check if there are any other headers which need special treatment
                    //Handle the content-type header as little differently, as they need to be attached to the content when using a HttpRequestMessage
                    //Strip the Content-Length header as is automatically attached to the request
                    if (requestHeader.Key.Equals("Content-Type", StringComparison.InvariantCultureIgnoreCase) ||
                        requestHeader.Key.Equals("Content-Length", StringComparison.InvariantCultureIgnoreCase))
                    {
                        continue;
                    }

                    to.Headers.Add(requestHeader.Key, requestHeader.Value);
                }
            }

            if (from.Body != null)
            {
                HttpBodyContent bodyContent = _httpBodyContentMapper.Convert(from.Body, from.Headers);
                to.Content = _httpContentMapper.Convert(bodyContent);
            }

            return(to);
        }