mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 01:03:24 +00:00
Merge pull request #423 from sdvicorp/keith/fix_empty_post
Fix exception when body of request is empty.
This commit is contained in:
@@ -136,6 +136,23 @@ def test_replace_post_data_parameters():
|
|||||||
assert request.body == b"one=keep&three=tada&four=SHOUT"
|
assert request.body == b"one=keep&three=tada&four=SHOUT"
|
||||||
|
|
||||||
|
|
||||||
|
def test_replace_post_data_parameters_empty_body():
|
||||||
|
# This test ensures replace_post_data_parameters doesn't throw exception when body is empty.
|
||||||
|
body = None
|
||||||
|
request = Request("POST", "http://google.com", body, {})
|
||||||
|
replace_post_data_parameters(
|
||||||
|
request,
|
||||||
|
[
|
||||||
|
("two", None),
|
||||||
|
("three", "tada"),
|
||||||
|
("four", lambda key, value, request: value.upper()),
|
||||||
|
("five", lambda key, value, request: None),
|
||||||
|
("six", "doesntexist"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
assert request.body is None
|
||||||
|
|
||||||
|
|
||||||
def test_remove_post_data_parameters():
|
def test_remove_post_data_parameters():
|
||||||
# Test the backward-compatible API wrapper.
|
# Test the backward-compatible API wrapper.
|
||||||
body = b"id=secret&foo=bar"
|
body = b"id=secret&foo=bar"
|
||||||
|
|||||||
@@ -82,6 +82,10 @@ def replace_post_data_parameters(request, replacements):
|
|||||||
3. A callable which accepts (key, value, request) and returns a string
|
3. A callable which accepts (key, value, request) and returns a string
|
||||||
value or None.
|
value or None.
|
||||||
"""
|
"""
|
||||||
|
if not request.body:
|
||||||
|
# Nothing to replace
|
||||||
|
return request
|
||||||
|
|
||||||
replacements = dict(replacements)
|
replacements = dict(replacements)
|
||||||
if request.method == "POST" and not isinstance(request.body, BytesIO):
|
if request.method == "POST" and not isinstance(request.body, BytesIO):
|
||||||
if request.headers.get("Content-Type") == "application/json":
|
if request.headers.get("Content-Type") == "application/json":
|
||||||
|
|||||||
Reference in New Issue
Block a user