def test_shouldInjectCorsHeadersInResponse(self): response = Response.empty() self.filter.doFilter(self.request, response, self.filter_chain_mock) self.cors_handler_mock.inject_cors_headers.assert_called_with( self.request.path, response.headers)
def test_givenAResponseWhichShouldNotBeTemplated_whenApplyingFilter_thenDoNothing( self): returned_response = Response.empty() self.templateFilter.doFilter(A_REQUEST, returned_response, self.filterChainMock) self.assertEqual(A_RESPONSE.headers.items(), returned_response.headers.items()) self.assertEqual(A_RESPONSE.body, returned_response.body) self.assertEqual(A_RESPONSE.status, returned_response.status)
def test_givenMatchingExceptionMapper_whenHandlingException_thenReturnTheCreatedHttpResponse( self): created_response = Response(200, {"foo": "bar"}, "body") self.exceptionMapperMock.handles.return_value = True self.exceptionMapperMock.create_response.return_value = created_response self.filterChainMock.doFilter.side_effect = AnException() returned_response = Response.empty() self.filter.doFilter(None, returned_response, self.filterChainMock) self.assertEqual(created_response.headers, returned_response.headers) self.assertEqual(created_response.body, returned_response.body) self.assertEqual(created_response.status, returned_response.status)
def route(self, env, start_response): request = self.request_factory.build_request(env) filter_chain = self.filter_chain_factory.create_filter_chain(request) response = Response.empty() filter_chain.doFilter(request, response) start_response( self.http_status_resolver.get_status_code(response.status), [x for x in response.headers.items()]) if isinstance(response.body, str): return [response.body.encode('utf-8')] else: return [response.body]
import unittest from unittest import mock from jivago.wsgi.filter.filter import Filter from jivago.wsgi.filter.filter_chain import FilterChain from jivago.wsgi.invocation.incorrect_resource_parameters_exception import IncorrectResourceParametersException from jivago.wsgi.invocation.resource_invoker import ResourceInvoker from jivago.wsgi.invocation.route_handler_factory import RouteHandlerFactory from jivago.wsgi.request.request import Request from jivago.wsgi.request.response import Response A_REQUEST = Request('GET', "/path", {}, "", "") A_RESPONSE = Response.empty() class FilterChainTest(unittest.TestCase): def setUp(self): self.resourceInvokerFactoryMock: RouteHandlerFactory = mock.create_autospec( RouteHandlerFactory) self.resourceInvokerMock: ResourceInvoker = mock.create_autospec( ResourceInvoker) self.filterMock: Filter = mock.create_autospec(Filter) self.secondFilterMock: Filter = mock.create_autospec(Filter) self.filterChain = FilterChain( [self.filterMock, self.secondFilterMock], self.resourceInvokerFactoryMock) self.resourceInvokerFactoryMock.create_route_handlers.return_value = [ self.resourceInvokerMock ] def test_givenEmptyFilterChain_whenApplyingFilterChain_thenInvokeResourceInvocator(