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 IncrementUsage_WhenCalled_SetsUsageCountToOne() { var interaction = new ProviderServiceInteraction(); interaction.IncrementUsage(); Assert.Equal(1, interaction.UsageCount); }
public MissingInteractionComparisonFailure(ProviderServiceInteraction interaction) { var requestMethod = interaction.Request != null ? interaction.Request.Method.ToString().ToUpperInvariant() : "No Method"; var requestPath = interaction.Request != null ? interaction.Request.Path : "No Path"; RequestDescription = String.Format("{0} {1}", requestMethod, requestPath); Result = String.Format( "The interaction with description '{0}' and provider state '{1}', was not used by the test. Missing request {2}.", interaction.Description, interaction.ProviderState, RequestDescription); }
public void Handle_WithAPostRequestToInteractions_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 Handle_WithAGetRequestToInteractionsVerificationAndCorrectlyMatchedHandledRequest_ReturnsOkResponse() { var context = new NancyContext { Request = new Request("GET", "/interactions/verification", "http") }; var interaction = new ProviderServiceInteraction(); var handler = GetSubject(); _mockProviderRepository.TestScopedInteractions.Returns(new List<ProviderServiceInteraction> { interaction }); _mockProviderRepository.HandledRequests.Returns(new List<HandledRequest> { new HandledRequest(new ProviderServiceRequest(), interaction) }); var response = handler.Handle(context); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public void Handle_WithAGetRequestToInteractionsVerificationAndAnInteractionWasSentButNotRegisteredByTheTest_ThrowsPactFailureExceptionWithTheCorrectMessageAndLogsTheUnexpectedRequest() { const string failure = "An unexpected request POST /tester was seen by the mock provider service."; var context = new NancyContext { Request = new Request("GET", "/interactions/verification", "http") }; var handler = GetSubject(); var handledRequest = new ProviderServiceRequest(); var handledInteraction = new ProviderServiceInteraction { Request = handledRequest }; var unExpectedRequest = new ProviderServiceRequest { Method = HttpVerb.Post, Path = "/tester" }; _mockProviderRepository.TestScopedInteractions .Returns(new List<ProviderServiceInteraction> { handledInteraction }); _mockProviderRepository.HandledRequests .Returns(new List<HandledRequest> { new HandledRequest(handledRequest, handledInteraction), new HandledRequest(unExpectedRequest, null) }); var exception = Assert.Throws<PactFailureException>(() => handler.Handle(context)); _mockLog.Received().Log(LogLevel.Error, Arg.Any<Func<string>>(), null, Arg.Any<object[]>()); Assert.Equal(failure, exception.Message); }
private ComparisonResult ValidateInteraction(ProviderServiceInteraction interaction) { var expectedResponse = interaction.Response; var actualResponse = _httpRequestSender.Send(interaction.Request); return _providerServiceResponseComparer.Compare(expectedResponse, actualResponse); }
public void UsageCount_WhenIncrementUsageIsNotExecuted_DefaultsToZero() { var interaction = new ProviderServiceInteraction(); Assert.Equal(0, interaction.UsageCount); }
public void Handle_WithNancyContext_ConvertIsCalledOnThProviderServiceRequestMapper() { var expectedRequest = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/" }; var expectedResponse = new ProviderServiceResponse(); var nancyContext = new NancyContext { Request = new Request("GET", "/", "HTTP") }; var handler = GetSubject(); _mockRequestMapper.Convert(nancyContext.Request).Returns(expectedRequest); var interaction = new ProviderServiceInteraction { Request = expectedRequest, Response = expectedResponse }; _mockProviderRepository.GetMatchingTestScopedInteraction(expectedRequest) .Returns(interaction); handler.Handle(nancyContext); _mockRequestMapper.Received(1).Convert(nancyContext.Request); }
public void Handle_WithNancyContext_AddHandledRequestIsCalledOnTheMockProviderRepository() { var expectedRequest = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/" }; var actualRequest = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/", Body = new {} }; var expectedResponse = new ProviderServiceResponse(); var handler = GetSubject(); var nancyContext = new NancyContext { Request = new Request("GET", "/", "HTTP") }; var interaction = new ProviderServiceInteraction { Request = expectedRequest, Response = expectedResponse }; _mockProviderRepository.GetMatchingTestScopedInteraction(Arg.Any<ProviderServiceRequest>()) .Returns(interaction); _mockRequestMapper.Convert(nancyContext.Request).Returns(actualRequest); handler.Handle(nancyContext); _mockProviderRepository.Received(1).AddHandledRequest(Arg.Is<HandledRequest>(x => x.ActualRequest == actualRequest && x.MatchedInteraction == interaction)); }
public void Handle_WithNancyContextRequestThatMatchesExpectedRequest_ReturnsNancyResponse() { var expectedRequest = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/Test" }; var actualRequest = new ProviderServiceRequest { Method = HttpVerb.Get, Path = "/Test" }; var expectedResponse = new ProviderServiceResponse { Status = 200 }; var nancyResponse = new Response { StatusCode = HttpStatusCode.OK }; var handler = GetSubject(); var nancyContext = new NancyContext { Request = new Request("GET", "/Test", "HTTP") }; var interaction = new ProviderServiceInteraction { Request = expectedRequest, Response = expectedResponse }; _mockProviderRepository.GetMatchingTestScopedInteraction(Arg.Any<ProviderServiceRequest>()) .Returns(interaction); _mockRequestMapper.Convert(nancyContext.Request).Returns(actualRequest); //mockRequestComparer.Compare Doesnt throw any exceptions _mockResponseMapper.Convert(expectedResponse).Returns(nancyResponse); var response = handler.Handle(nancyContext); Assert.Equal(nancyResponse, response); }
private void ValidateInteraction(ProviderServiceInteraction interaction) { var request = _httpRequestMessageMapper.Convert(interaction.Request); var response = _httpClient.SendAsync(request, CancellationToken.None).Result; var expectedResponse = interaction.Response; var actualResponse = _providerServiceResponseMapper.Convert(response); _providerServiceResponseComparer.Compare(expectedResponse, actualResponse); }
private void RegisterInteraction() { if (String.IsNullOrEmpty(_description)) { throw new InvalidOperationException("description has not been set, please supply using the UponReceiving method."); } if (_request == null) { throw new InvalidOperationException("request has not been set, please supply using the With method."); } if (_response == null) { throw new InvalidOperationException("response has not been set, please supply using the WillRespondWith method."); } var interaction = new ProviderServiceInteraction { ProviderState = _providerState, Description = _description, Request = _request, Response = _response }; _testScopedInteractions = _testScopedInteractions ?? new List<ProviderServiceInteraction>(); _interactions = _interactions ?? new List<ProviderServiceInteraction>(); if (_testScopedInteractions.Any(x => x.Description == interaction.Description && x.ProviderState == interaction.ProviderState)) { throw new InvalidOperationException(String.Format("An interaction already exists with the description \"{0}\" and provider state \"{1}\". Please supply a different description or provider state.", interaction.Description, interaction.ProviderState)); } if (!_interactions.Any(x => x.Description == interaction.Description && x.ProviderState == interaction.ProviderState)) { _interactions.Add(interaction); } _testScopedInteractions.Add(interaction); ClearTrasientState(); }
public void Handle_WithAGetRequestToInteractionsVerificationAndAnInteractionWasSentButNotRegisteredByTheTest_ReturnsFailureResponseContent() { const string failure = "An unexpected request POST /tester was seen by the mock provider service."; var context = new NancyContext { Request = new Request("GET", "/interactions/verification", "http") }; var handler = GetSubject(); var handledRequest = new ProviderServiceRequest(); var handledInteraction = new ProviderServiceInteraction { Request = handledRequest }; var unExpectedRequest = new ProviderServiceRequest { Method = HttpVerb.Post, Path = "/tester" }; _mockProviderRepository.TestScopedInteractions .Returns(new List<ProviderServiceInteraction> { handledInteraction }); _mockProviderRepository.HandledRequests .Returns(new List<HandledRequest> { new HandledRequest(handledRequest, handledInteraction), new HandledRequest(unExpectedRequest, null) }); var response = handler.Handle(context); var content = ReadResponseContent(response); Assert.Equal(failure, content); }
private void VerifyInteraction(ComparisonResult comparisonResult, ProviderServiceInteraction registeredInteraction, bool mayBeUsedMoreThanOnce = false) { var interactionUsages = _mockProviderRepository.HandledRequests.Where(x => x.MatchedInteraction != null && x.MatchedInteraction == registeredInteraction).ToList(); if (interactionUsages == null || !interactionUsages.Any()) { comparisonResult.RecordFailure(new MissingInteractionComparisonFailure(registeredInteraction)); } else if (interactionUsages.Count() > 1 && !mayBeUsedMoreThanOnce) { comparisonResult.RecordFailure(new ErrorMessageComparisonFailure(String.Format("The interaction with description '{0}' and provider state '{1}', was used {2} time/s by the test.", registeredInteraction.Description, registeredInteraction.ProviderState, interactionUsages.Count()))); } }