public void With_WithRequest_SetsRequest()
        {
            var description = "My description";
            var request     = new ProviderServiceRequest
            {
                Method = HttpVerb.Head,
                Path   = "/tester/testing/1"
            };
            var response = new ProviderServiceResponse
            {
                Status = (int)HttpStatusCode.ProxyAuthenticationRequired
            };

            var expectedInteraction = new ProviderServiceInteraction
            {
                Description = description,
                Request     = request,
                Response    = response
            };
            var expectedInteractionJson = expectedInteraction.AsJsonString();

            var mockService = GetSubject();

            mockService.Start();

            mockService.UponReceiving(description)
            .With(request)
            .WillRespondWith(response);

            var actualInteractionJson = _fakeHttpMessageHandler.RequestContentRecieved.Single();

            Assert.Equal(expectedInteractionJson, actualInteractionJson);
        }
예제 #2
0
        public void AddInteraction(ProviderServiceInteraction interaction)
        {
            if (interaction == null)
            {
                throw new ArgumentNullException("interaction");
            }

            //You cannot have any duplicate interaction defined in a test scope
            if (_testScopedInteractions.Any(x => x.Description == interaction.Description &&
                                            x.ProviderState == interaction.ProviderState))
            {
                throw new PactFailureException(String.Format("An interaction already exists with the description '{0}' and provider state '{1}' in this test. Please supply a different description or provider state.", interaction.Description, interaction.ProviderState));
            }

            //From a Pact specification perspective, I should de-dupe any interactions that have been registered by another test as long as they match exactly!
            var duplicateInteractions = _interactions.Where(x => x.Description == interaction.Description && x.ProviderState == interaction.ProviderState).ToList();

            if (!duplicateInteractions.Any())
            {
                _interactions.Add(interaction);
            }
            else if (duplicateInteractions.Any(di => di.AsJsonString() != interaction.AsJsonString()))
            {
                //If the interaction description and provider state match, however anything else in the interaction is different, throw
                throw new PactFailureException(String.Format("An interaction registered by another test already exists with the description '{0}' and provider state '{1}', however the interaction does not match exactly. Please supply a different description or provider state. Alternatively align this interaction to match the duplicate exactly.", interaction.Description, interaction.ProviderState));
            }

            _testScopedInteractions.Add(interaction);
        }
        public void Handle_WithALowercasedPostRequestToInteractions_ReturnsOkResponse()
        {
            var interaction = new ProviderServiceInteraction
            {
                Description = "My description",
                Request     = new ProviderServiceRequest
                {
                    Method = HttpVerb.Get,
                    Path   = "/test"
                },
                Response = new ProviderServiceResponse
                {
                    Status = (int)HttpStatusCode.NoContent
                }
            };
            var interactionJson = interaction.AsJsonString();

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

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

            var handler = GetSubject();

            var response = handler.Handle(context);

            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
        public void Handle_WithAPostRequestToInteractions_AddInteractionIsCalledOnTheMockProviderRepository()
        {
            var interaction = new ProviderServiceInteraction
            {
                Description = "My description",
                Request     = new ProviderServiceRequest
                {
                    Method = HttpVerb.Get,
                    Path   = "/test"
                },
                Response = new ProviderServiceResponse
                {
                    Status = (int)HttpStatusCode.NoContent
                }
            };
            var interactionJson = interaction.AsJsonString();

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

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

            var handler = GetSubject();

            handler.Handle(context);

            _mockProviderRepository.Received(1).AddInteraction(Arg.Is <ProviderServiceInteraction>(x => x.AsJsonString() == interactionJson));
        }
        public void WillRespondWith_WithValidInteraction_PerformsAdminInteractionsPostRequestWithInteraction()
        {
            var providerState = "My provider state";
            var description   = "My description";
            var request       = new ProviderServiceRequest
            {
                Method = HttpVerb.Head,
                Path   = "/tester/testing/1"
            };
            var response = new ProviderServiceResponse
            {
                Status = (int)HttpStatusCode.ProxyAuthenticationRequired
            };

            var expectedInteraction = new ProviderServiceInteraction
            {
                ProviderState = providerState,
                Description   = description,
                Request       = request,
                Response      = response
            };
            var expectedInteractionJson = expectedInteraction.AsJsonString();

            var mockService = GetSubject();

            mockService.Start();

            mockService
            .Given(providerState)
            .UponReceiving(description)
            .With(request)
            .WillRespondWith(response);

            var actualRequest         = _fakeHttpMessageHandler.RequestsRecieved.Single();
            var actualInteractionJson = _fakeHttpMessageHandler.RequestContentRecieved.Single();

            Assert.Equal(HttpMethod.Post, actualRequest.Method);
            Assert.Equal("http://localhost:1234/interactions", actualRequest.RequestUri.OriginalString);
            Assert.True(actualRequest.Headers.Contains(Constants.AdministrativeRequestHeaderKey));

            Assert.Equal(expectedInteractionJson, actualInteractionJson);
        }
        public void ToString_WhenCalled_ReturnsJsonRepresentation()
        {
            const string expectedInteractionJson = "{\"description\":\"My description\",\"provider_state\":\"My provider state\",\"request\":{\"method\":\"delete\",\"path\":\"/tester\",\"query\":\"test=2\",\"headers\":{\"Accept\":\"application/json\"},\"body\":{\"test\":\"hello\"}},\"response\":{\"status\":407,\"headers\":{\"Content-Type\":\"application/json\"},\"body\":{\"yep\":\"it worked\"}}}";
            var          interaction             = new ProviderServiceInteraction
            {
                Request = new ProviderServiceRequest
                {
                    Method = HttpVerb.Delete,
                    Body   = new
                    {
                        test = "hello"
                    },
                    Headers = new Dictionary <string, object>
                    {
                        { "Accept", "application/json" }
                    },
                    Path  = "/tester",
                    Query = "test=2"
                },
                Response = new ProviderServiceResponse
                {
                    Status = (int)HttpStatusCode.ProxyAuthenticationRequired,
                    Body   = new
                    {
                        yep = "it worked"
                    },
                    Headers = new Dictionary <string, object>
                    {
                        { "Content-Type", "application/json" }
                    }
                },
                Description   = "My description",
                ProviderState = "My provider state",
            };

            var actualInteractionJson = interaction.AsJsonString();

            Assert.Equal(expectedInteractionJson, actualInteractionJson);
        }