Beispiel #1
0
        public void Convert_WithJsonBody_CallsHttpBodyContentMapperAndCorrectlySetsBody()
        {
            var headers = new Dictionary <string, IEnumerable <string> >
            {
                { "Content-Type", new List <string> {
                      "application/json", "charset=utf-8"
                  } }
            };
            var body = new
            {
                Test  = "tester",
                test2 = 1
            };
            const string content         = "{\"Test\":\"tester\",\"test2\":1}";
            var          contentBytes    = Encoding.UTF8.GetBytes(content);
            var          request         = GetPreCannedRequest(headers: headers, content: content);
            var          httpBodyContent = new HttpBodyContent(content: contentBytes, contentType: "application/json", encoding: Encoding.UTF8);

            var mockHttpVerbMapper        = Substitute.For <IHttpVerbMapper>();
            var mockHttpBodyContentMapper = Substitute.For <IHttpBodyContentMapper>();

            mockHttpVerbMapper.Convert("GET").Returns(HttpVerb.Get);
            mockHttpBodyContentMapper.Convert(content: Arg.Any <byte[]>(), headers: Arg.Any <IDictionary <string, string> >()).Returns(httpBodyContent);

            var mapper = new ProviderServiceRequestMapper(mockHttpVerbMapper, mockHttpBodyContentMapper);

            var result = mapper.Convert(request);

            Assert.Equal(body.Test, (string)result.Body.Test);
            Assert.Equal(body.test2, (int)result.Body.test2);
            mockHttpBodyContentMapper.Received(1).Convert(content: Arg.Any <byte[]>(), headers: Arg.Any <IDictionary <string, string> >());
        }
        public void Convert_WithPlainContentTypeHeader_HeaderIsNotAddedToHttpRequestMessageAndHttpContentMapperIsCalledWithContentType()
        {
            const string contentTypeString = "text/plain";
            var          request           = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString }
                },
                Body = new { }
            };
            var httpBodyContent = new HttpBodyContent(content: Encoding.UTF8.GetBytes(String.Empty), contentType: new MediaTypeHeaderValue(contentTypeString)
            {
                CharSet = "utf-8"
            });

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            _mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(httpBodyContent);

            var result = mapper.Convert(request);

            Assert.Empty(result.Headers);
            _mockHttpContentMapper.Received(1).Convert(httpBodyContent);
        }
Beispiel #3
0
        public void Ctor2_WithContentType_SetsContentType()
        {
            const string contentTypeString = "text/html";
            var          httpBodyContent   = new HttpBodyContent(content: Encoding.UTF8.GetBytes(String.Empty), contentType: contentTypeString, encoding: null);

            Assert.Equal(contentTypeString, httpBodyContent.ContentType);
        }
Beispiel #4
0
        public void Convert_WithContentLengthHeader_ContentLengthHeaderIsAddedToHttpRequestMessageContentHeaders()
        {
            var request = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Length", "12" }
                },
                Body = "Some content"
            };
            var httpBodyContent = new HttpBodyContent(contentType: new MediaTypeHeaderValue("text/plain")
            {
                CharSet = "utf-8"
            });

            httpBodyContent.GenerateContent(request.Body);

            var stringContent = new StringContent(request.Body, Encoding.UTF8, "text/plain");

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            _mockHttpBodyContentMapper.Convert(Arg.Any <string>(), Arg.Any <IDictionary <string, string> >()).Returns(httpBodyContent);
            _mockHttpContentMapper.Convert(httpBodyContent).Returns(stringContent);

            var result = mapper.Convert(request);

            Assert.Equal(request.Headers.Last().Key, result.Content.Headers.Last().Key);
            Assert.Equal(request.Headers.Last().Value, result.Content.Headers.Last().Value.First());
        }
        public void Ctor2_WithContentType_SetsContentType()
        {
            var contentTypeString = "text/html";
            var httpBodyContent   = new HttpBodyContent(content: String.Empty, contentType: contentTypeString, encoding: null);

            Assert.Equal(contentTypeString, httpBodyContent.ContentType);
        }
        public void Convert_WithContentTypeAndCustomHeader_OnlyCustomHeadersIsAddedToHttpRequestMessage()
        {
            const string contentTypeString = "text/plain";
            var          request           = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString },
                    { "X-Custom", "My Custom header" }
                },
                Body = new { }
            };
            var httpBodyContent = new HttpBodyContent(content: Encoding.UTF8.GetBytes(String.Empty), contentType: new MediaTypeHeaderValue(contentTypeString)
            {
                CharSet = "utf-8"
            });

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            _mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(httpBodyContent);

            var result = mapper.Convert(request);

            Assert.Equal(request.Headers.Last().Key, result.Headers.First().Key);
            Assert.Equal(request.Headers.Last().Value, result.Headers.First().Value.First());
        }
