Skip to content

Commit

Permalink
Adding some more test for PactProviderServiceRequestMapper and a few …
Browse files Browse the repository at this point in the history
…other little tweaks
  • Loading branch information
neilcampbell committed Jul 15, 2014
1 parent 3852d14 commit 5bec4f6
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void Convert_WithHeaders_CorrectlySetsHeaders()

var result = mapper.Convert(request);

Assert.Equal(contentType + "; " + contentEncoding, result.Headers["Content-Type"]);
Assert.Equal(contentType + ", " + contentEncoding, result.Headers["Content-Type"]);
Assert.Equal(customHeaderValue, result.Headers["X-Custom"]);
}

Expand Down Expand Up @@ -158,8 +158,8 @@ public void Convert_WithJsonBody_CallsHttpBodyContentMapperAndCorrectlySetsBody(

var result = mapper.Convert(request);

Assert.Equal(body.Test, result.Body.Test);
Assert.Equal(body.test2, result.Body.test2);
Assert.Equal(body.Test, (string)result.Body.Test);
Assert.Equal(body.test2, (int)result.Body.test2);
mockHttpBodyContentMapper.Received(1).Convert(content: content, headers: Arg.Any<IDictionary<string, string>>());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
using NSubstitute;
using PactNet.Mocks.MockHttpService.Mappers;
using PactNet.Mocks.MockHttpService.Models;
using Xunit;

namespace PactNet.Tests.Mocks.MockHttpService.Mappers
{
public class PactProviderServiceResponseMapperTests
{
[Fact]
public void Convert_WithNullHttpResponseMessage_ReturnsNull()
{
IPactProviderServiceResponseMapper mapper = new PactProviderServiceResponseMapper();

mapper.Convert(null);
}

[Fact]
public void Convert_WithStatusCode_CorrectlyMapsStatusCode()
{
var message = new HttpResponseMessage { StatusCode = HttpStatusCode.Accepted };

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

IPactProviderServiceResponseMapper mapper = new PactProviderServiceResponseMapper(mockHttpBodyContentMapper);

var result = mapper.Convert(message);

Assert.Equal(202, result.Status);
}

[Fact]
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>();

IPactProviderServiceResponseMapper mapper = new PactProviderServiceResponseMapper(mockHttpBodyContentMapper);

var result = mapper.Convert(message);

Assert.Equal(headerValue, result.Headers["X-Custom"]);
}

[Fact]
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));

IPactProviderServiceResponseMapper mapper = new PactProviderServiceResponseMapper(mockHttpBodyContentMapper);

var result = mapper.Convert(message);

Assert.Equal("text/plain; charset=utf-8", result.Headers["Content-Type"]);
}

[Fact]
public void Convert_WithResponseAndResponseContentHeaders_CorrectlyMapsHeaders()
{
var stringContent = new StringContent("", 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<string>(), Arg.Any<IDictionary<string, string>>()).Returns(new HttpBodyContent("", "text/plain", Encoding.UTF8));

IPactProviderServiceResponseMapper mapper = new PactProviderServiceResponseMapper(mockHttpBodyContentMapper);

var result = mapper.Convert(message);

Assert.Equal(headerValue, result.Headers["X-Custom"]);
Assert.Equal("text/plain; charset=utf-8", result.Headers["Content-Type"]);
}

[Fact]
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));

IPactProviderServiceResponseMapper mapper = new PactProviderServiceResponseMapper(mockHttpBodyContentMapper);

var result = mapper.Convert(message);

Assert.Equal(content, result.Body);
mockHttpBodyContentMapper.Received(1).Convert(content, Arg.Any<Dictionary<string, string>>());
}

[Fact]
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));

