示例#1
0
def test_headers():
    headers = {'X-Header1': ['h1'], 'X-Header2': 'h2'}
    req = Request('GET', 'http://go.com/', '', headers)
    assert req.headers == {'X-Header1': 'h1', 'X-Header2': 'h2'}

    req.add_header('X-Header1', 'h11')
    assert req.headers == {'X-Header1': 'h11', 'X-Header2': 'h2'}
示例#2
0
 def request(self, method, url, body=None, headers=None):
     '''Persist the request metadata in self._vcr_request'''
     self._vcr_request = Request(method=method,
                                 uri=self._uri(url),
                                 body=body,
                                 headers=headers or {})
     log.debug('Got {0}'.format(self._vcr_request))
示例#3
0
def test_headers():
    headers = {'X-Header1': ['h1'], 'X-Header2': 'h2'}
    req = Request('GET', 'http://go.com/', '', headers)
    assert req.headers == {'X-Header1': 'h1', 'X-Header2': 'h2'}

    req.add_header('X-Header1', 'h11')
    assert req.headers == {'X-Header1': 'h11', 'X-Header2': 'h2'}
示例#4
0
async def record_responses(cassette, vcr_request, response):
    """Because aiohttp follows redirects by default, we must support
        them by default. This method is used to write individual
        request-response chains that were implicitly followed to get
        to the final destination.
    """

    for past_response in response.history:
        aiohttp_request = past_response.request_info

        # No data because it's following a redirect.
        past_request = Request(
            aiohttp_request.method,
            str(aiohttp_request.url),
            None,
            _serialize_headers(aiohttp_request.headers),
        )
        await record_response(cassette, past_request, past_response)

    # If we're following redirects, then the last request-response
    # we record is the one attached to the `response`.
    if response.history:
        aiohttp_request = response.request_info
        vcr_request = Request(
            aiohttp_request.method,
            str(aiohttp_request.url),
            None,
            _serialize_headers(aiohttp_request.headers),
        )

    await record_response(cassette, vcr_request, response)
示例#5
0
def _before_record_request(request: VcrRequest) -> VcrRequest:
    """This function formats and cleans request data to make it
    more readable and idempotent when re-snapshotting"""

    request.uri = re.sub(r"\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}",
                         "2000-01-01_00-00-00", request.uri)

    try:
        body_str = request.body.decode("utf-8")
    except Exception:
        body_str = None

    if "content-type" in request.headers:
        request.headers["content-type"] = re.sub(
            r"boundary=[0-9a-f]+",
            "boundary=fffffff0000000",
            request.headers["content-type"],
        )
        if (request.headers["content-type"].startswith("multipart/form-data")
                and body_str is not None):
            for regex_to_replace, replace_with in _REPLACE_IN_REQUEST_MULTIFORM.items(
            ):
                body_str = re.sub(regex_to_replace, replace_with, body_str)

    if body_str is not None:
        for regex_to_replace, replace_with in _REPLACE_IN_REQUEST_CONTENT.items(
        ):
            body_str = re.sub(regex_to_replace, replace_with, body_str)
        request.body = body_str
    return request
示例#6
0
def test_replace_dict_post_data_parameters():
    # This tests all of:
    #   1. keeping a parameter
    #   2. removing a parameter
    #   3. replacing a parameter
    #   4. replacing a parameter using a callable
    #   5. removing a parameter using a callable
    #   6. replacing a parameter that doesn't exist
    body = {
        "one": "keep",
        "two": "lose",
        "three": "change",
        "four": "shout",
        "five": "whisper"
    }
    request = Request("POST", "http://google.com", body, {})
    request.headers["Content-Type"] = "application/x-www-form-urlencoded"
    replace_post_data_parameters(
        request,
        [
            ("two", None),
            ("three", "tada"),
            ("four", lambda key, value, request: value.upper()),
            ("five", lambda key, value, request: None),
            ("six", "doesntexist"),
        ],
    )
    expected_data = {"one": "keep", "three": "tada", "four": "SHOUT"}
    assert request.body == expected_data
示例#7
0
def test_headers():
    headers = {"X-Header1": ["h1"], "X-Header2": "h2"}
    req = Request("GET", "http://go.com/", "", headers)
    assert req.headers == {"X-Header1": "h1", "X-Header2": "h2"}

    req.add_header("X-Header1", "h11")
    assert req.headers == {"X-Header1": "h11", "X-Header2": "h2"}
