Skip to content

Commit

Permalink
Merge pull request #159 from MrJohz/master
Browse files Browse the repository at this point in the history
Fix issue #158
  • Loading branch information
colonelpanic8 committed Jun 25, 2015
2 parents 731a33a + e742d32 commit ce7ceb0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 17 additions & 0 deletions tests/integration/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,20 @@ 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.
with vcr.use_cassette(cass_file, filter_post_data_parameters=['id']) as cass:
assert b'id=secret' not in cass.requests[0].body
'''
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'})
with vcr.use_cassette(cass_loc, filter_post_data_parameters=['key']) as cass:
assert b'key=value' not in cass.requests[0].body

7 changes: 5 additions & 2 deletions vcr/filters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit ce7ceb0

Please sign in to comment.