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 integration test for the pactbuilder failure scenarios
- Loading branch information
1 parent
9a512cb
commit 32d3c6a
Showing
8 changed files
with
206 additions
and
9 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
PactNet.Tests/IntegrationTests/IntegrationTestsMyApiPact.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,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
114
PactNet.Tests/IntegrationTests/PactBuilderFailureIntegrationTests.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,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()); | ||
} | ||
} | ||
} |
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
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" | ||
} | ||
} |
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