IPactProviderServiceResponseMapper mapper = new PactProviderServiceResponseMapper(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>>());
}
}
}
1 change: 1 addition & 0 deletions PactNet.Tests/PactNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="Mocks\MockHttpService\Mappers\HttpVerbMapperTests.cs" />
<Compile Include="Mocks\MockHttpService\Mappers\NancyResponseMapperTests.cs" />
<Compile Include="Mocks\MockHttpService\Mappers\PactProviderServiceRequestMapperTests.cs" />
<Compile Include="Mocks\MockHttpService\Mappers\PactProviderServiceResponseMapperTests.cs" />
<Compile Include="Mocks\MockHttpService\MockProviderNancyRequestDispatcherTests.cs" />
<Compile Include="Mocks\MockHttpService\MockProviderServiceTests.cs" />
<Compile Include="Mocks\MockHttpService\Models\PactProviderServiceRequestTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public PactProviderServiceRequest Convert(Request from)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ namespace PactNet.Mocks.MockHttpService.Mappers
{
public class PactProviderServiceResponseMapper : IPactProviderServiceResponseMapper
{
private readonly IHttpBodyContentMapper _httpBodyContentMapper;

[Obsolete("For testing only.")]
public PactProviderServiceResponseMapper(IHttpBodyContentMapper httpBodyContentMapper)
{
_httpBodyContentMapper = httpBodyContentMapper;
}

public PactProviderServiceResponseMapper() : this(
new HttpBodyContentMapper())
{

}

public PactProviderServiceResponse Convert(HttpResponseMessage from)
{
if (from == null)
Expand All @@ -20,23 +34,27 @@ public PactProviderServiceResponse Convert(HttpResponseMessage from)
var to = new PactProviderServiceResponse
{
Status = (int) from.StatusCode,
Headers = ConvertHeaders(from.Headers, from.Content.Headers)
Headers = ConvertHeaders(from.Headers, from.Content)
};

var responseContent = from.Content.ReadAsStringAsync().Result;
if (!String.IsNullOrEmpty(responseContent))
if(from.Content != null)
{
to.Body = JsonConvert.DeserializeObject<dynamic>(responseContent);
var responseContent = from.Content.ReadAsStringAsync().Result;
if (responseContent != null)
{
var httpBodyContent = _httpBodyContentMapper.Convert(responseContent, to.Headers);

to.Body = httpBodyContent.Body;
}
}

return to;
}

//TODO: This can be split out into a seperate mapper
private Dictionary<string, string> ConvertHeaders(HttpResponseHeaders responseHeaders, HttpContentHeaders contentHeaders)
private Dictionary<string, string> ConvertHeaders(HttpResponseHeaders responseHeaders, HttpContent httpContent)
{
if ((responseHeaders == null || !responseHeaders.Any()) &&
(contentHeaders == null || !contentHeaders.Any()))
(httpContent == null || (httpContent.Headers == null || !httpContent.Headers.Any())))
{
return null;
}
Expand All @@ -47,15 +65,15 @@ private Dictionary<string, string> ConvertHeaders(HttpResponseHeaders responseHe
{
foreach (var responseHeader in responseHeaders)
{
headers.Add(responseHeader.Key, responseHeader.Value.First());
headers.Add(responseHeader.Key, String.Join(", ", responseHeader.Value.Select(x => x)));
}
}

if (contentHeaders != null && contentHeaders.Any())
if (httpContent != null && httpContent.Headers != null && httpContent.Headers.Any())
{
foreach (var contentHeader in contentHeaders)
foreach (var contentHeader in httpContent.Headers)
{
headers.Add(contentHeader.Key, contentHeader.Value.First());
headers.Add(contentHeader.Key, String.Join(", ", contentHeader.Value.Select(x => x)));
}
}

Expand Down
4 changes: 2 additions & 2 deletions PactNet/Mocks/MockHttpService/Models/HttpBodyContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ public HttpBodyContent(string content, string contentType, Encoding encoding)

Content = content;
Body = ContentType.Equals("application/json")
? JsonConvert.DeserializeObject<ExpandoObject>(content)
: content as dynamic;
? JsonConvert.DeserializeObject<dynamic>(content)
: content;
}
}
}

0 comments on commit 5bec4f6

Please sign in to comment.