示例#8
0
def test_remove_json_post_data_parameters():
    body = b'{"id": "secret", "foo": "bar", "baz": "qux"}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    remove_post_data_parameters(request, ['id'])
    request_body_json = json.loads(request.body.decode('utf-8'))
    expected_json = json.loads(b'{"foo": "bar", "baz": "qux"}'.decode('utf-8'))
    assert request_body_json == expected_json
示例#9
0
def test_remove_dict_post_data_parameters():
    # Test the backward-compatible API wrapper.
    body = {"id": "secret", "foo": "bar", "baz": "qux"}
    request = Request("POST", "http://google.com", body, {})
    request.headers["Content-Type"] = "application/x-www-form-urlencoded"
    remove_post_data_parameters(request, ["id"])
    expected_data = {"foo": "bar", "baz": "qux"}
    assert request.body == expected_data
示例#10
0
def test_remove_json_post_data_parameters():
    body = b'{"id": "secret", "foo": "bar", "baz": "qux"}'
    request = Request('POST', 'http://google.com', body, {})
    request.add_header('Content-Type', 'application/json')
    remove_post_data_parameters(request, ['id'])
    request_body_json = json.loads(request.body.decode('utf-8'))
    expected_json = json.loads(b'{"foo": "bar", "baz": "qux"}'.decode('utf-8'))
    assert request_body_json == expected_json
示例#11
0
def test_remove_json_post_data_parameters():
    # Test the backward-compatible API wrapper.
    body = b'{"id": "secret", "foo": "bar", "baz": "qux"}'
    request = Request("POST", "http://google.com", body, {})
    request.headers["Content-Type"] = "application/json"
    remove_post_data_parameters(request, ["id"])
    request_body_json = json.loads(request.body.decode("utf-8"))
    expected_json = json.loads(b'{"foo": "bar", "baz": "qux"}'.decode("utf-8"))
    assert request_body_json == expected_json
示例#12
0
def test_remove_json_post_data_parameters():
    # Test the backward-compatible API wrapper.
    body = b'{"id": "secret", "foo": "bar", "baz": "qux"}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    remove_post_data_parameters(request, ['id'])
    request_body_json = json.loads(request.body.decode('utf-8'))
    expected_json = json.loads(b'{"foo": "bar", "baz": "qux"}'.decode('utf-8'))
    assert request_body_json == expected_json
示例#13
0
 def putrequest(self, method, url, *args, **kwargs):
     """
     httplib gives you more than one way to do it.  This is a way
     to start building up a request.  Usually followed by a bunch
     of putheader() calls.
     """
     self._vcr_request = Request(method=method,
                                 uri=self._uri(url),
                                 body="",
                                 headers={})
     log.debug('Got {0}'.format(self._vcr_request))
示例#14
0
def test_record_errors_to_cassette(temp_yaml_file, record_errors):
    custom_persister = CustomPersister()

    cassette_request = Request(
        **{
            'method': 'GET',
            'uri': 'http://127.0.0.1:8080/test',
            'body': None,
            'headers': {
                'User-Agent': 'python-requests/2.26.0',
                'Accept-Encoding': 'gzip, deflate',
                'Accept': '*/*',
                'Connection': 'keep-alive'
            }
        })
    cassette_response = {
        'status': {
            'code': 401,
            'message': 'Unauthorized'
        },
        'headers': {
            'Server': ['http-kit'],
            'Content-Length': ['19'],
            'Date': ['Fri, 15 Oct 2021 10:41:06 GMT']
        },
        'body': {
            'string': b'Unauthorized access'
        }
    }

    cassette_request.uri = 'http://127.0.0.1:8080/test'
    cassette_dict = {
        "requests": [cassette_request],
        "responses": [cassette_response]
    }
    CustomPersister.record_errors = record_errors
    CustomPersister.base_uri = "http://127.0.0.1:8080"
    CustomPersister.mock_url = "http://cornell"
    custom_persister.save_cassette(temp_yaml_file, cassette_dict,
                                   yamlserializer)
    output = open(temp_yaml_file, encoding="utf-8")
    if record_errors:
        assert yaml.safe_load(output)["interactions"] == [{
            "request":
            cassette_request._to_dict(),  # pylint: disable=protected-access
            "response":
            cassette_response
        }]
    else:
        assert output.read() == "0"