Beispiel #7
0
        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             streamArray     = ConvertStreamToBytes(from.Body);
                HttpBodyContent httpBodyContent = _httpBodyContentMapper.Convert(content: streamArray, headers: to.Headers);
                to.Body = httpBodyContent.Body;
            }

            return(to);
        }
        public void Convert_WithContentTypeSpecifiedAndAlsoBeingSetByStringContent_ContentTypeHeaderIsNotReAddedToHttpRequestMessageContentHeaders()
        {
            var request = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", "text/plain" }
                },
                Body = "Some content"
            };
            var httpBodyContent = new HttpBodyContent(body: request.Body, contentType: new MediaTypeHeaderValue("text/plain")
            {
                CharSet = "utf-8"
            });
            var stringContent = new StringContent(request.Body, Encoding.UTF8, "text/plain");

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            _mockHttpBodyContentMapper.Convert(Arg.Any <string>(), Arg.Any <IDictionary <string, string> >()).Returns(httpBodyContent);
            _mockHttpContentMapper.Convert(httpBodyContent).Returns(stringContent);

            var result = mapper.Convert(request);

            Assert.Equal(1, result.Content.Headers.Count());
            Assert.Equal(request.Headers.First().Key, result.Content.Headers.First().Key);
            Assert.Equal("text/plain; charset=utf-8", result.Content.Headers.First().Value.First());
        }
Beispiel #9
0
        public void Convert_WithPlainContentTypeAndUtf8CharsetHeader_HeaderIsNotAddedToHttpRequestMessageAndHttpContentMapperIsCalledWithEncodingAndContentType()
        {
            const string contentTypeString = "text/plain";
            const string encodingString    = "utf-8";
            var          encoding          = Encoding.UTF8;
            var          request           = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString + "; charset=" + encodingString }
                },
                Body = new { }
            };
            var httpBodyContent = new HttpBodyContent(String.Empty, contentTypeString, encoding);

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            _mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(httpBodyContent);

            var result = mapper.Convert(request);

            Assert.Empty(result.Headers);
            _mockHttpBodyContentMapper.Received(1).Convert(request.Body, request.Headers);
            _mockHttpContentMapper.Received(1).Convert(httpBodyContent);
        }
Beispiel #10
0
        public void Convert_WithPlainContentTypeHeader_HeaderIsNotAddedToHttpRequestMessageAndHttpContentMapperIsCalledWithContentType()
        {
            const string contentTypeString = "text/plain";
            var          request           = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString }
                },
                Body = new {}
            };
            var httpBodyContent = new HttpBodyContent(String.Empty, contentTypeString, null);

            var mockHttpMethodMapper      = Substitute.For <IHttpMethodMapper>();
            var mockHttpContentMapper     = Substitute.For <IHttpContentMapper>();
            var mockHttpBodyContentMapper = Substitute.For <IHttpBodyContentMapper>();

            mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(httpBodyContent);

            IHttpRequestMessageMapper mapper = new HttpRequestMessageMapper(
                mockHttpMethodMapper,
                mockHttpContentMapper,
                mockHttpBodyContentMapper);

            var result = mapper.Convert(request);

            Assert.Empty(result.Headers);
            mockHttpContentMapper.Received(1).Convert(httpBodyContent);
        }
        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);
        }
Beispiel #12
0
        public void Convert_WithJsonContentTypeAndUnicodeCharsetHeader_HeaderIsNotAddedToHttpRequestMessageAndHttpContentMapperIsCalledWithEncodingAndContentType()
        {
            const string contentTypeString = "application/json";
            const string encodingString    = "utf-16";
            var          encoding          = Encoding.Unicode;
            var          request           = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString + "; charset=" + encodingString }
                },
                Body = new { }
            };
            var httpBodyContent = new HttpBodyContent(String.Empty, contentTypeString, encoding);

            var mockHttpMethodMapper      = Substitute.For <IHttpMethodMapper>();
            var mockHttpContentMapper     = Substitute.For <IHttpContentMapper>();
            var mockHttpBodyContentMapper = Substitute.For <IHttpBodyContentMapper>();

            mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(httpBodyContent);

            IHttpRequestMessageMapper mapper = new HttpRequestMessageMapper(
                mockHttpMethodMapper,
                mockHttpContentMapper,
                mockHttpBodyContentMapper);

            var result = mapper.Convert(request);

            Assert.Empty(result.Headers);
            mockHttpBodyContentMapper.Received(1).Convert(request.Body, request.Headers);
            mockHttpContentMapper.Received(1).Convert(httpBodyContent);
        }
