GitHub
08/08/2023, 9:11 PMimage▾
[Fact]
public async Task Test01() => await Test();
private static async Task Test()
{
var api1PactBuilder = CreatePactBuilder("API-1", 9091);
AddGetRequest(api1PactBuilder, "/api-1-endpoint-1");
AddGetRequest(api1PactBuilder, "/api-1-endpoint-2");
var api2PactBuilder = CreatePactBuilder("API-2", 9092);
AddGetRequest(api2PactBuilder, "/api-2-endpoint-1");
await api1PactBuilder.VerifyAsync(async _ =>
{
await api2PactBuilder.VerifyAsync(async _ =>
{
var request = new HttpRequestMessage(HttpMethod.Get, new Uri("/slow-tests", UriKind.RelativeOrAbsolute));
var httpClient = new WebApplicationFactory<Program>().CreateClient();
await httpClient.SendAsync(request);
});
});
}
private static void AddGetRequest(IPactBuilderV3 pactBuilder, string url)
{
pactBuilder
.UponReceiving("Test")
.Given("Test")
.WithRequest(HttpMethod.Get, url)
.WillRespond();
}
private static IPactBuilderV3 CreatePactBuilder(string provider, int port)
{
var pact = Pact.V3("Test", provider);
return pact.WithHttpInteractions(port);
}
API source code:
public class SlowTestsController : ControllerBase
{
[HttpGet()]
public async Task<IActionResult> Get()
{
var httpClient = new HttpClient();
var api1Endpoint2Task = HttpRequest(httpClient, "<http://localhost:9091/api-1-endpoint-1>");
var api1Endpoint3Task = HttpRequest(httpClient, "<http://localhost:9091/api-1-endpoint-2>");
await Task.WhenAll(api1Endpoint2Task, api1Endpoint3Task);
await HttpRequest(httpClient, "<http://localhost:9092/api-2-endpoint-1>");
return Ok();
}
public async Task HttpRequest(HttpClient httpClient, string url)
{
using var request = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
await httpClient.SendAsync(request);
}
}
pact-foundation/pact-net