示例#15
0
def test_replace_headers():
    # This tests all of:
    #   1. keeping a header
    #   2. removing a header
    #   3. replacing a header
    #   4. replacing a header using a callable
    #   5. removing a header using a callable
    #   6. replacing a header that doesn't exist
    headers = {
        "one": ["keep"],
        "two": ["lose"],
        "three": ["change"],
        "four": ["shout"],
        "five": ["whisper"]
    }
    request = Request("GET", "http://google.com", "", headers)
    replace_headers(
        request,
        [
            ("two", None),
            ("three", "tada"),
            ("four", lambda key, value, request: value.upper()),
            ("five", lambda key, value, request: None),
            ("six", "doesntexist"),
        ],
    )
    assert request.headers == {"one": "keep", "three": "tada", "four": "SHOUT"}
示例#16
0
def test_serialize_empty_request():
    request = Request(method="POST",
                      uri="http://localhost/",
                      body="",
                      headers={})

    serialize({"requests": [request], "responses": [{}]}, jsonserializer)
示例#17
0
def test_serialize_json_request():
    request = Request(method="POST",
                      uri="http://localhost/",
                      body="{'hello': 'world'}",
                      headers={})

    serialize({"requests": [request], "responses": [{}]}, jsonserializer)
示例#18
0
def play_responses(cassette, vcr_request, kwargs):
    history = []
    allow_redirects = kwargs.get("allow_redirects", True)
    vcr_response = cassette.play_response(vcr_request)
    response = build_response(vcr_request, vcr_response, history)

    # If we're following redirects, continue playing until we reach
    # our final destination.
    while allow_redirects and 300 <= response.status <= 399:
        if "location" not in response.headers:
            break

        next_url = URL(response.url).join(URL(response.headers["location"]))

        # Make a stub VCR request that we can then use to look up the recorded
        # VCR request saved to the cassette. This feels a little hacky and
        # may have edge cases based on the headers we're providing (e.g. if
        # there's a matcher that is used to filter by headers).
        vcr_request = Request("GET", str(next_url), None, _serialize_headers(response.request_info.headers))
        vcr_requests = cassette.find_requests_with_most_matches(vcr_request)
        for vcr_request, *_ in vcr_requests:
            if cassette.can_play_response_for(vcr_request):
                break

        # Tack on the response we saw from the redirect into the history
        # list that is added on to the final response.
        history.append(response)
        vcr_response = cassette.play_response(vcr_request)
        response = build_response(vcr_request, vcr_response, history)

    return response
示例#19
0
def test_replace_headers():
    # This tests all of:
    #   1. keeping a header
    #   2. removing a header
    #   3. replacing a header
    #   4. replacing a header using a callable
    #   5. removing a header using a callable
    #   6. replacing a header that doesn't exist
    headers = {
        'one': ['keep'],
        'two': ['lose'],
        'three': ['change'],
        'four': ['shout'],
        'five': ['whisper'],
    }
    request = Request('GET', 'http://google.com', '', headers)
    replace_headers(request, [
        ('two', None),
        ('three', 'tada'),
        ('four', lambda key, value, request: value.upper()),
        ('five', lambda key, value, request: None),
        ('six', 'doesntexist'),
    ])
    assert request.headers == {
        'one': 'keep',
        'three': 'tada',
        'four': 'SHOUT',
    }
示例#20
0
def play_responses(cassette, vcr_request):
    history = []
    vcr_response = cassette.play_response(vcr_request)
    response = build_response(vcr_request, vcr_response, history)

    # If we're following redirects, continue playing until we reach
    # our final destination.
    while 300 <= response.status <= 399:
        new_location = response.headers["location"]
        potential_next_url = URL(new_location)
        next_url = (potential_next_url if potential_next_url.is_absolute() else
                    URL(response.url).with_path(new_location))

        # Make a stub VCR request that we can then use to look up the recorded
        # VCR request saved to the cassette. This feels a little hacky and
        # may have edge cases based on the headers we're providing (e.g. if
        # there's a matcher that is used to filter by headers).
        vcr_request = Request(
            "GET", str(next_url), None,
            _serialize_headers(response.request_info.headers))
        vcr_request = cassette.find_requests_with_most_matches(
            vcr_request)[0][0]

        # Tack on the response we saw from the redirect into the history
        # list that is added on to the final response.
        history.append(response)
        vcr_response = cassette.play_response(vcr_request)
        response = build_response(vcr_request, vcr_response, history)

    return response
