When I am running the .NET 8 Provider for verifying my contract, the server seems to just stop during the verify. I'm not getting any useful logs too so I can't tell why it's just stopping. I can, however, get a test to pass when I use a delay and hit the endpoint of the server through Postman during this delay and before the .Verify()
is hit.
public class ContractTests : IClassFixture<ApiFixture>
{
private readonly ITestOutputHelper _output;
private readonly ApiFixture _fixture;
private readonly Uri _pactBrokerBaseUri = new Uri("<pactBrokerUri>");
private readonly string _providerName = "provider";
private readonly string _consumerName = "consumer";
public ContractTests(ITestOutputHelper outputHelper, ApiFixture fixture)
{
_fixture = fixture;
_output = outputHelper;
}
[Fact]
public async Task EnsureProviderApiHonoursPactWithConsumer()
{
// Arrange
var config = new PactVerifierConfig
{
Outputters = new List<Pact.IOutput>
{
new XunitOutput(_output)
},
LogLevel = PactLogLevel.Debug,
};
// Act
IPactVerifier pactVerifier = new PactVerifier(_providerName, config);
var _pactVerifier = pactVerifier
.WithHttpEndpoint(_fixture.ServerUri)
.WithUriSource(
new Uri($"{_pactBrokerBaseUri}pacts/provider/{_providerName}/consumer/{_consumerName}/latest"));
// This is in place to allow a call to the server from Postman
await Task.Delay(TimeSpan.FromSeconds(30));
// Assert
_pactVerifier.Verify();
}
}
This test fails unless I hit the same endpoint using Postman during the delay time.
public class ApiFixture : IDisposable
{
private readonly IHost _server;
public Uri ServerUri { get; }
public ApiFixture()
{
ServerUri = new Uri("https://localhost:5066");
_server = CreateHostBuilder(ServerUri)
.Build();
_server.Start();
}
public static IHostBuilder CreateHostBuilder(Uri serverUri) =>
Host.CreateDefaultBuilder()
.UseSerilog()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseUrls(serverUri.ToString());
webBuilder
.UseStartup<TestStartup>();
});
public void Dispose()
{
_server.Dispose();
}
}
This server does run on the correct port and can be hit from Postman during the timeout but if I don't hit Postman it just stops during the .Verify()
The only error I get is:
Request Failed - error sending request for url (https://localhost:5066/api/v1/searchEndpoint)
and the logs just end with:
DEBUG ThreadId(02) verify_interaction{interaction="a Search request"}: hyper_util::client::legacy::connect::http: connecting to [::1]:5066
DEBUG ThreadId(02) verify_interaction{interaction="a Search request"}: hyper_util::client::legacy::connect::http: connected to [::1]:5066
WARN ThreadId(02) pact_matching::metrics:
In this case the issue was that the timeout was not long enough on the _pactVerifier
. By default, this time is set to 5 seconds PactNet and this can be overridden using
_pactVerifier.WithRequestTimeout(TimeSpan.FromSeconds(20))
to increase the verifier timeout to 20 seconds in this case.