forked from pact-foundation/pact-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding some more test for PactProviderServiceRequestMapper and a few …
…other little tweaks
- Loading branch information
1 parent
3852d14
commit 5bec4f6
Showing
6 changed files
with
192 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
PactNet.Tests/Mocks/MockHttpService/Mappers/PactProviderServiceResponseMapperTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>>()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters