Example #1
0
        public async Task VerificationForMessagePactShouldFailWhenWrongMessageIsReturned()
        {
            var recipeRepository     = new FakeRecipeRepository();
            var providerStateHandler = new ProviderStateHandler(recipeRepository);

            var config = new PactVerifierConfig
            {
                ProviderStateHandler = providerStateHandler.Handle,
                MessageProducer      = (d) => null
            };

            var pactVerifier = new PactVerifier(config);

            var buildDirectory = AppContext.BaseDirectory;
            var pactDir        = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");

            try
            {
                await pactVerifier.VerifyPactAsync(pactDir + "messageConsumer-messageProvider.json");
            }
            catch (PactVerificationException e)
            {
                Assert.IsTrue(e.Message.Contains("Expected body or contents to be present, but was not"));
                throw;
            }
        }
Example #2
0
        public async Task ShouldVerifyPact()
        {
            var baseUrl = "http://localhost:9494";

            var recipeRepository     = new FakeRecipeRepository();
            var providerStateHandler = new ProviderStateHandler(recipeRepository);

            var pactVerifier = new PactVerifier(new PactVerifierConfig {
                ProviderBaseUrl = baseUrl, ProviderStateHandler = providerStateHandler.Handle
            });

            var cts = new CancellationTokenSource();

            var hostTask = WebHost.CreateDefaultBuilder()
                           .UseKestrel()
                           .UseUrls(baseUrl)
                           .ConfigureServices(services =>
            {
                services.Add(new ServiceDescriptor(typeof(IRecipeRepository), recipeRepository));
            })
                           .UseStartup <TestStartup>()
                           .Build().RunAsync(cts.Token);

            var buildDirectory = AppContext.BaseDirectory;
            var pactDir        = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            await pactVerifier.VerifyPactAsync(pactDir + "recipe-consumer-recipe-service.json");

            cts.Cancel();
            await hostTask;
        }
Example #3
0
        public async Task ShouldPublishVerificationResults()
        {
            var buildDirectory               = AppContext.BaseDirectory;
            var pactDir                      = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            var pactFileToReturn             = File.ReadAllText(pactDir + "messageConsumer-messageProvider.json");
            var fakePactBrokerMessageHandler = new FakePactBrokerMessageHandler
            {
                ObjectToReturn = JsonConvert.DeserializeObject(pactFileToReturn)
            };

            var messageSender = new MessageSender();

            var config = new PactVerifierConfig
            {
                MessageProducer            = messageSender.Send,
                ProviderVersion            = "1.0",
                PublishVerificationResults = true,
                PactBrokerClient           = new HttpClient(fakePactBrokerMessageHandler)
                {
                    BaseAddress = new Uri("http://localhost:9292")
                }
            };

            var pactVerifier = new PactVerifier(config);

            await pactVerifier.VerifyPactAsync("pacts/provider/messageProvider/consumer/messageConsumer/latest");

            var sentVerificationResults = JsonConvert.DeserializeObject <VerificationResults>(fakePactBrokerMessageHandler.SentRequestContents.First().Value);

            Assert.IsTrue(sentVerificationResults.Success);
        }
Example #4
0
        public async Task ShouldVerifyMessagePactWhenSerializedJsonIsReturned()
        {
            var messageSender = new MessageSender();

            var config = new PactVerifierConfig
            {
                MessageProducer = (p, d) => JsonConvert.SerializeObject(messageSender.Send(p, d))
            };

            var pactVerifier = new PactVerifier(config);

            var buildDirectory = AppContext.BaseDirectory;
            var pactDir        = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            await pactVerifier.VerifyPactAsync(pactDir + "messageConsumer-messageProvider.json");
        }
Example #5
0
        public async Task ShouldVerifyMessagePact()
        {
            var messageSender = new MessageSender();

            var config = new PactVerifierConfig
            {
                MessageProducer = messageSender.Send
            };

            var pactVerifier = new PactVerifier(config);

            var buildDirectory = AppContext.BaseDirectory;
            var pactDir        = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            await pactVerifier.VerifyPactAsync(pactDir + "messageConsumer-messageProvider.json");
        }
