コード例 #1
0
    def add_auth(self,
                 r: requests.PreparedRequest) -> requests.PreparedRequest:
        rr = r.copy()
        url = urlparse(r.url)

        if 'Host' in r.headers:
            netloc = r.headers['Host'].decode('utf-8')
        else:
            netloc = url.netloc

        rr.url = urlunparse((url.scheme, netloc, url.path, url.params,
                             url.query, url.fragment))

        if r.method == 'POST':
            if r.body:
                if isinstance(r.body, bytes):
                    body = dict(parse_qsl(r.body.decode("utf-8").strip()))
                elif isinstance(r.body, str):
                    body = dict(parse_qsl(r.body.strip()))
                r.body = urlencode(self.update_params(rr, body))
                new_headers = r.headers
                new_headers[
                    'Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8'
                r.headers = new_headers

        elif r.method == 'GET':
            url = urlparse(r.url)
            if url.query:
                new_query = urlencode(
                    self.update_params(rr, dict(parse_qsl(url.query))))
                r.url = urlunparse((url.scheme, url.netloc, url.path,
                                    url.params, new_query, url.fragment))

        return r
コード例 #2
0
    def get_origin_response(origin_request: PreparedRequest, retry: int,
                            timeout: int, delay: int, proxies: dict,
                            allow_redirects: bool,
                            logger: Logger) -> Union[Response, None]:
        """ Получает ответ на оригинальный запрос

        :return:    None - если по истечении `retry` попыток не удалось получить ответ от сервера
                    Response - если удалось получить ответ от сервера
        """

        return RequestHelper.do_request(origin_request.copy(), retry, timeout,
                                        delay, proxies, allow_redirects,
                                        logger)
コード例 #3
0
ファイル: test_matchers.py プロジェクト: Hasimir/betamax
class TestMatchers(unittest.TestCase):
    def setUp(self):
        self.alt_url = ('http://example.com/path/to/end/point?query=string'
                        '&foo=bar')
        self.p = PreparedRequest()
        self.p.body = 'Foo bar'
        self.p.headers = {'User-Agent': 'betamax/test'}
        self.p.url = 'http://example.com/path/to/end/point?query=string'
        self.p.method = 'GET'
        self.p._cookies = RequestsCookieJar()

    def test_matcher_registry_has_body_matcher(self):
        assert 'body' in matchers.matcher_registry

    def test_matcher_registry_has_digest_auth_matcher(self):
        assert 'digest-auth' in matchers.matcher_registry

    def test_matcher_registry_has_headers_matcher(self):
        assert 'headers' in matchers.matcher_registry

    def test_matcher_registry_has_host_matcher(self):
        assert 'host' in matchers.matcher_registry

    def test_matcher_registry_has_method_matcher(self):
        assert 'method' in matchers.matcher_registry

    def test_matcher_registry_has_path_matcher(self):
        assert 'path' in matchers.matcher_registry

    def test_matcher_registry_has_query_matcher(self):
        assert 'query' in matchers.matcher_registry

    def test_matcher_registry_has_uri_matcher(self):
        assert 'uri' in matchers.matcher_registry

    def test_body_matcher(self):
        match = matchers.matcher_registry['body'].match
        assert match(self.p, {
            'body': 'Foo bar',
            'headers': {'User-Agent': 'betamax/test'},
            'uri': 'http://example.com/path/to/end/point?query=string',
            'method': 'GET',
        })
        assert match(self.p, {
            'body': b'',
            'headers': {'User-Agent': 'betamax/test'},
            'uri': 'http://example.com/path/to/end/point?query=string',
            'method': 'GET',
        }) is False

    def test_body_matcher_without_body(self):
        p = self.p.copy()
        p.body = None
        match = matchers.matcher_registry['body'].match
        assert match(p, {
            'body': 'Foo bar',
            'headers': {'User-Agent': 'betamax/test'},
            'uri': 'http://example.com/path/to/end/point?query=string',
            'method': 'GET',
        }) is False
        assert match(p, {
            'body': b'',
            'headers': {'User-Agent': 'betamax/test'},
            'uri': 'http://example.com/path/to/end/point?query=string',
            'method': 'GET',
        })

    def test_digest_matcher(self):
        match = matchers.matcher_registry['digest-auth'].match
        assert match(self.p, {'headers': {}})
        saved_auth = (
            'Digest username="******", realm="realm", nonce="nonce", uri="/", '
            'response="r", opaque="o", qop="auth", nc=00000001, cnonce="c"'
            )
        self.p.headers['Authorization'] = saved_auth
        assert match(self.p, {'headers': {}}) is False
        assert match(self.p, {'headers': {'Authorization': saved_auth}})
        new_auth = (
            'Digest username="******", realm="realm", nonce="nonce", uri="/", '
            'response="e", opaque="o", qop="auth", nc=00000001, cnonce="n"'
            )
        assert match(self.p, {'headers': {'Authorization': new_auth}})
        new_auth = (
            'Digest username="******", realm="realm", nonce="nonce", uri="/", '
            'response="e", opaque="o", qop="auth", nc=00000001, cnonce="n"'
            )
        assert match(self.p, {'headers': {'Authorization': new_auth}}) is False

    def test_headers_matcher(self):
        match = matchers.matcher_registry['headers'].match
        assert match(self.p, {'headers': {'User-Agent': 'betamax/test'}})
        assert match(self.p, {'headers': {'X-Sha': '6bbde0af'}}) is False

    def test_host_matcher(self):
        match = matchers.matcher_registry['host'].match
        assert match(self.p, {'uri': 'http://example.com'})
        assert match(self.p, {'uri': 'https://example.com'})
        assert match(self.p, {'uri': 'https://example.com/path'})
        assert match(self.p, {'uri': 'https://example2.com'}) is False

    def test_method_matcher(self):
        match = matchers.matcher_registry['method'].match
        assert match(self.p, {'method': 'GET'})
        assert match(self.p, {'method': 'POST'}) is False

    def test_path_matcher(self):
        match = matchers.matcher_registry['path'].match
        assert match(self.p, {'uri': 'http://example.com/path/to/end/point'})
        assert match(self.p,
                     {'uri': 'http://example.com:8000/path/to/end/point'})
        assert match(self.p,
                     {'uri': 'http://example.com:8000/path/to/end/'}) is False

    def test_query_matcher(self):
        match = matchers.matcher_registry['query'].match
        assert match(
            self.p,
            {'uri': 'http://example.com/path/to/end/point?query=string'}
        )
        assert match(
            self.p,
            {'uri': 'http://example.com/?query=string'}
        )
        self.p.url = self.alt_url
        assert match(
            self.p,
            {'uri': self.alt_url}
        )
        # Regression test (order independence)
        assert match(
            self.p,
            {'uri': 'http://example.com/?foo=bar&query=string'}
        )
        # Regression test (no query issue)
        assert match(self.p, {'uri': 'http://example.com'}) is False

    def test_uri_matcher(self):
        match = matchers.matcher_registry['uri'].match
        assert match(
            self.p,
            {'uri': 'http://example.com/path/to/end/point?query=string'}
        )
        assert match(self.p, {'uri': 'http://example.com'}) is False

    def test_uri_matcher_handles_query_strings(self):
        match = matchers.matcher_registry['uri'].match
        self.p.url = 'http://example.com/path/to?query=string&form=value'
        other_uri = 'http://example.com/path/to?form=value&query=string'
        assert match(self.p, {'uri': other_uri}) is True
コード例 #4
0
class TestMatchers(unittest.TestCase):
    def setUp(self):
        self.alt_url = ('http://example.com/path/to/end/point?query=string'
                        '&foo=bar')
        self.p = PreparedRequest()
        self.p.body = 'Foo bar'
        self.p.headers = {'User-Agent': 'betamax/test'}
        self.p.url = 'http://example.com/path/to/end/point?query=string'
        self.p.method = 'GET'
        self.p._cookies = RequestsCookieJar()

    def test_matcher_registry_has_body_matcher(self):
        assert 'body' in matchers.matcher_registry

    def test_matcher_registry_has_digest_auth_matcher(self):
        assert 'digest-auth' in matchers.matcher_registry

    def test_matcher_registry_has_headers_matcher(self):
        assert 'headers' in matchers.matcher_registry

    def test_matcher_registry_has_host_matcher(self):
        assert 'host' in matchers.matcher_registry

    def test_matcher_registry_has_method_matcher(self):
        assert 'method' in matchers.matcher_registry

    def test_matcher_registry_has_path_matcher(self):
        assert 'path' in matchers.matcher_registry

    def test_matcher_registry_has_query_matcher(self):
        assert 'query' in matchers.matcher_registry

    def test_matcher_registry_has_uri_matcher(self):
        assert 'uri' in matchers.matcher_registry

    def test_body_matcher(self):
        match = matchers.matcher_registry['body'].match
        assert match(
            self.p, {
                'body': 'Foo bar',
                'headers': {
                    'User-Agent': 'betamax/test'
                },
                'uri': 'http://example.com/path/to/end/point?query=string',
                'method': 'GET',
            })
        assert match(
            self.p, {
                'body': '',
                'headers': {
                    'User-Agent': 'betamax/test'
                },
                'uri': 'http://example.com/path/to/end/point?query=string',
                'method': 'GET',
            }) is False

    def test_body_matcher_without_body(self):
        p = self.p.copy()
        p.body = None
        match = matchers.matcher_registry['body'].match
        assert match(
            p, {
                'body': 'Foo bar',
                'headers': {
                    'User-Agent': 'betamax/test'
                },
                'uri': 'http://example.com/path/to/end/point?query=string',
                'method': 'GET',
            }) is False
        assert match(
            p, {
                'body': '',
                'headers': {
                    'User-Agent': 'betamax/test'
                },
                'uri': 'http://example.com/path/to/end/point?query=string',
                'method': 'GET',
            })

    def test_digest_matcher(self):
        match = matchers.matcher_registry['digest-auth'].match
        assert match(self.p, {'headers': {}})
        saved_auth = (
            'Digest username="******", realm="realm", nonce="nonce", uri="/", '
            'response="r", opaque="o", qop="auth", nc=00000001, cnonce="c"')
        self.p.headers['Authorization'] = saved_auth
        assert match(self.p, {'headers': {}}) is False
        assert match(self.p, {'headers': {'Authorization': saved_auth}})
        new_auth = (
            'Digest username="******", realm="realm", nonce="nonce", uri="/", '
            'response="e", opaque="o", qop="auth", nc=00000001, cnonce="n"')
        assert match(self.p, {'headers': {'Authorization': new_auth}})
        new_auth = (
            'Digest username="******", realm="realm", nonce="nonce", uri="/", '
            'response="e", opaque="o", qop="auth", nc=00000001, cnonce="n"')
        assert match(self.p, {'headers': {'Authorization': new_auth}}) is False

    def test_headers_matcher(self):
        match = matchers.matcher_registry['headers'].match
        assert match(self.p, {'headers': {'User-Agent': 'betamax/test'}})
        assert match(self.p, {'headers': {'X-Sha': '6bbde0af'}}) is False

    def test_host_matcher(self):
        match = matchers.matcher_registry['host'].match
        assert match(self.p, {'uri': 'http://example.com'})
        assert match(self.p, {'uri': 'https://example.com'})
        assert match(self.p, {'uri': 'https://example.com/path'})
        assert match(self.p, {'uri': 'https://example2.com'}) is False

    def test_method_matcher(self):
        match = matchers.matcher_registry['method'].match
        assert match(self.p, {'method': 'GET'})
        assert match(self.p, {'method': 'POST'}) is False

    def test_path_matcher(self):
        match = matchers.matcher_registry['path'].match
        assert match(self.p, {'uri': 'http://example.com/path/to/end/point'})
        assert match(self.p,
                     {'uri': 'http://example.com:8000/path/to/end/point'})
        assert match(self.p,
                     {'uri': 'http://example.com:8000/path/to/end/'}) is False

    def test_query_matcher(self):
        match = matchers.matcher_registry['query'].match
        assert match(
            self.p,
            {'uri': 'http://example.com/path/to/end/point?query=string'})
        assert match(self.p, {'uri': 'http://example.com/?query=string'})
        self.p.url = self.alt_url
        assert match(self.p, {'uri': self.alt_url})
        # Regression test (order independence)
        assert match(self.p,
                     {'uri': 'http://example.com/?foo=bar&query=string'})
        # Regression test (no query issue)
        assert match(self.p, {'uri': 'http://example.com'}) is False

    def test_uri_matcher(self):
        match = matchers.matcher_registry['uri'].match
        assert match(
            self.p,
            {'uri': 'http://example.com/path/to/end/point?query=string'})
        assert match(self.p, {'uri': 'http://example.com'}) is False

    def test_uri_matcher_handles_query_strings(self):
        match = matchers.matcher_registry['uri'].match
        self.p.url = 'http://example.com/path/to?query=string&form=value'
        other_uri = 'http://example.com/path/to?form=value&query=string'
        assert match(self.p, {'uri': other_uri}) is True