示例#21
0
def test_vcr_before_record_request_params():
    base_path = 'http://httpbin.org/'
    def before_record_cb(request):
        if request.path != '/get':
            return request
    test_vcr = VCR(filter_headers=('cookie',), before_record_request=before_record_cb,
                   ignore_hosts=('www.test.com',), ignore_localhost=True,
                   filter_query_parameters=('foo',))

    with test_vcr.use_cassette('test') as cassette:
        assert cassette.filter_request(Request('GET', base_path + 'get', '', {})) is None
        assert cassette.filter_request(Request('GET', base_path + 'get2', '', {})) is not None

        assert cassette.filter_request(Request('GET', base_path + '?foo=bar', '', {})).query == []
        assert cassette.filter_request(
            Request('GET', base_path + '?foo=bar', '',
                    {'cookie': 'test', 'other': 'fun'})).headers == {'other': 'fun'}
        assert cassette.filter_request(Request('GET', base_path + '?foo=bar', '',
                                               {'cookie': 'test', 'other': 'fun'})).headers == {'other': 'fun'}

        assert cassette.filter_request(Request('GET', 'http://www.test.com' + '?foo=bar', '',
                                               {'cookie': 'test', 'other': 'fun'})) is None

    with test_vcr.use_cassette('test', before_record_request=None) as cassette:
        # Test that before_record can be overwritten with
        assert cassette.filter_request(Request('GET', base_path + 'get', '', {})) is not None
示例#22
0
 def request(self, method, url, body=None, headers=None):
     '''Persist the request metadata in self._vcr_request'''
     self._vcr_request = Request(
         method=method,
         uri=self._uri(url),
         body=body,
         headers=headers or {}
     )
     log.debug('Got {0}'.format(self._vcr_request))
示例#23
0
def remove_creds(req: request.Request) -> request.Request:
    if req.path.endswith(Resource._urls['login']) and req.method == 'POST':
        username_key = 'j_id6:j_id20'
        password_key = 'j_id6:j_id22'
        body = parse.parse_qs(req.body.decode('utf-8'))
        body[username_key] = ['USERNAME']
        body[password_key] = ['PASSWORD']
        req.body = parse.urlencode(body)
    return req
示例#24
0
def test_serialize_empty_request():
    request = Request(
        method='POST',
        uri='http://localhost/',
        body='',
        headers={},
    )

    serialize({'requests': [request], 'responses': [{}]}, jsonserializer)
示例#25
0
def test_serialize_binary_request():
    msg = "Does this HTTP interaction contain binary data?"

    request = Request(method="POST", uri="http://localhost/", body=b"\x8c", headers={})

    try:
        serialize({"requests": [request], "responses": [{}]}, jsonserializer)
    except (UnicodeDecodeError, TypeError) as exc:
        assert msg in str(exc)
示例#26
0
def test_serialize_json_request():
    request = Request(
        method='POST',
        uri='http://localhost/',
        body="{'hello': 'world'}",
        headers={},
    )

    serialize({'requests': [request], 'responses': [{}]}, jsonserializer)
示例#27
0
def request_has_matches(cassette_path, flask_request):
    cassette_requests = Request(method=flask_request.method, uri=request.url, body=flask_request.data, headers=flask_request.headers)
    with set_underlying_vcr_logging_level():
        cassette = Cassette.load(path=cassette_path, match_on=(extended_vcr_body_matcher, extended_query_matcher,
                                                               method, extended_vcr_body_matcher))
    for matches in cassette.find_requests_with_most_matches(cassette_requests):
        *_, failure = matches
        if not failure:
            return True
    return False
示例#28
0
def test_vcr_before_record_request_params():
    base_path = "http://httpbin.org/"

    def before_record_cb(request):
        if request.path != "/get":
            return request

    test_vcr = VCR(
        filter_headers=("cookie", ("bert", "ernie")),
        before_record_request=before_record_cb,
        ignore_hosts=("www.test.com", ),
        ignore_localhost=True,
        filter_query_parameters=("foo", ("tom", "jerry")),
        filter_post_data_parameters=("posted", ("no", "trespassing")),
    )

    with test_vcr.use_cassette("test") as cassette:
        # Test explicit before_record_cb
        request_get = Request("GET", base_path + "get", "", {})
        assert cassette.filter_request(request_get) is None
        request = Request("GET", base_path + "get2", "", {})
        assert cassette.filter_request(request) is not None

        # Test filter_query_parameters
        request = Request("GET", base_path + "?foo=bar", "", {})
        assert cassette.filter_request(request).query == []
        request = Request("GET", base_path + "?tom=nobody", "", {})
        assert cassette.filter_request(request).query == [("tom", "jerry")]

        # Test filter_headers
        request = Request("GET", base_path + "?foo=bar", "", {
            "cookie": "test",
            "other": "fun",
            "bert": "nobody"
        })
        assert cassette.filter_request(request).headers == {
            "other": "fun",
            "bert": "ernie"
        }

        # Test ignore_hosts
        request = Request("GET", "http://www.test.com" + "?foo=bar", "", {
            "cookie": "test",
            "other": "fun"
        })
        assert cassette.filter_request(request) is None

        # Test ignore_localhost
        request = Request("GET", "http://localhost:8000" + "?foo=bar", "", {
            "cookie": "test",
            "other": "fun"
        })
        assert cassette.filter_request(request) is None

    with test_vcr.use_cassette("test", before_record_request=None) as cassette:
        # Test that before_record can be overwritten in context manager.
        assert cassette.filter_request(request_get) is not None
