public void Handle_WithAPostRequestToPactAndInteractionsHaveBeenRegistered_ReturnsResponseWithPactFileJson()
        {
            var pactDetails = new PactDetails
            {
                Consumer = new Party {
                    Name = "Consumer"
                },
                Provider = new Party {
                    Name = "Provider"
                }
            };

            var interactions = new List <ProviderServiceInteraction>
            {
                new ProviderServiceInteraction
                {
                    Description = "My description",
                    Request     = new ProviderServiceRequest
                    {
                        Method = HttpVerb.Get,
                        Path   = "/test"
                    },
                    Response = new ProviderServiceResponse
                    {
                        Status = (int)HttpStatusCode.NoContent
                    }
                }
            };

            var pactFile = new ProviderServicePactFile
            {
                Provider     = pactDetails.Provider,
                Consumer     = pactDetails.Consumer,
                Interactions = interactions
            };

            var pactFileJson    = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings);
            var pactDetailsJson = JsonConvert.SerializeObject(pactDetails, JsonConfig.ApiSerializerSettings);

            var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(pactDetailsJson));

            var requestStream = new RequestStream(jsonStream, jsonStream.Length, true);
            var context       = new NancyContext
            {
                Request = new Request("POST", new Url("http://localhost/pact"), requestStream)
            };

            var handler = GetSubject();

            _mockProviderRepository.Interactions.Returns(interactions);

            var response = handler.Handle(context);

            Assert.Equal("application/json", response.Headers["Content-Type"]);
            Assert.Equal(pactFileJson, ReadResponseContent(response.Contents));
        }
        public void Handle_WithAPostRequestToPactAndInteractionsHaveBeenRegistered_NewPactFileIsSavedWithInteractions()
        {
            var pactDetails = new PactDetails
            {
                Consumer = new Party {
                    Name = "Consumer"
                },
                Provider = new Party {
                    Name = "Provider"
                }
            };

            var interactions = new List <ProviderServiceInteraction>
            {
                new ProviderServiceInteraction
                {
                    Description = "My description",
                    Request     = new ProviderServiceRequest
                    {
                        Method = HttpVerb.Get,
                        Path   = "/test"
                    },
                    Response = new ProviderServiceResponse
                    {
                        Status = (int)HttpStatusCode.NoContent
                    }
                }
            };

            var pactFile = new ProviderServicePactFile
            {
                Provider     = pactDetails.Provider,
                Consumer     = pactDetails.Consumer,
                Interactions = interactions
            };

            var pactFileJson    = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings);
            var pactDetailsJson = JsonConvert.SerializeObject(pactDetails, JsonConfig.ApiSerializerSettings);

            var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(pactDetailsJson));

            var requestStream = new RequestStream(jsonStream, jsonStream.Length, true);
            var context       = new NancyContext
            {
                Request = new Request("POST", new Url("http://localhost/pact"), requestStream)
            };

            var handler = GetSubject();

            _mockProviderRepository.Interactions.Returns(interactions);

            handler.Handle(context);

            _mockFileSystem.File.Received(1).WriteAllText(Path.Combine(Constants.DefaultPactFileDirectory, pactDetails.GeneratePactFileName()), pactFileJson);
        }
Ejemplo n.º 3
0
        GeneratePactFileName_WhenConsumerNameAndProviderNameHaveNotBeenSet_ReturnsFileNameWithNoConsumerAndProviderNameAndDoesNotThrow()
        {
            var details = new PactDetails
            {
                Consumer = new Pacticipant(),
                Provider = new Pacticipant()
            };

            var fileName = details.GeneratePactFileName();

            Assert.Equal("-.json", fileName);
        }
Ejemplo n.º 4
0
        public void GeneratePactFileName_WhenConsumerNameAndProviderNameHaveBeenSet_ReturnsFileName()
        {
            var details = new PactDetails
            {
                Consumer = new Pacticipant {
                    Name = "My Consumer"
                },
                Provider = new Pacticipant {
                    Name = "My Provider"
                }
            };

            var fileName = details.GeneratePactFileName();

            Assert.Equal("my_consumer-my_provider.json", fileName);
        }