Example #6
0
        public async Task ShouldPublishVerificationResults()
        {
            var buildDirectory               = AppContext.BaseDirectory;
            var pactDir                      = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            var pactFileToReturn             = File.ReadAllText(pactDir + "messageConsumer-messageProvider.json");
            var fakePactBrokerMessageHandler = new FakePactBrokerMessageHandler();

            fakePactBrokerMessageHandler
            .Configure(HttpMethod.Get,
                       "http://localhost:9292/pacts/provider/messageProvider/consumer/messageConsumer/latest")
            .RespondsWith(HttpStatusCode.Created).Returns(JsonConvert.DeserializeObject(pactFileToReturn));
            fakePactBrokerMessageHandler
            .Configure(HttpMethod.Post, "http://localhost:9292/publish/verification/results/path")
            .RespondsWith(HttpStatusCode.Created);

            var recipeRepository     = new FakeRecipeRepository();
            var providerStateHandler = new ProviderStateHandler(recipeRepository);
            var messageSender        = new MessageSender(recipeRepository);

            var config = new PactVerifierConfig
            {
                ProviderStateHandler       = providerStateHandler.Handle,
                MessageProducer            = messageSender.Send,
                ProviderVersion            = "1.0",
                PublishVerificationResults = true,
                PactBrokerClient           = new HttpClient(fakePactBrokerMessageHandler)
                {
                    BaseAddress = new Uri("http://localhost:9292")
                }
            };

            var pactVerifier = new PactVerifier(config);

            await pactVerifier.VerifyPactAsync("pacts/provider/messageProvider/consumer/messageConsumer/latest");

            var sentVerificationResults = JsonConvert.DeserializeObject <VerificationResults>(
                fakePactBrokerMessageHandler.GetStatus(HttpMethod.Post,
                                                       "http://localhost:9292/publish/verification/results/path")
                .SentRequestContents.First().Value);

            Assert.IsTrue(sentVerificationResults.Success);
            Assert.AreEqual(1, sentVerificationResults.TestResults.Summary.TestCount);
            Assert.AreEqual(0, sentVerificationResults.TestResults.Summary.FailureCount);
        }
        public void VerifyPact(
            string providerName,
            string consumerName,
            Action <ProviderState> providerState,
            Action <IServiceCollection> configureDi = null,
            IConfiguration configuration            = null,
            string pactDirectory = "../../../../pacts"
            )
        {
            var baseUrl = $"http://localhost:{_servicePort}";

            var serviceBuilder = new HostBuilder()
                                 .ConfigureWebHostDefaults(c =>
            {
                c.UseStartup <TStartup>();
                c.UseEnvironment("Development");

                if (configureDi != null)
                {
                    c.ConfigureTestServices(configureDi);
                }

                c.UseUrls(baseUrl);

                if (configuration != null)
                {
                    c.UseConfiguration(configuration);
                }

                c.ConfigureLogging(l => l.AddXUnit(_testOutput));
            });

            using (serviceBuilder.StartAsync().GetAwaiter().GetResult())
            {
                var pactFilePath = $"{pactDirectory}/{consumerName}-{providerName}.json";
                var pactVerifier = new PactVerifier(new PactVerifierConfig {
                    ProviderBaseUrl = baseUrl, ProviderStateHandler = providerState
                });

                pactVerifier.VerifyPactAsync(pactFilePath).GetAwaiter().GetResult();
            }
        }
Example #8
0
        public async Task ShouldVerifyPact()
        {
            var baseUrl = "http://localhost:9494";

            var pactVerifier = new PactVerifier(new PactVerifierConfig {
                ProviderBaseUrl = baseUrl
            });

            var cts = new CancellationTokenSource();

            var hostTask = WebHost.CreateDefaultBuilder()
                           .UseKestrel()
                           .UseUrls(baseUrl)
                           .UseStartup <TestStartup>()
                           .Build().RunAsync(cts.Token);

            var buildDirectory = AppContext.BaseDirectory;
            var pactDir        = Path.GetFullPath($"{buildDirectory}{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}..{Path.DirectorySeparatorChar}pacts{Path.DirectorySeparatorChar}");
            await pactVerifier.VerifyPactAsync(pactDir + "recipe-consumer-recipe-service.json");

            cts.Cancel();
            await hostTask;
        }