def test_unmatched_too_specific(self):
        url_pattern = 'https://www.example.com/path'
        route = ForwardingRoute([url_pattern], self.forwarding_url)

        request_url = 'https://www.example.com'

        self.assertIsNone(route._find_pattern_for_request(request_url))
    def test_matched_case_insensitive(self):
        url_pattern = 'https://www.example.com'
        route = ForwardingRoute([url_pattern], self.forwarding_url)

        request_url = 'https://www.example.com'.upper()

        self.assertTrue(route._find_pattern_for_request(request_url))
    def test_matched_exact(self):
        url_pattern = 'https://www.example.com'
        route = ForwardingRoute([url_pattern], self.forwarding_url)

        request_url = url_pattern

        self.assertTrue(route._find_pattern_for_request(request_url))
    def test_matched_beginning(self):
        url_pattern = 'https://www.example.com'
        route = ForwardingRoute([url_pattern], self.forwarding_url)

        request_url = '{0}/path?query=true'.format(url_pattern)

        self.assertTrue(route._find_pattern_for_request(request_url))
    def setUp(self):
        super(Test_Create_Forwarded_Url, self).setUp()

        self.url_patterns = ['https://www.example.com']
        self.forwarding_url = 'https://new.example.com'
        self.forwarding_route = ForwardingRoute(self.url_patterns,
                                                self.forwarding_url)
Example #6
0
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))
class Test_Create_Forwarded_Url(TestCase):
    def setUp(self):
        super(Test_Create_Forwarded_Url, self).setUp()

        self.url_patterns = ['https://www.example.com']
        self.forwarding_url = 'https://new.example.com'
        self.forwarding_route = ForwardingRoute(self.url_patterns,
                                                self.forwarding_url)

    def test_matched_once(self):
        path = '/path?redirect={0}/index.html'.format(self.url_patterns[0])
        url = self.url_patterns[0] + path

        forwarded_url = self.forwarding_route._create_forwarded_url(url)

        self.assertEqual(self.forwarding_url + path, forwarded_url)

    def test_matched_path(self):
        path = '/path?and=query#hash'
        url = self.url_patterns[0] + path

        forwarded_url = self.forwarding_route._create_forwarded_url(url)

        self.assertEqual(self.forwarding_url + path, forwarded_url)

    def test_matched_path_empty(self):
        url = self.url_patterns[0]

        forwarded_url = self.forwarding_route._create_forwarded_url(url)

        self.assertEqual(self.forwarding_url, forwarded_url)

    def test_unmatched(self):
        with ExpectedException(RequestError):
            url = 'https://bad.example.com/bad'

            self.forwarding_route._create_forwarded_url(url)
Example #8
0
    def test___call__(self):
        route = ForwardingRoute(['http://example.com'], 'http://1.example.com')

        request = Request.blank('http://example.com/path')

        requests_response = RequestsResponseMock(headers={})
        requests_patcher = patch('requests.request',
                                 return_value=requests_response)
        requests_patcher.start()

        response___init__ = patch.object(SingleResponse,
                                         '__init__',
                                         return_value=None)
        sr_mock = response___init__.start()

        response_response = patch.object(SingleResponse, 'response')
        response_response.start()

        route(request)

        sr_mock.assert_called_with(request, requests_response)

        response_response.stop()
        response___init__.stop()