class Test_Resolve_Query_String(TestCase): def setUp(self): super(Test_Resolve_Query_String, self).setUp() self.route = EndpointsRoute([], [], [], []) self.request_base_url = 'https://1.example.com/path' def test_with_no_override(self): url = self.route._resolve_query_string( self.request_base_url + '?curryproxy=1:test=123,2:test=456', '1' ) self.assertEquals(self.request_base_url + '?test=123', url) def test_with_override(self): url = self.route._resolve_query_string( self.request_base_url + '?test=123&curryproxy=1:test=456', '1' ) self.assertEquals(self.request_base_url + '?test=456', url) def test_no_params_for_endpoint(self): url = self.route._resolve_query_string( self.request_base_url + '?curryproxy=1:test=123', '2' ) self.assertEquals(self.request_base_url, url) def test_multiple_parameters(self): url = self.route._resolve_query_string( self.request_base_url + '?curryproxy=1:test=123%26another_test=456,2:test=456', '1' ) self.assertEquals( self.request_base_url + '?test=123&another_test=456', url ) def test_multiple_curryproxy_parameters(self): url = self.route._resolve_query_string( ( self.request_base_url + '?curryproxy=1:test=123&curryproxy=1:test=456' ), '1' ) self.assertEquals(self.request_base_url + '?test=123', url)
def setUp(self): super(Test_Create_Forwarded_Urls, self).setUp() url_patterns = [ 'https://example.com/{Endpoint_IDs}/', 'https://www.example.com/{Endpoint_IDs}/' ] self.endpoints = { "one": "https://1.example.com/", "two": "https://2.example.com/" } self.route = EndpointsRoute(url_patterns, self.endpoints, [], [])
def setUp(self): super(Test_Find_Pattern_For_Request, self).setUp() self.url_patterns = [ 'https://example.com/{Endpoint_IDs}/', 'https://www.example.com/{Endpoint_IDs}/' ] endpoints = { "one": "https://1.example.com/", "two": "https://2.example.com/" } self.route = EndpointsRoute(self.url_patterns, endpoints, [], [])
def setUp(self): super(Test__Log_Response, self).setUp() self.endpoints_route = EndpointsRoute( mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ) self.logger = logging.getLogger('curryproxy.routes.endpoints_route') patcher = patch.object(logging, 'debug') self.logging_debug = patcher.start() self.addCleanup(patcher.stop)
def setUp(self): super(Test_Find_Pattern_For_Request, self).setUp() self.url_patterns = ['https://example.com/{Endpoint_IDs}/', 'https://www.example.com/{Endpoint_IDs}/'] endpoints = {"one": "https://1.example.com/", "two": "https://2.example.com/"} self.route = EndpointsRoute(self.url_patterns, endpoints, [], [])
def setUp(self): super(Test_Create_Forwarded_Urls, self).setUp() url_patterns = ['https://example.com/{Endpoint_IDs}/', 'https://www.example.com/{Endpoint_IDs}/'] self.endpoints = {"one": "https://1.example.com/", "two": "https://2.example.com/"} self.route = EndpointsRoute(url_patterns, self.endpoints, [], [])
def test_forbidden_endpoint_id_asterisk(self): endpoints = { "one": "http://1.example.com/", "*": "http://2.example.com/" } with ExpectedException(ConfigError): EndpointsRoute(None, endpoints, None, None)
def test_duplicate_endpoint_ids(self): endpoints = { "one": "http://1.example.com/", "ONE": "http://1.example.com/", "two": "http://2.example.com/" } with ExpectedException(ConfigError): EndpointsRoute(None, endpoints, None, None)
def setUp(self): super(Test__call__Unmocked, self).setUp() url_patterns = ['http://example.com/{Endpoint_IDs}/'] endpoint = {'test': 'http://example.com/'} endpoints = {'1': 'http://1.example.com/', '2': 'http://2.example.com/'} endpoints_for_ignore = {'1': 'http://1.example.com/', '2': 'http://2.example.com/', '3': 'http://3.example.com/', '4': 'http://4.example.com/', '5': 'http://5.example.com/'} self.endpoint_route = EndpointsRoute(url_patterns, endpoint, [], []) self.endpoints_route = EndpointsRoute(url_patterns, endpoints, [], []) self.endpoints_route_with_ignore = EndpointsRoute(url_patterns, endpoints_for_ignore, [], [0, 400, 500, 501, 502, 503])
def test_endpoint_ids_lowered(self): endpoints = { "ONE": "http://1.example.com/", "two": "http://2.example.com/" } endpoints_route = EndpointsRoute(None, endpoints, None, None) self.assertTrue('one' in endpoints_route._endpoints) self.assertFalse('ONE' in endpoints_route._endpoints)
def parse_dict(route_config): """Produces a route based on an entry in CurryProxy's configuration file. Note that this function will be called multiple times, once for each route CurryProxy is configured to handle. Args: route_config: Dictionary representing one entry in CurryProxy's configuration file. Raises: ConfigError: An error was detected when parsing the data found in the configuration file. """ url_patterns = None if 'status' in route_config: return StatusRoute(route_config['status']) if 'route' not in route_config: raise ConfigError('Each route must contain "route"') # Convert to list for later support of multiple routes per endpoint url_patterns = [route_config['route']] if ('forwarding_url' in route_config and 'endpoints' in route_config): raise ConfigError( 'The route "{0}" cannot contain both ' '"forwarding_url" and "endpoints"'.format(url_patterns)) if 'forwarding_url' in route_config: return ForwardingRoute(url_patterns, route_config['forwarding_url']) if 'endpoints' in route_config: if any('{Endpoint_IDs' not in pattern for pattern in url_patterns): raise ConfigError( 'The route "{0}" must contain the ' 'placeholder "{{Endpoint_IDs}}"'.format(url_patterns)) endpoints = {} for endpoint in route_config['endpoints']: endpoints[endpoint['id']] = endpoint['url'] priority_errors = [] if 'priority_errors' in route_config: priority_errors = route_config['priority_errors'] ignore_errors = parse_ignore_rules(route_config) return EndpointsRoute(url_patterns, endpoints, priority_errors, list(ignore_errors)) raise ConfigError('The route "{0}" must contain either "forwarding_url" ' 'or "endpoints"'.format(url_patterns))
def setUp(self): super(Test__Call__, self).setUp() self.patcher = patch.object(grequests, 'map') self.grequests_map = self.patcher.start() url_patterns = ['http://example.com/{Endpoint_IDs}/'] endpoint = {'test': 'http://example.com/'} endpoints = {'1': 'http://1.example.com/', '2': 'http://2.example.com/'} endpoints_for_ignore = {'1': 'http://1.example.com/', '2': 'http://2.example.com/', '3': 'http://3.example.com/', '4': 'http://4.example.com/', '5': 'http://5.example.com/'} self.endpoint_route = EndpointsRoute(url_patterns, endpoint, [], []) self.endpoints_route = EndpointsRoute(url_patterns, endpoints, [], []) self.endpoints_route_with_ignore = EndpointsRoute(url_patterns, endpoints_for_ignore, [], [0, 400, 500, 501, 502, 503])
class Test_Find_Pattern_For_Request(TestCase): def setUp(self): super(Test_Find_Pattern_For_Request, self).setUp() self.url_patterns = [ 'https://example.com/{Endpoint_IDs}/', 'https://www.example.com/{Endpoint_IDs}/' ] endpoints = { "one": "https://1.example.com/", "two": "https://2.example.com/" } self.route = EndpointsRoute(self.url_patterns, endpoints, [], []) def test_case_insensitivity(self): request_url = 'https://example.com/one,TwO/path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEqual(self.url_patterns[0], url_pattern) def test_pattern_not_found(self): request_url = 'http://example.com/' url_pattern = self.route._find_pattern_for_request(request_url) self.assertIsNone(url_pattern) def test_whitespace_encoded_leading(self): request_url = 'https://example.com/%20one,%20two/path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEqual(self.url_patterns[0], url_pattern) def test_whitespace_encoded_trailing(self): request_url = 'https://example.com/one%20,two%20/path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEqual(self.url_patterns[0], url_pattern) def test_whitespace_leading(self): request_url = 'https://example.com/ one, two/path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEqual(self.url_patterns[0], url_pattern) def test_whitespace_trailing(self): request_url = 'https://example.com/one ,two /path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEqual(self.url_patterns[0], url_pattern)
class Test_Find_Pattern_For_Request(TestCase): def setUp(self): super(Test_Find_Pattern_For_Request, self).setUp() self.url_patterns = ['https://example.com/{Endpoint_IDs}/', 'https://www.example.com/{Endpoint_IDs}/'] endpoints = {"one": "https://1.example.com/", "two": "https://2.example.com/"} self.route = EndpointsRoute(self.url_patterns, endpoints, [], []) def test_case_insensitivity(self): request_url = 'https://example.com/one,TwO/path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEquals(self.url_patterns[0], url_pattern) def test_pattern_not_found(self): request_url = 'http://example.com/' url_pattern = self.route._find_pattern_for_request(request_url) self.assertIsNone(url_pattern) def test_whitespace_encoded_leading(self): request_url = 'https://example.com/%20one,%20two/path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEquals(self.url_patterns[0], url_pattern) def test_whitespace_encoded_trailing(self): request_url = 'https://example.com/one%20,two%20/path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEquals(self.url_patterns[0], url_pattern) def test_whitespace_leading(self): request_url = 'https://example.com/ one, two/path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEquals(self.url_patterns[0], url_pattern) def test_whitespace_trailing(self): request_url = 'https://example.com/one ,two /path' url_pattern = self.route._find_pattern_for_request(request_url) self.assertEquals(self.url_patterns[0], url_pattern)
class Test_Resolve_Query_String(TestCase): def setUp(self): super(Test_Resolve_Query_String, self).setUp() self.route = EndpointsRoute([], [], [], []) self.request_base_url = 'https://1.example.com/path' def test_with_no_override(self): url = self.route._resolve_query_string( self.request_base_url + '?curryproxy=1:test=123,2:test=456', '1') self.assertEquals(self.request_base_url + '?test=123', url) def test_with_override(self): url = self.route._resolve_query_string( self.request_base_url + '?test=123&curryproxy=1:test=456', '1') self.assertEquals(self.request_base_url + '?test=456', url) def test_no_params_for_endpoint(self): url = self.route._resolve_query_string( self.request_base_url + '?curryproxy=1:test=123', '2') self.assertEquals(self.request_base_url, url) @unittest.skip("FIXME: Broken test; fails half the time because the query " "order isn't guaranteed") def test_multiple_parameters(self): url = self.route._resolve_query_string( self.request_base_url + '?curryproxy=1:test=123%26another_test=456,2:test=456', '1') self.assertEquals(self.request_base_url + '?test=123&another_test=456', url) def test_multiple_curryproxy_parameters(self): url = self.route._resolve_query_string( (self.request_base_url + '?curryproxy=1:test=123&curryproxy=1:test=456'), '1') self.assertEquals(self.request_base_url + '?test=123', url) def test_upper_case_curryproxy_parameters(self): url = self.route._resolve_query_string( (self.request_base_url + '?curryproxy=A:test=123'), 'a') self.assertEquals(self.request_base_url + '?test=123', url)
def setUp(self): super(Test_Resolve_Query_String, self).setUp() self.route = EndpointsRoute([], [], [], []) self.request_base_url = "https://1.example.com/path"
class Test_Create_Forwarded_Urls(TestCase): def setUp(self): super(Test_Create_Forwarded_Urls, self).setUp() url_patterns = [ 'https://example.com/{Endpoint_IDs}/', 'https://www.example.com/{Endpoint_IDs}/' ] self.endpoints = { "one": "https://1.example.com/", "two": "https://2.example.com/" } self.route = EndpointsRoute(url_patterns, self.endpoints, [], []) def test_all_endpoints(self): request_path = 'path' request_url = 'https://example.com/*/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_case_insensitivity(self): request_path = 'path' request_url = 'https://example.com/one,TwO/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_invalid_endpoint(self): request_path = 'path' request_url = 'https://example.com/1/' + request_path with ExpectedException(RequestError): self.route._create_forwarded_urls(request_url) def test_single_endpoint(self): request_path = 'path' request_url = 'https://example.com/one/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) two_url = self.endpoints['two'] + request_path self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(two_url not in forwarded_urls) def test_whitespace_encoded_leading(self): request_path = 'path' request_url = 'https://example.com/%20one,%20two/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_whitespace_encoded_trailing(self): request_path = 'path' request_url = 'https://example.com/one%20,two%20/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_whitespace_leading(self): request_path = 'path' request_url = 'https://example.com/ one, two/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_whitespace_trailing(self): request_path = 'path' request_url = 'https://example.com/one ,two /' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls)
class Test__Log_Response(TestCase): def setUp(self): super(Test__Log_Response, self).setUp() self.endpoints_route = EndpointsRoute( mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ) self.logger = logging.getLogger('curryproxy.routes.endpoints_route') patcher = patch.object(logging, 'debug') self.logging_debug = patcher.start() self.addCleanup(patcher.stop) def test_log_level_notset(self): self.logger.setLevel(logging.NOTSET) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertTrue(self.logging_debug.called) def test_log_level_debug(self): self.logger.setLevel(logging.DEBUG) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertTrue(self.logging_debug.called) def test_log_level_info(self): self.logger.setLevel(logging.INFO) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertFalse(self.logging_debug.called) def test_log_level_warning(self): self.logger.setLevel(logging.WARNING) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertFalse(self.logging_debug.called) def test_log_level_error(self): self.logger.setLevel(logging.ERROR) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertFalse(self.logging_debug.called) def test_log_level_critical(self): self.logger.setLevel(logging.CRITICAL) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertFalse(self.logging_debug.called)
def setUp(self): super(Test_Resolve_Query_String, self).setUp() self.route = EndpointsRoute([], [], [], []) self.request_base_url = 'https://1.example.com/path'
class Test__Log_Response(TestCase): def setUp(self): super(Test__Log_Response, self).setUp() self.endpoints_route = EndpointsRoute( mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), mock.MagicMock(), ) self.logger = logging.getLogger('curryproxy.routes.endpoints_route') patcher = patch.object(logging, 'debug') self.logging_debug = patcher.start() self.addCleanup(patcher.stop) # FIXME: I do not think that this does quite what was intended. # Always fails. @unittest.skip("FIXME: I do not think that this does quite what was " "intended. Always fails. See comment in log_responses.") def test_log_level_notset(self): self.logger.setLevel(logging.NOTSET) self.endpoints_route._log_responses(mock.Mock(), [MagicMock(), None]) self.assertTrue(self.logging_debug.called) def test_log_level_debug(self): self.logger.setLevel(logging.DEBUG) self.endpoints_route._log_responses(Mock(), [MagicMock(), None]) self.assertTrue(self.logging_debug.called) def test_log_level_info(self): self.logger.setLevel(logging.INFO) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertFalse(self.logging_debug.called) def test_log_level_warning(self): self.logger.setLevel(logging.WARNING) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertFalse(self.logging_debug.called) def test_log_level_error(self): self.logger.setLevel(logging.ERROR) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertFalse(self.logging_debug.called) def test_log_level_critical(self): self.logger.setLevel(logging.CRITICAL) self.endpoints_route._log_responses(mock.Mock(), mock.MagicMock()) self.assertFalse(self.logging_debug.called)
class Test_Create_Forwarded_Urls(TestCase): def setUp(self): super(Test_Create_Forwarded_Urls, self).setUp() url_patterns = ['https://example.com/{Endpoint_IDs}/', 'https://www.example.com/{Endpoint_IDs}/'] self.endpoints = {"one": "https://1.example.com/", "two": "https://2.example.com/"} self.route = EndpointsRoute(url_patterns, self.endpoints, [], []) def test_all_endpoints(self): request_path = 'path' request_url = 'https://example.com/*/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_case_insensitivity(self): request_path = 'path' request_url = 'https://example.com/one,TwO/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_invalid_endpoint(self): request_path = 'path' request_url = 'https://example.com/1/' + request_path with ExpectedException(RequestError): self.route._create_forwarded_urls(request_url) def test_single_endpoint(self): request_path = 'path' request_url = 'https://example.com/one/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) two_url = self.endpoints['two'] + request_path self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(two_url not in forwarded_urls) def test_whitespace_encoded_leading(self): request_path = 'path' request_url = 'https://example.com/%20one,%20two/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_whitespace_encoded_trailing(self): request_path = 'path' request_url = 'https://example.com/one%20,two%20/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_whitespace_leading(self): request_path = 'path' request_url = 'https://example.com/ one, two/' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls) def test_whitespace_trailing(self): request_path = 'path' request_url = 'https://example.com/one ,two /' + request_path forwarded_urls = self.route._create_forwarded_urls(request_url) self.assertTrue(self.endpoints['one'] + request_path in forwarded_urls) self.assertTrue(self.endpoints['two'] + request_path in forwarded_urls)