public void EnsureProviderApiHonoursPactWithConsumer() { // Arrange var config = new PactVerifierConfig { // NOTE: We default to using a ConsoleOutput, // however xUnit 2 does not capture the console output, // so a custom outputter is required. Outputters = new List <IOutput> { new ConsoleOutput() }, // Output verbose verification logs to the test output Verbose = true, PublishVerificationResults = true, ProviderVersion = System.Environment.GetEnvironmentVariable("TRAVIS_COMMIT") }; IPactVerifier pactVerifier = new PactVerifier(config); string pactUrl = System.Environment.GetEnvironmentVariable("PACT_URL"); pactVerifier .ProviderState($"{_pactServiceUri}/provider-states") .ServiceProvider("pactflow-example-provider-dotnet", _providerUri); if (pactUrl != "" && pactUrl != null) { // Webhook path - verify the specific pact pactVerifier.PactUri(pactUrl, new PactUriOptions(System.Environment.GetEnvironmentVariable("PACT_BROKER_TOKEN"))); } else { // Standard verification path - run the pactVerifier.PactBroker(System.Environment.GetEnvironmentVariable("PACT_BROKER_BASE_URL"), uriOptions: new PactUriOptions(System.Environment.GetEnvironmentVariable("PACT_BROKER_TOKEN")), consumerVersionTags: new List <string> { "master", "prod" }); } // Act / Assert pactVerifier.Verify(); }
private PactnetVerificationResult VerifyUsingPactNet(string pactFile, bool publishResultViaBroker, Models.Pact runningPact) { var result = new PactnetVerificationResult(); var logger = new StringAppendOutput(); var verifierConfig = new PactVerifierConfig { Outputters = new IOutput[] { logger }, PublishVerificationResults = publishResultViaBroker, ProviderVersion = "1" }; var verifier = new PactVerifier(verifierConfig); verifier.ServiceProvider(runningPact.Provider?.Name, _serviceUri); verifier.PactUri(pactFile); try { verifier.Verify(); result.Success = true; } catch (Exception e) { result.Success = false; result.Exception = e; } result.Logs = logger.Log; return(result); }
public void PactflowWebhook_EnsureProviderApiHonorsContractsWithPublishedPact() { // arrange // setup mocked Foo API const string guidRegex = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}"; _fooApiMock.ClearInteractions(); _fooApiMock .Given("There is a foo with some-guid") .UponReceiving("A GET request to retrieve foo") .With(new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/foos", Query = Match.Regex("name=00000000-0000-0000-0000-000000000000", $"^name={guidRegex}$"), Headers = new Dictionary <string, object> { { "Accept", "application/json" } } }) .WillRespondWith(new ProviderServiceResponse { Status = 200, Headers = new Dictionary <string, object> { { "Content-Type", "application/json; charset=utf-8" } }, Body = new[] { new { id = "some id", name = "some name", description = "some description" } } }); // get environment variables DotNetEnv.Env.TraversePath().Load(); bool isRunningInPipeline = "true".Equals(Environment.GetEnvironmentVariable("CI")); string pactBrokerBaseUrl = Environment.GetEnvironmentVariable("PACT_BROKER_BASE_URL"); string pactBrokerApiToken = Environment.GetEnvironmentVariable("PACT_BROKER_API_TOKEN"); string pactProviderTag = Environment.GetEnvironmentVariable("PACT_PROVIDER_TAG"); string pactUrl = Environment.GetEnvironmentVariable("PACT_URL"); string gitCommitShortSha = Environment.GetEnvironmentVariable("GIT_COMMIT_SHORT_SHA"); var pactVerifier = new PactVerifier(new PactVerifierConfig { ProviderVersion = gitCommitShortSha, PublishVerificationResults = isRunningInPipeline, Outputters = new[] { new XUnitOutput(_outputHelper) }, Verbose = true }); PactUriOptions pactUriOptions = new PactUriOptions().SetBearerAuthentication(pactBrokerApiToken); pactVerifier .ProviderState($"{TestServiceBaseUri}/provider-states") .ServiceProvider("Provider API", TestServiceBaseUri); // pactUrl is provided to pipeline triggered by pactflow webhook indicating what specific pact should be verified // if value is missing then default verification is performed for all consumers with tags: main, dev, uat, prod bool isVerifyingSpecificPact = !string.IsNullOrWhiteSpace(pactUrl); if (isVerifyingSpecificPact) { pactVerifier.PactUri(pactUrl, pactUriOptions); } else { var consumerVersionSelectors = new List <VersionTagSelector> { new VersionTagSelector("refs/heads/main", latest: true), new VersionTagSelector("dev", latest: true), new VersionTagSelector("uat", latest: true), new VersionTagSelector("prod", latest: true) }; pactVerifier.PactBroker(pactBrokerBaseUrl, pactUriOptions, true, providerVersionTags: new[] { pactProviderTag }, consumerVersionSelectors: consumerVersionSelectors); } // act // assert pactVerifier.Verify(); }