1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-09 01:03:24 +00:00

Allow filtering post params in requests

This commit is contained in:
Jonathan
2015-06-24 16:23:00 +01:00
parent 731a33a79a
commit ccc1ccaa0e
2 changed files with 18 additions and 2 deletions

View File

@@ -217,3 +217,16 @@ def test_post_file(tmpdir, scheme):
with open('tox.ini', 'rb') as f:
new_response = requests.post(url, f).content
assert original_response == new_response
def test_filter_post_params(tmpdir, scheme):
'''
This tests the issue in https://github.com/kevin1024/vcrpy/issues/158
Ensure that a post request made through requests can still be filtered.
'''
url = scheme + '://httpbin.org/post'
cass_loc = str(tmpdir.join('filter_post_params.yaml'))
with vcr.use_cassette(cass_loc, filter_post_data_parameters=['key']) as cass:
requests.post(url, data={'key': 'value'})

View File

@@ -1,4 +1,4 @@
from six import BytesIO
from six import BytesIO, text_type
from six.moves.urllib.parse import urlparse, urlencode, urlunparse
try:
from collections import OrderedDict
@@ -41,7 +41,10 @@ def remove_post_data_parameters(request, post_data_parameters_to_remove):
request.body = json.dumps(json_data).encode('utf-8')
else:
post_data = OrderedDict()
for k, sep, v in [p.partition(b'=') for p in request.body.split(b'&')]:
if isinstance(request.body, text_type):
request.body = request.body.encode('utf-8')
for k, sep, v in (p.partition(b'=') for p in request.body.split(b'&')):
if k in post_data:
post_data[k].append(v)
elif len(k) > 0 and k.decode('utf-8') not in post_data_parameters_to_remove: