public void Handle_WithNancyContextRequestThatDoesNotMatchExpectedRequest_ResponseMapperIsCalledAndReturns500Response()
        {
            var expectedRequest = new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path   = "/Test"
            };
            var actualRequest = new ProviderServiceRequest
            {
                Method = HttpVerb.Put,
                Path   = "/Test"
            };
            var expectedResponse = new ProviderServiceResponse {
                Status = 200
            };
            var nancyResponse = new Response {
                StatusCode = HttpStatusCode.OK
            };
            var compareException = new CompareFailedException("Something failed");

            var mockRequestComparer = Substitute.For <IProviderServiceRequestComparer>();
            var mockRequestMapper   = Substitute.For <IProviderServiceRequestMapper>();
            var mockResponseMapper  = Substitute.For <INancyResponseMapper>();

            var nancyContext = new NancyContext
            {
                Request = new Request("GET", "/Test", "HTTP")
            };

            var interactions = new List <ProviderServiceInteraction>
            {
                new ProviderServiceInteraction {
                    Request = expectedRequest, Response = expectedResponse
                }
            };

            nancyContext.SetMockInteraction(interactions);

            mockRequestMapper.Convert(nancyContext.Request).Returns(actualRequest);
            mockRequestComparer
            .When(x => x.Compare(expectedRequest, actualRequest))
            .Do(x => { throw compareException; });

            mockResponseMapper.Convert(expectedResponse).Returns(nancyResponse);

            mockResponseMapper.Convert(Arg.Any <ProviderServiceResponse>())
            .Returns(new Response
            {
                StatusCode = HttpStatusCode.InternalServerError
            });

            IMockProviderNancyRequestHandler handler = new MockProviderNancyRequestHandler(mockRequestComparer, mockRequestMapper, mockResponseMapper);

            var response = handler.Handle(nancyContext);

            mockResponseMapper.Received(1).Convert(Arg.Is <ProviderServiceResponse>(x => x.Status == 500));
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            Assert.NotEmpty(response.ReasonPhrase);
        }
        public void Handle_WhenExpectionIsThrownHandlingRequest_ReasonPhraseAndBodyContentIsSetWithoutBackSlashes()
        {
            var nancyResponse = new Response {
                StatusCode = HttpStatusCode.OK
            };
            var          compareException = new CompareFailedException("Something\r\n \t \\ failed");
            const string expectedMessage  = "Something     failed";

            var mockRequestComparer = Substitute.For <IProviderServiceRequestComparer>();
            var mockRequestMapper   = Substitute.For <IProviderServiceRequestMapper>();
            var mockResponseMapper  = Substitute.For <INancyResponseMapper>();

            var nancyContext = new NancyContext
            {
                Request = new Request("GET", "/Test", "HTTP")
            };

            mockRequestMapper
            .When(x => x.Convert(Arg.Any <Request>()))
            .Do(x => { throw compareException; });

            mockResponseMapper
            .Convert(Arg.Any <ProviderServiceResponse>())
            .Returns(nancyResponse);

            mockResponseMapper.Convert(Arg.Any <ProviderServiceResponse>())
            .Returns(new Response
            {
                StatusCode = HttpStatusCode.InternalServerError
            });

            IMockProviderNancyRequestHandler handler = new MockProviderNancyRequestHandler(mockRequestComparer, mockRequestMapper, mockResponseMapper);

            var response = handler.Handle(nancyContext);

            Assert.Equal(expectedMessage, response.ReasonPhrase);
            mockResponseMapper.Received(1).Convert(Arg.Is <ProviderServiceResponse>(x => BodyContentMatches(x, expectedMessage)));
        }
        public void Handle_WhenExpectionIsThrownHandlingRequest_ReasonPhraseAndBodyContentIsSetWithoutBackSlashes()
        {
            var nancyResponse = new Response { StatusCode = HttpStatusCode.OK };
            var compareException = new CompareFailedException("Something\r\n \t \\ failed");
            const string expectedMessage = "Something     failed";

            var mockRequestComparer = Substitute.For<IProviderServiceRequestComparer>();
            var mockRequestMapper = Substitute.For<IProviderServiceRequestMapper>();
            var mockResponseMapper = Substitute.For<INancyResponseMapper>();

            var nancyContext = new NancyContext
            {
                Request = new Request("GET", "/Test", "HTTP")
            };

            mockRequestMapper
                .When(x => x.Convert(Arg.Any<Request>()))
                .Do(x => { throw compareException; });

            mockResponseMapper
                .Convert(Arg.Any<ProviderServiceResponse>())
                .Returns(nancyResponse);

            mockResponseMapper.Convert(Arg.Any<ProviderServiceResponse>())
                .Returns(new Response
                {
                    StatusCode = HttpStatusCode.InternalServerError
                });

            IMockProviderNancyRequestHandler handler = new MockProviderNancyRequestHandler(mockRequestComparer, mockRequestMapper, mockResponseMapper);

            var response = handler.Handle(nancyContext);

            Assert.Equal(expectedMessage, response.ReasonPhrase);
            mockResponseMapper.Received(1).Convert(Arg.Is<ProviderServiceResponse>(x => BodyContentMatches(x, expectedMessage)));
        }
        public void Handle_WithNancyContextRequestThatDoesNotMatchExpectedRequest_ResponseMapperIsCalledAndReturns500Response()
        {
            var expectedRequest = new ProviderServiceRequest
            {
                Method = HttpVerb.Get,
                Path = "/Test"
            };
            var actualRequest = new ProviderServiceRequest
            {
                Method = HttpVerb.Put,
                Path = "/Test"
            };
            var expectedResponse = new ProviderServiceResponse { Status = 200 };
            var nancyResponse = new Response { StatusCode = HttpStatusCode.OK };
            var compareException = new CompareFailedException("Something failed");

            var mockRequestComparer = Substitute.For<IProviderServiceRequestComparer>();
            var mockRequestMapper = Substitute.For<IProviderServiceRequestMapper>();
            var mockResponseMapper = Substitute.For<INancyResponseMapper>();

            var nancyContext = new NancyContext
            {
                Request = new Request("GET", "/Test", "HTTP")
            };

            var interactions = new List<ProviderServiceInteraction>
            {
                new ProviderServiceInteraction { Request = expectedRequest, Response = expectedResponse }
            };

            nancyContext.SetMockInteraction(interactions);

            mockRequestMapper.Convert(nancyContext.Request).Returns(actualRequest);
            mockRequestComparer
                .When(x => x.Compare(expectedRequest, actualRequest))
                .Do(x => { throw compareException; });

            mockResponseMapper.Convert(expectedResponse).Returns(nancyResponse);

            mockResponseMapper.Convert(Arg.Any<ProviderServiceResponse>())
                .Returns(new Response
                {
                    StatusCode = HttpStatusCode.InternalServerError
                });

            IMockProviderNancyRequestHandler handler = new MockProviderNancyRequestHandler(mockRequestComparer, mockRequestMapper, mockResponseMapper);

            var response = handler.Handle(nancyContext);

            mockResponseMapper.Received(1).Convert(Arg.Is<ProviderServiceResponse>(x => x.Status == 500));
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            Assert.NotEmpty(response.ReasonPhrase);
        }