public void Convert_WithJsonContent_CallsConvertOnHttpBodyContentMapperAndCorrectlyMapsBody() { var body = new { Test = "tester", test2 = 1 }; const string content = "{\"Test\":\"tester\",\"test2\":1}"; var stringContent = new StringContent(content, Encoding.UTF8, "application/json"); var message = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = stringContent }; var mockHttpBodyContentMapper = Substitute.For<IHttpBodyContentMapper>(); mockHttpBodyContentMapper.Convert(Arg.Any<string>(), Arg.Any<IDictionary<string, string>>()).Returns(new HttpBodyContent(content, "application/json", Encoding.UTF8)); IProviderServiceResponseMapper mapper = new ProviderServiceResponseMapper(mockHttpBodyContentMapper); var result = mapper.Convert(message); Assert.Equal(body.Test, (string)result.Body.Test); Assert.Equal(body.test2, (int)result.Body.test2); mockHttpBodyContentMapper.Received(1).Convert(content, Arg.Any<Dictionary<string, string>>()); }
public void Convert_WithStatusCode_CorrectlyMapsStatusCode() { var message = new HttpResponseMessage { StatusCode = HttpStatusCode.Accepted }; var mockHttpBodyContentMapper = Substitute.For<IHttpBodyContentMapper>(); IProviderServiceResponseMapper mapper = new ProviderServiceResponseMapper(mockHttpBodyContentMapper); var result = mapper.Convert(message); Assert.Equal(202, result.Status); }
public void Convert_WithResponseHeaders_CorrectlyMapsHeaders() { const string headerValue = "Customer Header Value"; var message = new HttpResponseMessage { StatusCode = HttpStatusCode.OK }; message.Headers.Add("X-Custom", headerValue); var mockHttpBodyContentMapper = Substitute.For<IHttpBodyContentMapper>(); IProviderServiceResponseMapper mapper = new ProviderServiceResponseMapper(mockHttpBodyContentMapper); var result = mapper.Convert(message); Assert.Equal(headerValue, result.Headers["X-Custom"]); }
public void Convert_WithResponseContentHeaders_CorrectlyMapsHeaders() { var stringContent = new StringContent("", Encoding.UTF8, "text/plain"); var message = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = stringContent }; var mockHttpBodyContentMapper = Substitute.For<IHttpBodyContentMapper>(); mockHttpBodyContentMapper.Convert(Arg.Any<string>(), Arg.Any<IDictionary<string, string>>()).Returns(new HttpBodyContent("", "text/plain", Encoding.UTF8)); IProviderServiceResponseMapper mapper = new ProviderServiceResponseMapper(mockHttpBodyContentMapper); var result = mapper.Convert(message); Assert.Equal("text/plain; charset=utf-8", result.Headers["Content-Type"]); }
public void Convert_WithPlainTextContent_CallsConvertOnHttpBodyContentMapperAndCorrectlyMapsBody() { const string content = "some plaintext content"; var stringContent = new StringContent(content, Encoding.UTF8, "text/plain"); var message = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = stringContent }; var mockHttpBodyContentMapper = Substitute.For<IHttpBodyContentMapper>(); mockHttpBodyContentMapper.Convert(Arg.Any<string>(), Arg.Any<IDictionary<string, string>>()).Returns(new HttpBodyContent(content, "text/plain", Encoding.UTF8)); IProviderServiceResponseMapper mapper = new ProviderServiceResponseMapper(mockHttpBodyContentMapper); var result = mapper.Convert(message); Assert.Equal(content, result.Body); mockHttpBodyContentMapper.Received(1).Convert(content, Arg.Any<Dictionary<string, string>>()); }
public void Convert_WithNullHttpResponseMessage_ReturnsNull() { IProviderServiceResponseMapper mapper = new ProviderServiceResponseMapper(); mapper.Convert(null); }
public void Convert_WithResponseAndResponseContentHeaders_CorrectlyMapsHeaders() { var stringContent = new StringContent(String.Empty, Encoding.UTF8, "text/plain"); const string headerValue = "Customer Header Value"; var message = new HttpResponseMessage { StatusCode = HttpStatusCode.OK, Content = stringContent }; message.Headers.Add("X-Custom", headerValue); var mockHttpBodyContentMapper = Substitute.For<IHttpBodyContentMapper>(); mockHttpBodyContentMapper.Convert(Arg.Any<byte[]>(), Arg.Any<IDictionary<string, string>>()).Returns(new HttpBodyContent(content: Encoding.UTF8.GetBytes(String.Empty), contentType: "text/plain", encoding: Encoding.UTF8)); IProviderServiceResponseMapper mapper = new ProviderServiceResponseMapper(mockHttpBodyContentMapper); var result = mapper.Convert(message); Assert.Equal(headerValue, result.Headers["X-Custom"]); Assert.Equal("text/plain; charset=utf-8", result.Headers["Content-Type"]); }