Ejemplo n.º 5
0
        private void PersistPactFile()
        {
            if (String.IsNullOrEmpty(ConsumerName))
            {
                throw new InvalidOperationException(
                          "ConsumerName has not been set, please supply a consumer name using the ServiceConsumer method.");
            }

            if (String.IsNullOrEmpty(ProviderName))
            {
                throw new InvalidOperationException(
                          "ProviderName has not been set, please supply a provider name using the HasPactWith method.");
            }

            var pactDetails = new PactDetails
            {
                Provider = new Pacticipant {
                    Name = ProviderName
                },
                Consumer = new Pacticipant {
                    Name = ConsumerName
                }
            };

            var pactFilePath = Path.Combine(pactConfig.PactDir, pactDetails.GeneratePactFileName());

            var pactFile = new MessagePactFile
            {
                Provider = pactDetails.Provider,
                Consumer = pactDetails.Consumer,
                Messages = mockMq.Interactions()
            };

            var pactFileJson = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings);

            try
            {
                File.WriteAllText(pactFilePath, pactFileJson);
            }
            catch (DirectoryNotFoundException)
            {
                Directory.CreateDirectory(pactConfig.PactDir);
                File.WriteAllText(pactFilePath, pactFileJson);
            }
        }
        private void AssertPactTagRequest(
            HttpRequestMessage httpRequest,
            string httpRequestContent,
            string brokerBaseUri,
            PactDetails expectedPactDetails,
            string expectedConsumerVersion,
            string expectedTag,
            PactUriOptions expectedPactUriOptions = null)
        {
            if (expectedPactUriOptions != null)
            {
                Assert.Equal(expectedPactUriOptions.AuthorizationScheme, httpRequest.Headers.Authorization.Scheme);
                Assert.Equal(expectedPactUriOptions.AuthorizationValue, httpRequest.Headers.Authorization.Parameter);
            }

            string expectedUri = $"{brokerBaseUri}/pacticipants/{Uri.EscapeDataString(expectedPactDetails.Consumer.Name)}/versions/{expectedConsumerVersion}/tags/{Uri.EscapeDataString(expectedTag)}";

            Assert.Equal(HttpMethod.Put, httpRequest.Method);
            Assert.Equal(expectedUri, httpRequest.RequestUri.OriginalString);
            Assert.Equal(string.Empty, httpRequestContent);
            Assert.Equal("application/json", httpRequest.Content.Headers.ContentType.MediaType);
            Assert.Equal("utf-8", httpRequest.Content.Headers.ContentType.CharSet);
        }
Ejemplo n.º 7
0
        private void PersistPactFile()
        {
            if (String.IsNullOrEmpty(ConsumerName))
            {
                throw new InvalidOperationException("ConsumerName has not been set, please supply a consumer name using the ServiceConsumer method.");
            }

            if (String.IsNullOrEmpty(ProviderName))
            {
                throw new InvalidOperationException("ProviderName has not been set, please supply a provider name using the HasPactWith method.");
            }

            var pactDetails = new PactDetails
            {
                Provider = new Pacticipant {
                    Name = ProviderName
                },
                Consumer = new Pacticipant {
                    Name = ConsumerName
                }
            };

            _mockProviderService.SendAdminHttpRequest(HttpVerb.Post, Constants.PactPath, pactDetails);
        }