Beispiel #13
0
        public void Convert_WithContentTypeAndCustomHeader_OnlyCustomHeadersIsAddedToHttpRequestMessage()
        {
            const string contentTypeString = "text/plain";
            var          request           = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString },
                    { "X-Custom", "My Custom header" }
                },
                Body = new { }
            };
            var httpBodyContent = new HttpBodyContent(String.Empty, contentTypeString, null);

            var mockHttpMethodMapper      = Substitute.For <IHttpMethodMapper>();
            var mockHttpContentMapper     = Substitute.For <IHttpContentMapper>();
            var mockHttpBodyContentMapper = Substitute.For <IHttpBodyContentMapper>();

            mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(httpBodyContent);

            IHttpRequestMessageMapper mapper = new HttpRequestMessageMapper(
                mockHttpMethodMapper,
                mockHttpContentMapper,
                mockHttpBodyContentMapper);

            var result = mapper.Convert(request);

            Assert.Equal(request.Headers.Last().Key, result.Headers.First().Key);
            Assert.Equal(request.Headers.Last().Value, result.Headers.First().Value.First());
        }
Beispiel #14
0
        public void Convert_WithPlainTextBody_CallsHttpBodyContentMapperAndCorrectlySetsBody()
        {
            const string content         = "Plain text body";
            Request      request         = GetPreCannedRequest(content: content);
            var          httpBodyContent = new HttpBodyContent(new MediaTypeHeaderValue("text/plain")
            {
                CharSet = "utf-8"
            });

            httpBodyContent.GenerateContent(request.Body);

            var mockHttpVerbMapper        = Substitute.For <IHttpVerbMapper>();
            var mockHttpBodyContentMapper = Substitute.For <IHttpBodyContentMapper>();

            mockHttpVerbMapper.Convert("GET").Returns(HttpVerb.Get);
            mockHttpBodyContentMapper.Convert(content: Arg.Any <byte[]>(), headers: null).Returns(httpBodyContent);

            var mapper = new ProviderServiceRequestMapper(mockHttpVerbMapper, mockHttpBodyContentMapper);

            ProviderServiceRequest result = mapper.Convert(request);

            Assert.Equal(content, result.Body);

            mockHttpBodyContentMapper.Received(1).Convert(content: Arg.Any <byte[]>(), headers: null);
        }
Beispiel #15
0
        public void Ctor1_WithContentType_SetsContentType()
        {
            const string contentTypeString = "text/html";
            var          httpBodyContent   = new HttpBodyContent(body: new {}, contentType: contentTypeString, encoding: null);

            Assert.Equal(contentTypeString, httpBodyContent.ContentType);
        }
        public void Ctor2_WithEncoding_SetsEncoding()
        {
            var encoding        = Encoding.Unicode;
            var httpBodyContent = new HttpBodyContent(body: String.Empty, contentType: null, encoding: encoding);

            Assert.Equal(encoding, httpBodyContent.Encoding);
        }
        public void Convert_WithContentTypeSpecifiedButNotBeingSetByByteArrayContent_ContentTypeHeaderIsNotReAddedToHttpRequestMessageContentHeaders()
        {
            var request = new ProviderServiceRequest
            {
                Method  = HttpVerb.Post,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", "application/octet-stream" }
                },
                Body = Encoding.UTF8.GetBytes("Some content")
            };
            var httpBodyContent = new HttpBodyContent(body: request.Body, contentType: new MediaTypeHeaderValue("text/plain")
            {
                CharSet = "utf-8"
            });
            var byteArrayContent = new ByteArrayContent(request.Body as byte[]);

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post);
            _mockHttpBodyContentMapper.Convert(body: Arg.Any <byte[]>(), headers: Arg.Any <IDictionary <string, string> >()).Returns(httpBodyContent);
            _mockHttpContentMapper.Convert(httpBodyContent).Returns(byteArrayContent);

            var result = mapper.Convert(request);

            Assert.Equal(1, result.Content.Headers.Count());
            Assert.Equal(request.Headers.First().Key, result.Content.Headers.First().Key);
            Assert.Equal("application/octet-stream", result.Content.Headers.First().Value.First());
        }