示例#29
0
 def request(self, method, url, body=None, headers=None):
     '''Persist the request metadata in self._vcr_request'''
     self._vcr_request = Request(
         protocol=self._protocol,
         host=self.host,
         port=self.port,
         method=method,
         path=url,
         body=body,
         headers=headers or {}
     )
示例#30
0
def scrub_body_request(request: Request) -> dict:
    body = request.body.decode('utf-8')
    body_dict = json.loads(body)
    try:
        body_dict['data']['systemRequest']['user'] = '******'
        body_dict['data']['systemRequest']['password'] = '******'
    except KeyError:
        pass

    request.body = json.dumps(body_dict)
    return request
示例#31
0
    def request(self, method, url, body=None, headers=None, *args, **kwargs):
        """Persist the request metadata in self._vcr_request"""
        self._vcr_request = Request(method=method, uri=self._uri(url), body=body, headers=headers or {})
        log.debug("Got {}".format(self._vcr_request))

        # Note: The request may not actually be finished at this point, so
        # I'm not sending the actual request until getresponse().  This
        # allows me to compare the entire length of the response to see if it
        # exists in the cassette.

        self._sock = VCRFakeSocket()
示例#32
0
def test_replace_query_parameters_callable():
    # This goes beyond test_replace_query_parameters() to ensure that the
    # callable receives the expected arguments.
    uri = 'http://g.com/?hey=there'
    request = Request('GET', uri, '', {})
    callme = mock.Mock(return_value='ho')
    replace_query_parameters(request, [('hey', callme)])
    assert request.uri == 'http://g.com/?hey=ho'
    assert callme.call_args == ((), {'request': request,
                                     'key': 'hey',
                                     'value': 'there'})
示例#33
0
def test_replace_headers_callable():
    # This goes beyond test_replace_headers() to ensure that the callable
    # receives the expected arguments.
    headers = {'hey': 'there'}
    request = Request('GET', 'http://google.com', '', headers)
    callme = mock.Mock(return_value='ho')
    replace_headers(request, [('hey', callme)])
    assert request.headers == {'hey': 'ho'}
    assert callme.call_args == ((), {'request': request,
                                     'key': 'hey',
                                     'value': 'there'})
示例#34
0
文件: test_vcr.py 项目: vladdyk/vcrpy
def test_vcr_before_record_request_params():
    base_path = 'http://httpbin.org/'

    def before_record_cb(request):
        if request.path != '/get':
            return request

    test_vcr = VCR(filter_headers=('cookie', ('bert', 'ernie')),
                   before_record_request=before_record_cb,
                   ignore_hosts=('www.test.com', ),
                   ignore_localhost=True,
                   filter_query_parameters=('foo', ('tom', 'jerry')),
                   filter_post_data_parameters=('posted', ('no',
                                                           'trespassing')))

    with test_vcr.use_cassette('test') as cassette:
        # Test explicit before_record_cb
        request_get = Request('GET', base_path + 'get', '', {})
        assert cassette.filter_request(request_get) is None
        request = Request('GET', base_path + 'get2', '', {})
        assert cassette.filter_request(request) is not None

        # Test filter_query_parameters
        request = Request('GET', base_path + '?foo=bar', '', {})
        assert cassette.filter_request(request).query == []
        request = Request('GET', base_path + '?tom=nobody', '', {})
        assert cassette.filter_request(request).query == [('tom', 'jerry')]

        # Test filter_headers
        request = Request('GET', base_path + '?foo=bar', '', {
            'cookie': 'test',
            'other': 'fun',
            'bert': 'nobody'
        })
        assert (cassette.filter_request(request).headers == {
            'other': 'fun',
            'bert': 'ernie'
        })

        # Test ignore_hosts
        request = Request('GET', 'http://www.test.com' + '?foo=bar', '', {
            'cookie': 'test',
            'other': 'fun'
        })
        assert cassette.filter_request(request) is None

        # Test ignore_localhost
        request = Request('GET', 'http://localhost:8000' + '?foo=bar', '', {
            'cookie': 'test',
            'other': 'fun'
        })
        assert cassette.filter_request(request) is None

    with test_vcr.use_cassette('test', before_record_request=None) as cassette:
        # Test that before_record can be overwritten in context manager.
        assert cassette.filter_request(request_get) is not None
