Skip to content

Commit

Permalink
Adding some integration test for the pactbuilder failure scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
neilcampbell committed Aug 26, 2014
1 parent 9a512cb commit 32d3c6a
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 9 deletions.
28 changes: 28 additions & 0 deletions PactNet.Tests/IntegrationTests/IntegrationTestsMyApiPact.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using PactNet.Mocks.MockHttpService;

namespace PactNet.Tests.IntegrationTests
{
public class IntegrationTestsMyApiPact : IDisposable
{
public IPactBuilder PactBuilder { get; private set; }
public IMockProviderService MockProviderService { get; private set; }

public int MockServerPort { get { return 4321; } }
public string MockProviderServiceBaseUri { get { return String.Format("http://localhost:{0}", MockServerPort); } }

public IntegrationTestsMyApiPact()
{
PactBuilder = new PactBuilder()
.ServiceConsumer("IntegrationTests")
.HasPactWith("MyApi");

MockProviderService = PactBuilder.MockService(MockServerPort);
}

public void Dispose()
{
PactBuilder.Build();
}
}
}
114 changes: 114 additions & 0 deletions PactNet.Tests/IntegrationTests/PactBuilderFailureIntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using PactNet.Mocks.MockHttpService;
using PactNet.Mocks.MockHttpService.Models;
using Xunit;

namespace PactNet.Tests.IntegrationTests
{
public class PactBuilderFailureIntegrationTests : IUseFixture<IntegrationTestsMyApiPact>
{
private IMockProviderService _mockProviderService;
private string _mockProviderServiceBaseUri;

public void SetFixture(IntegrationTestsMyApiPact data)
{
_mockProviderService = data.MockProviderService;
_mockProviderServiceBaseUri = data.MockProviderServiceBaseUri;
_mockProviderService.ClearInteractions();
}

[Fact]
public void WhenRegisteringAnInteractionThatIsNeverSent_ThenInvalidOperationExceptionIsThrown()
{
_mockProviderService
.UponReceiving("A POST request to create a new thing")
.With(new ProviderServiceRequest
{
Method = HttpVerb.Post,
Path = "/things",
Headers = new Dictionary<string, string>
{
{ "Content-Type", "application/json; charset=utf-8" }
},
Body = new
{
thingId = 1234,
type = "Awesome"
}
})
.WillRespondWith(new ProviderServiceResponse
{
Status = 201
});

Assert.Throws<InvalidOperationException>(() => _mockProviderService.VerifyInteractions());
}

[Fact]
public void WhenRegisteringAnInteractionThatIsSentMultipleTimes_ThenInvalidOperationExceptionIsThrown()
{
_mockProviderService
.UponReceiving("A GET request to retrieve a thing")
.With(new ProviderServiceRequest
{
Method = HttpVerb.Get,
Path = "/things/1234"
})
.WillRespondWith(new ProviderServiceResponse
{
Status = 200
});

var httpClient = new HttpClient {BaseAddress = new Uri(_mockProviderServiceBaseUri)};

var request1 = new HttpRequestMessage(HttpMethod.Get, "/things/1234");
var request2 = new HttpRequestMessage(HttpMethod.Get, "/things/1234");

var response1 = httpClient.SendAsync(request1).Result;
var response2 = httpClient.SendAsync(request2).Result;

if (response1.StatusCode != HttpStatusCode.OK || response2.StatusCode != HttpStatusCode.OK)
{
throw new Exception("Wrong status code was returned");
}

Assert.Throws<InvalidOperationException>(() => _mockProviderService.VerifyInteractions());
}

[Fact]
public void WhenRegisteringAnInteractionWhereTheRequestDoesNotExactlyMatchTheActualRequest_ThenInvalidOperationExceptionIsThrown()
{
_mockProviderService
.UponReceiving("A GET request to retrieve things by type")
.With(new ProviderServiceRequest
{
Method = HttpVerb.Get,
Path = "/things",
Query = "type=awesome",
Headers = new Dictionary<string, string>
{
{ "Accept", "application/json; charset=utf-8" }
},
})
.WillRespondWith(new ProviderServiceResponse
{
Status = 200
});

var httpClient = new HttpClient { BaseAddress = new Uri(_mockProviderServiceBaseUri) };

var request = new HttpRequestMessage(HttpMethod.Get, "/things?type=awesome");
var response = httpClient.SendAsync(request).Result;

if (response.StatusCode != HttpStatusCode.OK || response.StatusCode != HttpStatusCode.OK)
{
throw new Exception("Wrong status code was returned");
}

Assert.Throws<InvalidOperationException>(() => _mockProviderService.VerifyInteractions());
}
}
}
2 changes: 2 additions & 0 deletions PactNet.Tests/PactNet.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Fakes\FakeHttpClient.cs" />
<Compile Include="IntegrationTests\IntegrationTestsMyApiPact.cs" />
<Compile Include="IntegrationTests\PactBuilderFailureIntegrationTests.cs" />
<Compile Include="IntegrationTests\Specification\MockHttpServiceSpecificationTests.cs">
<SubType>Code</SubType>
</Compile>
Expand Down
54 changes: 54 additions & 0 deletions PactNet.Tests/pacts/integrationtests-myapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"provider": {
"name": "MyApi"
},
"consumer": {
"name": "IntegrationTests"
},
"interactions": [
{
"description": "A GET request to retrieve a thing",
"request": {
"method": "get",
"path": "/things/1234"
},
"response": {
"status": 200
}
},
{
"description": "A GET request to retrieve things by type",
"request": {
"method": "get",
"path": "/things",
"query": "type=awesome",
"headers": {
"Accept": "application/json; charset=utf-8"
}
},
"response": {
"status": 200
}
},
{
"description": "A POST request to create a new thing",
"request": {
"method": "post",
"path": "/things",
"headers": {
"Content-Type": "application/json; charset=utf-8"
},
"body": {
"thingId": 1234,
"type": "Awesome"
}
},
"response": {
"status": 201
}
}
],
"metadata": {
"pactSpecificationVersion": "1.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ public void Compare(string expectedQuery, string actualQuery)
return;
}

