public void Ctor1_WithContentType_SetsContentType() { var 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 Ctor2_WithContentType_SetsContentType() { const string contentTypeString = "text/html"; var httpBodyContent = new HttpBodyContent(content: String.Empty, contentType: contentTypeString, encoding: null); Assert.Equal(contentTypeString, httpBodyContent.ContentType); }
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(request.Body, "text/plain", Encoding.UTF8); 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 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_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()); }
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); }
public void Convert_WithEmptyContent_ReturnsNull() { var httpBodyContent = new HttpBodyContent(String.Empty, "text/plain", Encoding.UTF8); var mapper = GetSubject(); var result = mapper.Convert(httpBodyContent); Assert.Empty(result.ReadAsStringAsync().Result); }
public HttpContent Convert(HttpBodyContent from) { if (from == null) { return null; } return new StringContent(from.Content, from.Encoding, from.ContentType); }
public void Ctor1_WithJsonBodyAndTitleCasedContentType_SetsBodyAndContent() { var body = new { Test = "tester", tesTer = 1 }; const string content = "{\"Test\":\"tester\",\"tesTer\":1}"; var httpBodyContent = new HttpBodyContent(body: body, contentType: "Application/Json", encoding: null); Assert.Equal(content, httpBodyContent.Content); Assert.Equal(body, httpBodyContent.Body); }
public void Convert_WithJsonBody_CallsConvertOnHttpBodyContentMapperAndAssignsContents() { const string contentTypeString = "application/json"; var response = new ProviderServiceResponse { Status = 200, Headers = new Dictionary<string, string> { { "Content-Type", contentTypeString } }, Body = new { Test = "tester", Test2 = 1 } }; var jsonBody = "{\"Test\":\"tester\",\"Test2\":1}"; var httpBodyContent = new HttpBodyContent(content: Encoding.UTF8.GetBytes(jsonBody), contentType: contentTypeString, encoding: null); var mockHttpBodyContentMapper = Substitute.For<IHttpBodyContentMapper>(); mockHttpBodyContentMapper.Convert(body: Arg.Any<object>(), headers: response.Headers) .Returns(httpBodyContent); var mapper = new NancyResponseMapper(mockHttpBodyContentMapper); var result = mapper.Convert(response); string content; using (var stream = new MemoryStream()) { result.Contents(stream); stream.Position = 0; using (var reader = new StreamReader(stream)) { content = reader.ReadToEnd(); } } Assert.Equal(jsonBody, content); mockHttpBodyContentMapper.Received(1).Convert(body: Arg.Any<object>(), headers: response.Headers); }
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(request.Body, "text/plain", Encoding.UTF8); 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()); }
public void Ctor2_WithJsonContent_SetsBodyAndContent() { var body = new { Test = "tester", tesTer = 1 }; const string content = "{\"Test\":\"tester\",\"tesTer\":1}"; var httpBodyContent = new HttpBodyContent(content: content, contentType: "application/json", encoding: null); Assert.Equal(content, httpBodyContent.Content); Assert.Equal(body.Test, (string) httpBodyContent.Body.Test); Assert.Equal(body.tesTer, (int) httpBodyContent.Body.tesTer); }
public void Ctor1_WithCustomJsonBody_SetsBodyAndContent() { var body = new { Test = "tester", tesTer = 1 }; const string content = "{\"Test\":\"tester\",\"tesTer\":1}"; var httpBodyContent = new HttpBodyContent(body: body, contentType: "application/x-amz-json-1.1", encoding: null); Assert.Equal(content, httpBodyContent.Content); Assert.Equal(body, httpBodyContent.Body); }
public void Convert_WithPlainTextBody_CallsHttpBodyContentMapperAndCorrectlySetsBody() { const string content = "Plain text body"; var contentByes = Encoding.UTF8.GetBytes(content); var request = GetPreCannedRequest(content: content); var httpBodyContent = new HttpBodyContent(content, null, null); 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); var result = mapper.Convert(request); Assert.Equal(content, result.Body); mockHttpBodyContentMapper.Received(1).Convert(content: Arg.Any<byte[]>(), headers: null); }
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); }
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(request.Body, "text/plain", Encoding.UTF8); var byteArrayContent = new ByteArrayContent(request.Body as byte[]); var mapper = GetSubject(); _mockHttpMethodMapper.Convert(HttpVerb.Post).Returns(HttpMethod.Post); _mockHttpBodyContentMapper.Convert(Arg.Any<object>(), 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()); }
public void Encoding_WithNullEncodingSet_ReturnsUtf8Encoding() { var httpBodyContent = new HttpBodyContent(new {}, null, null); Assert.Equal(Encoding.UTF8, httpBodyContent.Encoding); }
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); }
public void ContentBytes_WithEmptyContent_ReturnsEmptyUtf8ByteArray() { var httpBodyContent = new HttpBodyContent(String.Empty, null, null); Assert.Empty(httpBodyContent.ContentBytes); }
public void ContentType_WithEmptyContentTypeSet_ReturnsPlainContentType() { var httpBodyContent = new HttpBodyContent(new {}, String.Empty, null); Assert.Equal("text/plain", httpBodyContent.ContentType); }
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 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 void Convert_WithPlainTextBody_CallsConvertOnHttpBodyContentMapperAndAssignsContents() { const string contentTypeString = "text/plain"; var response = new ProviderServiceResponse { Status = 200, Headers = new Dictionary<string, string> { { "Content-Type", contentTypeString } }, Body = "This is a plain body" }; var httpBodyContent = new HttpBodyContent(body: response.Body, contentType: contentTypeString, encoding: null); var mockHttpBodyContentMapper = Substitute.For<IHttpBodyContentMapper>(); mockHttpBodyContentMapper.Convert(body: Arg.Any<object>(), headers: response.Headers) .Returns(httpBodyContent); var mapper = new NancyResponseMapper(mockHttpBodyContentMapper); var result = mapper.Convert(response); string content; using (var stream = new MemoryStream()) { result.Contents(stream); stream.Position = 0; using (var reader = new StreamReader(stream)) { content = reader.ReadToEnd(); } } Assert.Equal(response.Body, content); mockHttpBodyContentMapper.Received(1).Convert(body: Arg.Any<object>(), headers: response.Headers); }
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); }
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_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 Convert_WithPlainContentTypeHeaderLowercased_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: contentTypeString, encoding: null); 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); }