Ejemplo n.º 8
0
        private bool ValidPactTagRequest(
            HttpRequestMessage httpRequest,
            string httpRequestContent,
            string brokerBaseUri,
            PactDetails expectedPactDetails,
            string expectedConsumerVersion,
            string expectedTag,
            PactUriOptions expectedPactUriOptions = null)
        {
            var authHeadersCorrect = false;

            if (expectedPactUriOptions != null)
            {
                if (httpRequest.Headers.Authorization.Scheme == expectedPactUriOptions.AuthorizationScheme &&
                    httpRequest.Headers.Authorization.Parameter == expectedPactUriOptions.AuthorizationValue)
                {
                    authHeadersCorrect = true;
                }
            }
            else if (httpRequest.Headers.Authorization == null)
            {
                authHeadersCorrect = true;
            }

            if (httpRequest.Method == HttpMethod.Put &&
                httpRequest.RequestUri.OriginalString == $"{brokerBaseUri}/pacticipants/{Uri.EscapeDataString(expectedPactDetails.Consumer.Name)}/versions/{expectedConsumerVersion}/tags/{Uri.EscapeDataString(expectedTag)}" &&
                httpRequestContent == "" &&
                httpRequest.Content.Headers.ContentType.MediaType == "application/json" &&
                httpRequest.Content.Headers.ContentType.CharSet == "utf-8" &&
                authHeadersCorrect)
            {
                return(true);
            }

            return(false);
        }
        public void Handle_WithAPostRequestToPactAndNoInteractionsHaveBeenRegistered_NewPactFileIsSavedWithNoInteractions()
        {
            var pactDetails = new PactDetails
            {
                Consumer = new Party {
                    Name = "Consumer"
                },
                Provider = new Party {
                    Name = "Provider"
                }
            };

            var pactFile = new ProviderServicePactFile
            {
                Provider     = pactDetails.Provider,
                Consumer     = pactDetails.Consumer,
                Interactions = new ProviderServiceInteraction[0]
            };

            var pactFileJson    = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings);
            var pactDetailsJson = JsonConvert.SerializeObject(pactDetails, JsonConfig.ApiSerializerSettings);

            var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(pactDetailsJson));

            var requestStream = new RequestStream(jsonStream, jsonStream.Length, true);
            var context       = new NancyContext
            {
                Request = new Request("POST", new Url("http://localhost/pact"), requestStream)
            };

            var handler = GetSubject();

            handler.Handle(context);

            _mockFileSystem.File.Received(1).WriteAllText(Path.Combine(Constants.DefaultPactFileDirectory, pactDetails.GeneratePactFileName()), pactFileJson);
        }
        public void Handle_WithAPostRequestToPactAndDirectoryDoesNotExist_DirectoryIsCreatedAndNewPactFileIsSavedWithInteractions()
        {
            var pactDetails = new PactDetails
            {
                Consumer = new Pacticipant {
                    Name = "Consumer"
                },
                Provider = new Pacticipant {
                    Name = "Provider"
                }
            };

            var interactions = new List <ProviderServiceInteraction>
            {
                new ProviderServiceInteraction
                {
                    Description = "My description",
                    Request     = new ProviderServiceRequest
                    {
                        Method = HttpVerb.Get,
                        Path   = "/test"
                    },
                    Response = new ProviderServiceResponse
                    {
                        Status = (int)HttpStatusCode.NoContent
                    }
                }
            };

            var pactFile = new ProviderServicePactFile
            {
                Provider     = pactDetails.Provider,
                Consumer     = pactDetails.Consumer,
                Interactions = interactions
            };

            var pactFileJson    = JsonConvert.SerializeObject(pactFile, JsonConfig.PactFileSerializerSettings);
            var pactDetailsJson = JsonConvert.SerializeObject(pactDetails, JsonConfig.ApiSerializerSettings);

            var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(pactDetailsJson));

            var requestStream = new RequestStream(jsonStream, jsonStream.Length, true);
            var context       = new NancyContext
            {
                Request = new Request("POST", new Url("http://localhost/pact"), requestStream)
            };

            var filePath = Path.Combine(Constants.DefaultPactDir, pactDetails.GeneratePactFileName());

            var handler = GetSubject();

            _mockProviderRepository.Interactions.Returns(interactions);

            var writeAllTextCount = 0;

            _mockFileSystem.File
            .When(x => x.WriteAllText(filePath, pactFileJson))
            .Do(x =>
            {
                writeAllTextCount++;
                if (writeAllTextCount == 1)
                {
                    throw new DirectoryNotFoundException("It doesn't exist");
                }
            });

            handler.Handle(context);

            _mockFileSystem.File.Received(2).WriteAllText(filePath, pactFileJson);
        }