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(new MediaTypeHeaderValue("text/plain") { CharSet = "utf-8" }); httpBodyContent.GenerateContent(request.Body); 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()); }
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); }
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(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(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 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()); }
public HttpBodyContent Convert(dynamic body, IDictionary <string, string> headers) { if (body == null) { return(null); } MediaTypeHeaderValue parsedHeaders = ParseContentTypeHeader(headers); var bodyContent = new HttpBodyContent(parsedHeaders); bodyContent.GenerateContent(body); return(bodyContent); }
public void Ctor1_WithPlainTextBody_SetsBodyAndContent() { const string body = "Some plain text"; var httpBodyContent = new HttpBodyContent(new MediaTypeHeaderValue("application/plain") { CharSet = "utf-8" }); httpBodyContent.GenerateContent(body); Assert.Equal(body, httpBodyContent.Content); Assert.Equal(body, httpBodyContent.Body); }
public void Ctor1_WithBinaryBody_SetsBodyAndAndBase64EncodesTheContent() { var body = new byte[] { 1, 2, 3 }; var httpBodyContent = new HttpBodyContent(new MediaTypeHeaderValue("application/octet-stream") { CharSet = "utf-8" }); httpBodyContent.GenerateContent(body); Assert.Equal(body, httpBodyContent.Body); Assert.Equal(Convert.ToBase64String(body), httpBodyContent.Content); }
public void Ctor1_WithJsonBody_SetsBodyAndContent() { var body = new { Test = "tester", tesTer = 1 }; const string content = "{\"Test\":\"tester\",\"tesTer\":1}"; var httpBodyContent = new HttpBodyContent(new MediaTypeHeaderValue("application/json") { CharSet = "utf-8" }); httpBodyContent.GenerateContent(body); Assert.Equal(content, httpBodyContent.Content); Assert.Equal(body, httpBodyContent.Body); }
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(new MediaTypeHeaderValue(contentTypeString) { CharSet = "utf-8" }); httpBodyContent.GenerateContent(response.Body); 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_WithContentAndContentType_SetsContentType() { const string contentType = "text/html"; const string parameterName = "date-format"; const string parameterValue = "json"; const string charSet = "utf-16"; const string body = "<html/>"; var httpBodyContent = new HttpBodyContent(contentType: new MediaTypeHeaderValue(contentType) { CharSet = charSet, Parameters = { new NameValueHeaderValue(parameterName, parameterValue) } }); httpBodyContent.GenerateContent(body); Assert.Equal(body, httpBodyContent.Content); Assert.Equal(contentType, httpBodyContent.ContentType.MediaType); Assert.Contains(new NameValueHeaderValue(parameterName, parameterValue), httpBodyContent.ContentType.Parameters); Assert.Equal(charSet, httpBodyContent.ContentType.CharSet); Assert.Equal(Encoding.Unicode, httpBodyContent.Encoding); }