示例#35
0
文件: __init__.py 项目: ghotiv/vcrpy
 def request(self, method, url, body=None, headers=None):
     '''Persist the request metadata in self._vcr_request'''
     self._vcr_request = Request(
         protocol=self._protocol,
         host=self.real_connection.host,
         port=self.real_connection.port,
         method=method,
         path=url,
         body=body,
         headers=headers or {}
     )
     log.debug('Got {0}'.format(self._vcr_request))
示例#36
0
def test_replace_json_post_data_parameters():
    # This tests all of:
    #   1. keeping a parameter
    #   2. removing a parameter
    #   3. replacing a parameter
    #   4. replacing a parameter using a callable
    #   5. removing a parameter using a callable
    #   6. replacing a parameter that doesn't exist
    body = b'{"one": "keep", "two": "lose", "three": "change", "four": "shout", "five": "whisper"}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    replace_post_data_parameters(request, [
        ('two', None),
        ('three', 'tada'),
        ('four', lambda key, value, request: value.upper()),
        ('five', lambda key, value, request: None),
        ('six', 'doesntexist'),
    ])
    request_data = json.loads(request.body.decode('utf-8'))
    expected_data = json.loads('{"one": "keep", "three": "tada", "four": "SHOUT"}')
    assert request_data == expected_data
示例#37
0
 def putrequest(self, method, url, *args, **kwargs):
     """
     httplib gives you more than one way to do it.  This is a way
     to start building up a request.  Usually followed by a bunch
     of putheader() calls.
     """
     self._vcr_request = Request(
         method=method,
         uri=self._uri(url),
         body="",
         headers={}
     )
     log.debug('Got {0}'.format(self._vcr_request))
示例#38
0
文件: __init__.py 项目: aah/vcrpy
 def putrequest(self, method, url, *args, **kwargs):
     """
     httplib gives you more than one way to do it.  This is a way
     to start building up a request.  Usually followed by a bunch
     of putheader() calls.
     """
     self._vcr_request = Request(
         protocol=self._protocol,
         host=self.real_connection.host,
         port=self.real_connection.port,
         method=method,
         path=url,
         body="",
         headers={}
     )
示例#39
0
def deserialize(cassette_string, serializer):
    try:
        data = serializer.deserialize(cassette_string)
    # Old cassettes used to use yaml object thingy so I have to
    # check for some fairly stupid exceptions here
    except (ImportError, yaml.constructor.ConstructorError):
        _warn_about_old_cassette_format()
    if _looks_like_an_old_cassette(data):
        _warn_about_old_cassette_format()

    requests = [Request._from_dict(r['request']) for r in data['interactions']]
    responses = [
        compat.convert_to_bytes(r['response']) for r in data['interactions']
    ]
    return requests, responses
示例#40
0
def deserialize(cassette_string):
    data = json.loads(cassette_string)
    requests = [Request._from_dict(r['request']) for r in data]
    responses = [_fix_response_unicode(r['response']) for r in data]
    return requests, responses
