class Test__Call__(TestCase):
    def setUp(self):
        super(Test__Call__, self).setUp()

        self.curry = CurryProxy('curryproxy/tests/etc/routes.empty.json',
                                'curryproxy/tests/etc/logging.console.conf')

    def test_matched_route(self):
        # Setup route
        self.route_config = {
            'route': 'https://www.example.com',
            'forwarding_url': 'https://new.example.com'
        }
        self.route = route_factory.parse_dict(self.route_config)
        self.curry._routes.append(self.route)

        # Mocked response
        mock_response = Response()
        mock_response.status = 200
        mock_response.body = 'Response Body'

        # Create request
        environ = {
            'wsgi.url_scheme': 'https',
            'HTTP_HOST': 'www.example.com',
            'PATH_INFO': '/path',
            'QUERY_STRING': 'query=string'
        }
        start_response = StartResponseMock()

        # Issue request
        with patch.object(ForwardingRoute,
                          '__call__',
                          return_value=mock_response):
            response = self.curry.__call__(environ, start_response)

            # Assert
            self.assertEqual(mock_response.status, start_response.status)
            self.assertEqual(mock_response.headerlist, start_response.headers)
            self.assertEqual(mock_response.body, response)

    def test_unmatched_route(self):
        environ = {
            'wsgi.url_scheme': 'https',
            'HTTP_HOST': 'www.example.com',
            'PATH_INFO': '/path',
            'QUERY_STRING': 'query=string'
        }
        start_response = StartResponseMock()

        response = self.curry.__call__(environ, start_response)

        self.assertEqual('403 Forbidden', start_response.status)
        self.assertIsNotNone(response)
class Test__Call__(TestCase):
    def setUp(self):
        super(Test__Call__, self).setUp()

        self.curry = CurryProxy('curryproxy/tests/etc')

    def test_matched_route(self):
        # Setup route
        conf = {
            'forwards': {
                'https://www.example.com': 'https://new.example.com'}}
        self.routes = config.make(conf)
        self.curry._routes.extend(self.routes)

        # Mocked response
        mock_response = Response()
        mock_response.status = 200
        mock_response.body = b'Response Body'

        # Create request
        environ = {'wsgi.url_scheme': 'https',
                   'HTTP_HOST': 'www.example.com',
                   'PATH_INFO': '/path',
                   'QUERY_STRING': 'query=string'}
        start_response = StartResponseMock()

        # Issue request
        with patch.object(ForwardingRoute,
                          '__call__',
                          return_value=mock_response):
            response = self.curry.__call__(environ, start_response)

            # Assert
            self.assertEqual(mock_response.status, start_response.status)
            self.assertEqual(mock_response.headerlist, start_response.headers)
            self.assertEqual([mock_response.body], response)

    def test_unmatched_route(self):
        environ = {'wsgi.url_scheme': 'https',
                   'HTTP_HOST': 'www.example.com',
                   'PATH_INFO': '/path',
                   'QUERY_STRING': 'query=string'}
        start_response = StartResponseMock()

        response = self.curry.__call__(environ, start_response)

        self.assertEqual('403 Forbidden', start_response.status)
        self.assertIsNotNone(response)
Exemple #3
0
class Test__Call__(TestCase):
    def setUp(self):
        super(Test__Call__, self).setUp()

        self.curry = CurryProxy('curryproxy/tests/etc/routes.empty.json',
                                'curryproxy/tests/etc/logging.console.conf')

    def test_matched_route(self):
        # Setup route
        self.route_config = {'route': 'https://www.example.com',
                             'forwarding_url': 'https://new.example.com'}
        self.route = route_factory.parse_dict(self.route_config)
        self.curry._routes.append(self.route)

        # Mocked response
        mock_response = Response()
        mock_response.status = 200
        mock_response.body = 'Response Body'

        # Create request
        environ = {'wsgi.url_scheme': 'https',
                   'HTTP_HOST': 'www.example.com',
                   'PATH_INFO': '/path',
                   'QUERY_STRING': 'query=string'}
        start_response = StartResponseMock()

        # Issue request
        with patch.object(ForwardingRoute,
                          '__call__',
                          return_value=mock_response):
            response = self.curry.__call__(environ, start_response)

            # Assert
            self.assertEqual(mock_response.status, start_response.status)
            self.assertEqual(mock_response.headerlist, start_response.headers)
            self.assertEqual(mock_response.body, response)

    def test_unmatched_route(self):
        environ = {'wsgi.url_scheme': 'https',
                   'HTTP_HOST': 'www.example.com',
                   'PATH_INFO': '/path',
                   'QUERY_STRING': 'query=string'}
        start_response = StartResponseMock()

        response = self.curry.__call__(environ, start_response)

        self.assertEqual('403 Forbidden', start_response.status)
        self.assertIsNotNone(response)