Beispiel #18
0
        public void Convert_WithTheWorks_CorrectlyMappedHttpRequestMessageIsReturned()
        {
            const string encodingString    = "utf-8";
            var          encoding          = Encoding.UTF8;
            const string contentTypeString = "application/json";
            const string bodyJson          = "{\"Test\":\"tester\",\"Testing\":1}";

            var request = new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString + "; charset=" + encodingString },
                    { "X-Custom", "My Custom header" },
                    { "Content-Length", "10000" }, //This header is removed and replace with the correct value of 29
                },
                Body = new
                {
                    Test    = "tester",
                    Testing = 1
                }
            };
            var httpBodyContent = new HttpBodyContent(bodyJson, contentTypeString, encoding);

            var mockHttpMethodMapper      = Substitute.For <IHttpMethodMapper>();
            var mockHttpContentMapper     = Substitute.For <IHttpContentMapper>();
            var mockHttpBodyContentMapper = Substitute.For <IHttpBodyContentMapper>();

            mockHttpMethodMapper.Convert(HttpVerb.Get).Returns(HttpMethod.Get);
            mockHttpContentMapper.Convert(httpBodyContent).Returns(new StringContent(bodyJson, encoding, contentTypeString));
            mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(httpBodyContent);

            IHttpRequestMessageMapper mapper = new HttpRequestMessageMapper(
                mockHttpMethodMapper,
                mockHttpContentMapper,
                mockHttpBodyContentMapper);

            var result         = mapper.Convert(request);
            var requestContent = result.Content.ReadAsStringAsync().Result;

            var contentTypeHeader   = result.Content.Headers.First(x => x.Key.Equals("Content-Type"));
            var customHeader        = result.Headers.First(x => x.Key.Equals("X-Custom"));
            var contentLengthHeader = result.Content.Headers.First(x => x.Key.Equals("Content-Length"));

            Assert.Equal(bodyJson, requestContent);

            //Content-Type header
            Assert.Equal(request.Headers.First().Key, contentTypeHeader.Key);
            Assert.Equal(request.Headers.First().Value, contentTypeHeader.Value.First());

            //X-Custom header
            Assert.Equal(request.Headers.Skip(1).First().Key, customHeader.Key);
            Assert.Equal(request.Headers.Skip(1).First().Value, customHeader.Value.First());

            //Content-Length header
            Assert.Equal(request.Headers.Last().Key, contentLengthHeader.Key);
            Assert.Equal("29", contentLengthHeader.Value.First());
        }
        public void Ctor2_WithPlainTextContent_SetsBodyAndContent()
        {
            const string content         = "Some plain text";
            var          httpBodyContent = new HttpBodyContent(content: content, contentType: "application/plain", encoding: null);

            Assert.Equal(content, httpBodyContent.Content);
            Assert.Equal(content, httpBodyContent.Body);
        }
        public void Ctor1_WithPlainTextBody_SetsBodyAndContent()
        {
            const string body            = "Some plain text";
            var          httpBodyContent = new HttpBodyContent(body: body, contentType: "application/plain", encoding: null);

            Assert.Equal(body, httpBodyContent.Content);
            Assert.Equal(body, httpBodyContent.Body);
        }
Beispiel #21
0
        public void Ctor1_WithBinaryBody_SetsBodyAndContent()
        {
            var body = new byte[] { 1, 2, 3 };

            var httpBodyContent = new HttpBodyContent(body, "application/octet-stream", Encoding.UTF8);

            Assert.Equal(body, httpBodyContent.Body);
            Assert.Equal(Encoding.UTF8.GetString(body), httpBodyContent.Content);
        }
Beispiel #22
0
        public HttpContent Convert(HttpBodyContent from)
        {
            if (from == null)
            {
                return(null);
            }

            return(new StringContent(from.Content, from.Encoding, from.ContentType));
        }
        public void Convert_WithEmptyContent_ReturnsNull()
        {
            var httpBodyContent = new HttpBodyContent(content: Encoding.UTF8.GetBytes(String.Empty), contentType: "text/plain", encoding: Encoding.UTF8);
            var mapper          = GetSubject();

            var result = mapper.Convert(httpBodyContent);

            Assert.Empty(result.ReadAsStringAsync().Result);
        }