示例#41
0
文件: __init__.py 项目: aah/vcrpy
class VCRConnection:
    # A reference to the cassette that's currently being patched in
    cassette = None

    def request(self, method, url, body=None, headers=None):
        '''Persist the request metadata in self._vcr_request'''

        self._vcr_request = Request(
            protocol=self._protocol,
            host=self.real_connection.host,
            port=self.real_connection.port,
            method=method,
            path=url,
            body=body,
            headers=headers or {}
        )

        # Note: The request may not actually be finished at this point, so
        # I'm not sending the actual request until getresponse().  This
        # allows me to compare the entire length of the response to see if it
        # exists in the cassette.

    def putrequest(self, method, url, *args, **kwargs):
        """
        httplib gives you more than one way to do it.  This is a way
        to start building up a request.  Usually followed by a bunch
        of putheader() calls.
        """
        self._vcr_request = Request(
            protocol=self._protocol,
            host=self.real_connection.host,
            port=self.real_connection.port,
            method=method,
            path=url,
            body="",
            headers={}
        )

    def putheader(self, header, *values):
        for value in values:
            self._vcr_request.add_header(header, value)

    def send(self, data):
        '''
        This method is called after request(), to add additional data to the
        body of the request.  So if that happens, let's just append the data
        onto the most recent request in the cassette.
        '''
        self._vcr_request.body = (self._vcr_request.body or '') + data

    def close(self):
        # Note: the real connection will only close if it's open, so
        # no need to check that here.
        self.real_connection.close()

    def endheaders(self, *args, **kwargs):
        """
        Normally, this would atually send the request to the server.
        We are not sending the request until getting the response,
        so bypass this method for now.
        """
        pass

    def getresponse(self, _=False):
        '''Retrieve a the response'''
        # Check to see if the cassette has a response for this request. If so,
        # then return it
        if self._vcr_request in self.cassette and \
                self.cassette.record_mode != "all" and \
                self.cassette.rewound:
            response = self.cassette.play_response(self._vcr_request)
            return VCRHTTPResponse(response)
        else:
            if self.cassette.write_protected:
                raise CannotOverwriteExistingCassetteException(
                    "Can't overwrite existing cassette (%r) in "
                    "your current record mode (%r)."
                    % (self.cassette._path, self.cassette.record_mode)
                )

            # Otherwise, we should send the request, then get the response
            # and return it.

            self.real_connection.request(
                method=self._vcr_request.method,
                url=self._vcr_request.path,
                body=self._vcr_request.body,
                headers=dict(self._vcr_request.headers or {})
            )

            # get the response
            response = self.real_connection.getresponse()

            # put the response into the cassette
            response = {
                'status': {
                    'code': response.status,
                    'message': response.reason
                },
                'headers': compat.get_headers(response),
                'body': {'string': response.read()},
            }
            self.cassette.append(self._vcr_request, response)
        return VCRHTTPResponse(response)

    def set_debuglevel(self, *args, **kwargs):
        self.real_connection.set_debuglevel(*args, **kwargs)

    def connect(self, *args, **kwargs):
        """
        httplib2 uses this.  Connects to the server I'm assuming.

        Only pass to the baseclass if we don't have a recorded response
        and are not write-protected.
        """

        if hasattr(self, '_vcr_request') and \
                self._vcr_request in self.cassette and \
                self.cassette.record_mode != "all" and \
                self.cassette.rewound:
            # We already have a response we are going to play, don't
            # actually connect
            return

        if self.cassette.write_protected:
            # Cassette is write-protected, don't actually connect
            return

        return self.real_connection.connect(*args, **kwargs)

    def __init__(self, *args, **kwargs):
        # need to temporarily reset here because the real connection
        # inherits from the thing that we are mocking out.  Take out
        # the reset if you want to see what I mean :)
        from vcr.patch import install, reset
        reset()
        self.real_connection = self._baseclass(*args, **kwargs)
        install(self.cassette)
示例#42
0
def test_remove_all_json_post_data_parameters():
    body = b'{"id": "secret", "foo": "bar"}'
    request = Request('POST', 'http://google.com', body, {})
    request.add_header('Content-Type', 'application/json')
    remove_post_data_parameters(request, ['id', 'foo'])
    assert request.body == b'{}'
示例#43
0
def deserialize(cassette_string):
    data = json.loads(cassette_string)
    requests = [Request._from_dict(r['request']) for r in data]
    responses = [compat.convert_to_bytes(r['response']) for r in data]
    return requests, responses
