public Response Handle(NancyContext context)
        {
            try
            {
                var response = HandlePactRequest(context);

                return(response);
            }
            catch (Exception ex)
            {
                var exceptionMessage = ex.Message
                                       .Replace("\r", " ")
                                       .Replace("\n", "")
                                       .Replace("\t", " ")
                                       .Replace(@"\", "");

                var errorResponse = new ProviderServiceResponse
                {
                    Status = 500,
                    Body   = exceptionMessage
                };
                var response = _responseMapper.Convert(errorResponse);
                response.ReasonPhrase = exceptionMessage;

                return(response);
            }
        }
        private Response HandlePactRequest(NancyContext context)
        {
            var actualRequest       = _requestMapper.Convert(context.Request);
            var actualRequestMethod = actualRequest.Method.ToString().ToUpperInvariant();
            var actualRequestPath   = actualRequest.Path;

            _log.InfoFormat("Received request {0} {1}", actualRequestMethod, actualRequestPath);
            _log.Debug(JsonConvert.SerializeObject(actualRequest, JsonConfig.PactFileSerializerSettings));

            ProviderServiceInteraction matchingInteraction;

            try
            {
                matchingInteraction = _mockProviderRepository.GetMatchingTestScopedInteraction(actualRequest);
                _mockProviderRepository.AddHandledRequest(new HandledRequest(actualRequest, matchingInteraction));

                _log.InfoFormat("Found matching response for {0} {1}", actualRequestMethod, actualRequestPath);
                _log.Debug(JsonConvert.SerializeObject(matchingInteraction.Response, JsonConfig.PactFileSerializerSettings));
            }
            catch (Exception)
            {
                _log.ErrorFormat("No matching interaction found for {0} {1}", actualRequestMethod, actualRequestPath);
                _mockProviderRepository.AddHandledRequest(new HandledRequest(actualRequest, null));
                throw;
            }

            return(_responseMapper.Convert(matchingInteraction.Response));
        }
        private Response HandlePactRequest(NancyContext context)
        {
            var actualRequest = _requestMapper.Convert(context.Request);

            var matchingInteraction = _mockProviderRepository.GetMatchingTestScopedInteraction(actualRequest);

            _mockProviderRepository.AddHandledRequest(new HandledRequest(actualRequest, matchingInteraction));

            return(_responseMapper.Convert(matchingInteraction.Response));
        }
Exemple #4
0
        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);
        }
        public Response Handle(NancyContext context)
        {
            //TODO: This is a hack and should probably go in VerifyInteractions()
            if (!_injected)
            {
                _requestComparer = new ProviderServiceRequestComparer(new Reporter());
            }

            var reporter = _requestComparer.GetReporter();

            try
            {
                var response = HandlePactRequest(context);

                reporter.ThrowIfAnyErrors();

                return(response);
            }
            catch (Exception ex)
            {
                var exceptionMessage = ex.Message
                                       .Replace("\r", " ")
                                       .Replace("\n", "")
                                       .Replace("\t", " ")
                                       .Replace(@"\", "");

                var errorResponse = new ProviderServiceResponse
                {
                    Status = 500,
                    Body   = exceptionMessage
                };
                var response = _responseMapper.Convert(errorResponse);
                response.ReasonPhrase = exceptionMessage;

                return(response);
            }
        }