def test_endpoints_and_forwarding_url_supplied(self): config = {'route': self.forwarding_pattern, 'forwarding_url': self.endpoint_1, 'endpoints': self.endpoints} with ExpectedException(ConfigError): route_factory.parse_dict(config)
def test_endpoints_wildcard_missing(self): config = { 'route': self.forwarding_pattern, 'endpoints': self.endpoints } with ExpectedException(ConfigError): route_factory.parse_dict(config)
def test_endpoints_ignore_errors_range_entry_missing_start_number(self): ignore_errors = ["-404"] route_dict = {'route': self.endpoints_pattern, 'endpoints': self.endpoints, 'ignore_errors': ignore_errors} with ExpectedException(ConfigError): route_factory.parse_dict(route_dict)
def test_endpoints_ignore_errors_range_entry_invalid_stop_number(self): ignore_errors = ["401-404A"] route_dict = {'route': self.endpoints_pattern, 'endpoints': self.endpoints, 'ignore_errors': ignore_errors} with ExpectedException(ConfigError): route_factory.parse_dict(route_dict)
def test_endpoints_ignore_errors_single_entry_invalid_range_max(self): ignore_errors = ["600"] route_dict = {'route': self.endpoints_pattern, 'endpoints': self.endpoints, 'ignore_errors': ignore_errors} with ExpectedException(ConfigError): route_factory.parse_dict(route_dict)
def test_endpoints_and_forwarding_url_supplied(self): config = { 'route': self.forwarding_pattern, 'forwarding_url': self.endpoint_1, 'endpoints': self.endpoints } with ExpectedException(ConfigError): route_factory.parse_dict(config)
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_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_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_endpoints_ignore_errors_single_entry_valid_range_min(self): ignore_errors = ["0"] route_dict = {'route': self.endpoints_pattern, 'endpoints': self.endpoints, 'ignore_errors': ignore_errors} route = route_factory.parse_dict(route_dict) self.assertEqual([0], route._ignore_errors)
def test_endpoints_priority_errors(self): priority_errors = [401, 404] route_dict = {'route': self.endpoints_pattern, 'endpoints': self.endpoints, 'priority_errors': priority_errors} route = route_factory.parse_dict(route_dict) self.assertEqual(priority_errors, route._priority_errors)
def test_endpoints(self): route = route_factory.parse_dict(self.endpoints_config) self.assertTrue(self.endpoint_1['id'] in route._endpoints) self.assertEqual(self.endpoint_1['url'], route._endpoints[self.endpoint_1['id']]) self.assertTrue(self.endpoint_2['id'] in route._endpoints) self.assertEqual(self.endpoint_2['url'], route._endpoints[self.endpoint_2['id']])
def test_endpoint(self): config = {'route': self.endpoints_pattern, 'endpoints': [self.endpoint_1]} route = route_factory.parse_dict(config) self.assertTrue(self.endpoint_1['id'] in route._endpoints) self.assertEqual(self.endpoint_1['url'], route._endpoints[self.endpoint_1['id']])
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_endpoints_ignore_errors_range_entry_valid(self): ignore_errors = ["401-404"] route_dict = {'route': self.endpoints_pattern, 'endpoints': self.endpoints, 'ignore_errors': ignore_errors} route = route_factory.parse_dict(route_dict) self.assertEqual(range(401, 405), route._ignore_errors)
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_endpoint(self): config = { 'route': self.endpoints_pattern, 'endpoints': [self.endpoint_1] } route = route_factory.parse_dict(config) self.assertTrue(self.endpoint_1['id'] in route._endpoints) self.assertEqual(self.endpoint_1['url'], route._endpoints[self.endpoint_1['id']])
def test_endpoints_priority_errors(self): priority_errors = [401, 404] route_dict = { 'route': self.endpoints_pattern, 'endpoints': self.endpoints, 'priority_errors': priority_errors } route = route_factory.parse_dict(route_dict) self.assertEqual(priority_errors, route._priority_errors)
def _process_routes(self, routes_file): """Read a configuration file and setup an instance of CurryProxy. Args: routes_file: Path to CurryProxy's main configuration file which details the routes to be served and the backend endpoints each route points to. """ try: route_configs = json.load(open(routes_file)) except ValueError: raise ConfigError('Configuration file contains invalid JSON.') for route_config in route_configs: self._routes.append(route_factory.parse_dict(route_config))
def test_endpoints_wildcard_missing(self): config = {'route': self.forwarding_pattern, 'endpoints': self.endpoints} with ExpectedException(ConfigError): route_factory.parse_dict(config)
def test_forwarding_url(self): route = route_factory.parse_dict(self.forwarding_config) self.assertEqual(self.endpoint_1, route._forwarding_url)
def test_endpoints_ignore_errors_missing(self): route = route_factory.parse_dict(self.endpoints_config) self.assertEqual([], route._ignore_errors)
def test_route(self): route = route_factory.parse_dict(self.endpoints_config) self.assertEqual([self.endpoints_pattern], route._url_patterns)
def test_route_missing(self): with ExpectedException(ConfigError): route_factory.parse_dict({'endpoints': self.endpoints})
def test_endpoints_and_forwarding_url_missing(self): with ExpectedException(ConfigError): route_factory.parse_dict({'route': self.forwarding_pattern})
def test_status(self): route_dict = {'status': ['http://curryproxy.example.com/status']} route = route_factory.parse_dict(route_dict) self.assertEqual(route_dict['status'], route._url_patterns)