Beispiel #24
0
        public void Ctor2_WithBinaryContent_SetsBodyAndContent()
        {
            const string content         = "LOL";
            var          httpBodyContent = new HttpBodyContent(content, "application/octet-stream", Encoding.UTF8);

            Assert.Equal(content, httpBodyContent.Content);
            Assert.IsType <byte[]>(httpBodyContent.Body);
            Assert.Equal(new byte[] { 76, 79, 76 }, httpBodyContent.Body);
        }
        public void Ctor2_WithEmptyContent_ReturnsEmptyUtf8ByteArray()
        {
            var httpBodyContent = new HttpBodyContent(content: Encoding.UTF8.GetBytes(String.Empty), contentType: new MediaTypeHeaderValue("text/plain")
            {
                CharSet = "utf-8"
            });

            Assert.Empty(httpBodyContent.ContentBytes);
        }
Beispiel #26
0
        public void Ctor1_WithBinaryBody_SetsBodyAndAndBase64EncodesTheContent()
        {
            var body = new byte[] { 1, 2, 3 };

            var httpBodyContent = new HttpBodyContent(body: body, contentType: "application/octet-stream", encoding: Encoding.UTF8);

            Assert.Equal(body, httpBodyContent.Body);
            Assert.Equal(Convert.ToBase64String(body), httpBodyContent.Content);
        }
Beispiel #27
0
        public void Convert_WithTheWorks_CorrectlyMappedHttpRequestMessageIsReturned()
        {
            const string encodingString    = "utf-8";
            const string contentTypeString = "application/json";
            const string bodyJson          = "{\"Test\":\"tester\",\"Testing\":1}";

            var request = new ProviderServiceRequest
            {
                Method  = HttpVerb.Get,
                Path    = "/events",
                Headers = new Dictionary <string, string>
                {
                    { "Content-Type", contentTypeString + "; charset=" + encodingString },
                    { "X-Custom", "My Custom header" },
                    { "Content-Length", "1000" }
                },
                Body = new
                {
                    Test    = "tester",
                    Testing = 1
                }
            };
            var httpBodyContent = new HttpBodyContent(new MediaTypeHeaderValue(contentTypeString)
            {
                CharSet = encodingString
            });

            httpBodyContent.GenerateContent(bodyJson);

            var mapper = GetSubject();

            _mockHttpMethodMapper.Convert(HttpVerb.Get).Returns(HttpMethod.Get);
            _mockHttpContentMapper.Convert(httpBodyContent).Returns(new StringContent(bodyJson, Encoding.UTF8, contentTypeString));
            _mockHttpBodyContentMapper.Convert(Arg.Any <object>(), request.Headers).Returns(httpBodyContent);

            var result         = mapper.Convert(request);
            var requestContent = result.Content.ReadAsStringAsync().Result;

            var contentTypeHeader   = result.Content.Headers.First(x => x.Key.Equals("Content-Type"));
            var customHeader        = result.Headers.First(x => x.Key.Equals("X-Custom"));
            var contentLengthHeader = result.Content.Headers.First(x => x.Key.Equals("Content-Length"));

            Assert.Equal(bodyJson, requestContent);

            //Content-Type header
            Assert.Equal(request.Headers.First().Key, contentTypeHeader.Key);
            Assert.Equal(request.Headers.First().Value, contentTypeHeader.Value.First());

            //X-Custom header
            Assert.Equal(request.Headers.Skip(1).First().Key, customHeader.Key);
            Assert.Equal(request.Headers.Skip(1).First().Value, customHeader.Value.First());

            //Content-Length header
            Assert.Equal(request.Headers.Last().Key, contentLengthHeader.Key);
            Assert.Equal(request.Headers.Last().Value, contentLengthHeader.Value.First());
        }
Beispiel #28
0
        public void Ctor2_WithBinaryContent_SetsBodyAndBase64EncodesContent()
        {
            const string content         = "LOL";
            var          contentBytes    = Encoding.UTF8.GetBytes(content);
            var          httpBodyContent = new HttpBodyContent(content: contentBytes, contentType: "application/octet-stream", encoding: Encoding.UTF8);

            Assert.Equal(content, httpBodyContent.Content);
            Assert.IsType <string>(httpBodyContent.Body);
            Assert.Equal(Convert.ToBase64String(contentBytes), httpBodyContent.Body);
        }
        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);
        }
        public HttpBodyContent Convert(byte[] content, IDictionary <string, string> headers)
        {
            if (content == null)
            {
                return(null);
            }

            var bodyContent = new HttpBodyContent(content, ParseContentTypeHeader(headers));

            return(bodyContent);
        }