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)
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)
def test_config_default(self): patch_path = 'curryproxy.CurryProxy._process_routes' with patch(patch_path) as mocked_method: CurryProxy() default_path = '/etc/curryproxy/routes.json' mocked_method.assert_called_with(default_path)
class Test_Match_Route(TestCase): def setUp(self): super(Test_Match_Route, self).setUp() self.src = "https://www.example.com" self.tgt = "https://new.example.com" self.curry = CurryProxy('curryproxy/tests/etc/') self.forward = next(config.make({'forwards': {self.src: self.tgt}})) def test_match(self): route = self.forward self.curry._routes.append(route) matched_route = self.curry._match_route(self.src + '/path') self.assertEqual(route, matched_route) def test_match_first_route(self): fixme = """ I do not think that this does what's expected. I *think* it's supposed to check that, if a url matches multiple routes, it uses the first one listed in the conf file. But 1. that behavior isn't documented, 2. it's not guaranteed after the conf file rework, and 3. This test doesn't actually test that. The request string only matches one of the two routes given. I don't think this is a good idea anyway. If we want determinism, use match length or lexical sort order or something, not conf file order. """ self.skipTest(fixme) route_config_1 = { 'route': 'https://www.example.com', 'forwarding_url': 'https://1.new.example.com' } route_config_2 = { 'route': 'https://www.example.com/path', 'forwarding_url': 'https://2.new.example.com' } conf = config.normalize([route_config_1, route_config_2]) routes = config.make(conf) self.curry._routes.extend(routes) matched_route = self.curry._match_route('https://www.example.com/p') self.assertEqual(routes[0], matched_route) def test_unmatched_no_matching_routes(self): route = self.forward self.curry._routes.append(route) with ExpectedException(RequestError): self.curry._match_route('https://1.www.example.com/path') def test_unmatched_no_routes(self): self.curry._routes = [] # Ew with ExpectedException(RequestError): self.curry._match_route('https://www.example.com')
class Test_Match_Route(TestCase): def setUp(self): super(Test_Match_Route, self).setUp() self.curry = CurryProxy('curryproxy/tests/etc/routes.empty.json', 'curryproxy/tests/etc/logging.console.conf') def test_match(self): route_config = {'route': 'https://www.example.com', 'forwarding_url': 'https://new.example.com'} route = route_factory.parse_dict(route_config) self.curry._routes.append(route) matched_route = self.curry._match_route('https://www.example.com/path') self.assertEqual(route, matched_route) def test_match_first_route(self): route_config_1 = {'route': 'https://www.example.com', 'forwarding_url': 'https://1.new.example.com'} route_1 = route_factory.parse_dict(route_config_1) route_config_2 = {'route': 'https://www.example.com/path', 'forwarding_url': 'https://2.new.example.com'} route_2 = route_factory.parse_dict(route_config_2) self.curry._routes += [route_1, route_2] matched_route = self.curry._match_route('https://www.example.com/p') self.assertEqual(route_1, matched_route) def test_unmatched_no_matching_routes(self): route_config = {'route': 'https://www.example.com', 'forwarding_url': 'https://new.example.com'} route = route_factory.parse_dict(route_config) self.curry._routes.append(route) with ExpectedException(RequestError): self.curry._match_route('https://1.www.example.com/path') def test_unmatched_no_routes(self): self.assertEqual(0, len(self.curry._routes)) with ExpectedException(RequestError): self.curry._match_route('https://www.example.com')
def test_config_multiple_routes(self): routes_path = 'curryproxy/tests/etc/routes.forwarding_addresses.json' curry = CurryProxy(routes_path, self.logging_conf_path) self.assertEqual(2, len(curry._routes))
def test_config_invalid_path(self): with ExpectedException(IOError): CurryProxy('curryproxy/tests/etc/missing_file.json', self.logging_conf_path)
def test_config_invalid_json(self): with ExpectedException(ConfigError): CurryProxy('curryproxy/tests/etc/routes.invalid_json.json', self.logging_conf_path)
def test_config_supplied(self): route_file_path = 'curryproxy/tests/etc/routes.forwarding_address.json' curry = CurryProxy(route_file_path) self.assertEqual(1, len(curry._routes))
def setUp(self): super(Test_Match_Route, self).setUp() self.src = "https://www.example.com" self.tgt = "https://new.example.com" self.curry = CurryProxy('curryproxy/tests/etc/') self.forward = next(config.make({'forwards': {self.src: self.tgt}}))
def setUp(self): super(Test_Match_Route, self).setUp() self.curry = CurryProxy('curryproxy/tests/etc/routes.empty.json', 'curryproxy/tests/etc/logging.console.conf')
# Included for convenience. Loads conf files from the default location # of /etc/curryproxy. # # Example usage: `gunicorn curryproxy.wsgi:app` # # FIXME: Not sure how to appropriately test this; presumably # /etc/curryproxy isn't guaranteed to be available in tox tests. Maybe a # chroot? from curryproxy import CurryProxy app = CurryProxy()
def setUp(self): super(Test__Call__, self).setUp() self.curry = CurryProxy('curryproxy/tests/etc')
def test_init(self): curry = CurryProxy(self.etc) self.assertIsInstance(curry, CurryProxy)