示例#44
0
class VCRConnection(object):
    # A reference to the cassette that's currently being patched in
    cassette = None

    def _port_postfix(self):
        """
        Returns empty string for the default port and ':port' otherwise
        """
        port = self.real_connection.port
        default_port = {'https': 433, 'http': 80}[self._protocol]
        return ':{0}'.format(port) if port != default_port else ''

    def _uri(self, url):
        """Returns request absolute URI"""
        uri = "{0}://{1}{2}{3}".format(
            self._protocol,
            self.real_connection.host,
            self._port_postfix(),
            url,
        )
        return uri

    def _url(self, uri):
        """Returns request selector url from absolute URI"""
        prefix = "{0}://{1}{2}".format(
            self._protocol,
            self.real_connection.host,
            self._port_postfix(),
        )
        return uri.replace(prefix, '', 1)

    def request(self, method, url, body=None, headers=None):
        '''Persist the request metadata in self._vcr_request'''
        self._vcr_request = Request(
            method=method,
            uri=self._uri(url),
            body=body,
            headers=headers or {}
        )
        log.debug('Got {0}'.format(self._vcr_request))

        # Note: The request may not actually be finished at this point, so
        # I'm not sending the actual request until getresponse().  This
        # allows me to compare the entire length of the response to see if it
        # exists in the cassette.

    def putrequest(self, method, url, *args, **kwargs):
        """
        httplib gives you more than one way to do it.  This is a way
        to start building up a request.  Usually followed by a bunch
        of putheader() calls.
        """
        self._vcr_request = Request(
            method=method,
            uri=self._uri(url),
            body="",
            headers={}
        )
        log.debug('Got {0}'.format(self._vcr_request))

    def putheader(self, header, *values):
        for value in values:
            self._vcr_request.add_header(header, value)

    def send(self, data):
        '''
        This method is called after request(), to add additional data to the
        body of the request.  So if that happens, let's just append the data
        onto the most recent request in the cassette.
        '''
        self._vcr_request.body = (self._vcr_request.body or '') + data

    def close(self):
        # Note: the real connection will only close if it's open, so
        # no need to check that here.
        self.real_connection.close()

    def endheaders(self, *args, **kwargs):
        """
        Normally, this would atually send the request to the server.
        We are not sending the request until getting the response,
        so bypass this method for now.
        """
        pass

    def getresponse(self, _=False):
        '''Retrieve the response'''
        # Check to see if the cassette has a response for this request. If so,
        # then return it
        if self.cassette.can_play_response_for(self._vcr_request):
            log.info(
                "Playing response for {0} from cassette".format(
                    self._vcr_request
                )
            )
            response = self.cassette.play_response(self._vcr_request)
            return VCRHTTPResponse(response)
        else:
            if self.cassette.write_protected and self.cassette.filter_request(self._vcr_request):
                raise CannotOverwriteExistingCassetteException(
                    "Can't overwrite existing cassette (%r) in "
                    "your current record mode (%r)."
                    % (self.cassette._path, self.cassette.record_mode)
                )

            # Otherwise, we should send the request, then get the response
            # and return it.

            log.info(
                "{0} not in cassette, sending to real server".format(
                    self._vcr_request
                )
            )
            self.real_connection.request(
                method=self._vcr_request.method,
                url=self._url(self._vcr_request.uri),
                body=self._vcr_request.body,
                headers=self._vcr_request.headers,
            )

            # get the response
            response = self.real_connection.getresponse()

            # put the response into the cassette
            response = {
                'status': {
                    'code': response.status,
                    'message': response.reason
                },
                'headers': serialize_headers(response),
                'body': {'string': response.read()},
            }
            self.cassette.append(self._vcr_request, response)
        return VCRHTTPResponse(response)

    def set_debuglevel(self, *args, **kwargs):
        self.real_connection.set_debuglevel(*args, **kwargs)

    def connect(self, *args, **kwargs):
        """
        httplib2 uses this.  Connects to the server I'm assuming.

        Only pass to the baseclass if we don't have a recorded response
        and are not write-protected.
        """

        if hasattr(self, '_vcr_request') and \
                self.cassette.can_play_response_for(self._vcr_request):
            # We already have a response we are going to play, don't
            # actually connect
            return

        if self.cassette.write_protected:
            # Cassette is write-protected, don't actually connect
            return

        return self.real_connection.connect(*args, **kwargs)

    @property
    def sock(self):
        if self.real_connection.sock:
            return self.real_connection.sock
        return VCRFakeSocket()

    @sock.setter
    def sock(self, value):
        if self.real_connection.sock:
            self.real_connection.sock = value

    def __init__(self, *args, **kwargs):
        if six.PY3:
            kwargs.pop('strict', None) # apparently this is gone in py3

        # need to temporarily reset here because the real connection
        # inherits from the thing that we are mocking out.  Take out
        # the reset if you want to see what I mean :)
        from vcr.patch import force_reset
        with force_reset():
            self.real_connection = self._baseclass(*args, **kwargs)
示例#45
0
def test_remove_nonexistent_json_post_data_parameters():
    body = b'{}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    remove_post_data_parameters(request, ['id'])
    assert request.body == b'{}'
示例#46
0
def test_remove_all_json_post_data_parameters():
    body = b'{"id": "secret", "foo": "bar"}'
    request = Request('POST', 'http://google.com', body, {})
    request.headers['Content-Type'] = 'application/json'
    replace_post_data_parameters(request, [('id', None), ('foo', None)])
    assert request.body == b'{}'