var nomalisedExpectedQuery = ConvertUrlEncodingToUpperCase(expectedQuery);
var nomalisedActualQuery = ConvertUrlEncodingToUpperCase(actualQuery);
var normalisedExpectedQuery = ConvertUrlEncodingToUpperCase(expectedQuery);
var normalisedActualQuery = ConvertUrlEncodingToUpperCase(actualQuery);

_reporter.ReportInfo(String.Format("{0} has query set to {1}", _messagePrefix, nomalisedExpectedQuery));
_reporter.ReportInfo(String.Format("Actual query is {0}", nomalisedActualQuery));
_reporter.ReportInfo(String.Format("{0} has query set to {1}", _messagePrefix, normalisedExpectedQuery));

if (nomalisedExpectedQuery != nomalisedActualQuery)
if (normalisedExpectedQuery != normalisedActualQuery)
{
_reporter.ReportError(expected: nomalisedExpectedQuery, actual: nomalisedActualQuery);
_reporter.ReportError(expected: normalisedExpectedQuery, actual: normalisedActualQuery);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class ProviderServiceRequest
public string Query { get; set; }

[JsonProperty(PropertyName = "headers")]
public Dictionary<string, string> Headers { get; set; }
public IDictionary<string, string> Headers { get; set; }

[JsonProperty(PropertyName = "body")]
public dynamic Body { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ProviderServiceResponse
public int Status { get; set; }

[JsonProperty(PropertyName = "headers")]
public Dictionary<string, string> Headers { get; set; }
public IDictionary<string, string> Headers { get; set; }

[JsonProperty(PropertyName = "body")]
public dynamic Body { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Samples/EventApi/Consumer.Tests/ConsumerEventApiPact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ConsumerEventApiPact : IDisposable
public IPactBuilder PactBuilder { get; private set; }
public IMockProviderService MockProviderService { get; private set; }

public int MockServerPort { get { return 12345; } }
public int MockServerPort { get { return 1234; } }
public string MockProviderServiceBaseUri { get { return String.Format("http://localhost:{0}", MockServerPort); } }

public ConsumerEventApiPact()
Expand Down

0 comments on commit 32d3c6a

Please sign in to comment.