1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

Merge pull request #386 from valgur/unicode-match-on-body

Fix matching on 'body' failing when Unicode symbols are present
This commit is contained in:
Arthur Hamon
2019-07-02 12:33:21 +02:00
committed by GitHub
4 changed files with 32 additions and 8 deletions

View File

@@ -282,3 +282,17 @@ def test_filter_post_params(tmpdir, httpbin_both):
requests.post(url, data={'key': 'value'})
with vcr.use_cassette(cass_loc, filter_post_data_parameters=['key']) as cass:
assert b'key=value' not in cass.requests[0].body
def test_post_unicode_match_on_body(tmpdir, httpbin_both):
'''Ensure that matching on POST body that contains Unicode characters works.'''
data = {'key1': 'value1', '●‿●': '٩(●̮̮̃•̃)۶'}
url = httpbin_both + '/post'
with vcr.use_cassette(str(tmpdir.join('requests.yaml')), additional_matchers=('body',)):
req1 = requests.post(url, data).content
with vcr.use_cassette(str(tmpdir.join('requests.yaml')), additional_matchers=('body',)):
req2 = requests.post(url, data).content
assert req1 == req2

View File

@@ -81,16 +81,23 @@ def test_amazon_doctype(tmpdir):
assert 'html' in r.text
def start_rpc_server(q):
httpd = xmlrpc_server.SimpleXMLRPCServer(('127.0.0.1', 0))
httpd.register_function(pow)
q.put('http://{}:{}'.format(*httpd.server_address))
httpd.serve_forever()
@pytest.yield_fixture(scope='session')
def rpc_server():
httpd = xmlrpc_server.SimpleXMLRPCServer(('', 0))
httpd.register_function(pow)
q = multiprocessing.Queue()
proxy_process = multiprocessing.Process(
target=httpd.serve_forever,
target=start_rpc_server,
args=(q,)
)
try:
proxy_process.start()
yield 'http://{}:{}'.format(*httpd.server_address)
yield q.get()
finally:
proxy_process.terminate()

View File

@@ -147,7 +147,7 @@ def test_vcr_path_transformer():
# and it should still work with cassette_library_dir
vcr = VCR(cassette_library_dir='/foo')
with vcr.use_cassette('test') as cassette:
assert cassette._path == '/foo/test'
assert os.path.abspath(cassette._path) == os.path.abspath('/foo/test')
@pytest.fixture

View File

@@ -68,9 +68,12 @@ def _transform_json(body):
_xml_header_checker = _header_checker('text/xml')
_xmlrpc_header_checker = _header_checker('xmlrpc', header='User-Agent')
_checker_transformer_pairs = (
(_header_checker('application/x-www-form-urlencoded'), urllib.parse.parse_qs),
(_header_checker('application/json'), _transform_json),
(lambda request: _xml_header_checker(request) and _xmlrpc_header_checker(request), xmlrpc_client.loads),
(_header_checker('application/x-www-form-urlencoded'),
lambda body: urllib.parse.parse_qs(body.decode('ascii'))),
(_header_checker('application/json'),
_transform_json),
(lambda request: _xml_header_checker(request) and _xmlrpc_header_checker(request),
xmlrpc_client.loads),
)