mirror of
https://github.com/kevin1024/vcrpy.git
synced 2025-12-09 17:15:35 +00:00
Merge pull request #738 from kevin1024/json-loads-py36-plus
Make `json.loads` of Python >=3.6 decode bytes by itself
This commit is contained in:
@@ -15,7 +15,7 @@ def assert_is_json_bytes(b: bytes):
|
|||||||
assert isinstance(b, bytes)
|
assert isinstance(b, bytes)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
json.loads(b.decode("utf-8"))
|
json.loads(b)
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
raise AssertionError() from error
|
raise AssertionError() from error
|
||||||
|
|
||||||
|
|||||||
@@ -120,8 +120,8 @@ def test_original_response_is_not_modified_by_before_filter(tmpdir, httpbin):
|
|||||||
|
|
||||||
# The scrubbed field should be the same, because no cassette existed.
|
# The scrubbed field should be the same, because no cassette existed.
|
||||||
# Furthermore, the responses should be identical.
|
# Furthermore, the responses should be identical.
|
||||||
inside_body = json.loads(inside.read().decode("utf-8"))
|
inside_body = json.loads(inside.read())
|
||||||
outside_body = json.loads(outside.read().decode("utf-8"))
|
outside_body = json.loads(outside.read())
|
||||||
assert not inside_body[field_to_scrub] == replacement
|
assert not inside_body[field_to_scrub] == replacement
|
||||||
assert inside_body[field_to_scrub] == outside_body[field_to_scrub]
|
assert inside_body[field_to_scrub] == outside_body[field_to_scrub]
|
||||||
|
|
||||||
@@ -131,5 +131,5 @@ def test_original_response_is_not_modified_by_before_filter(tmpdir, httpbin):
|
|||||||
conn.request("GET", "/get")
|
conn.request("GET", "/get")
|
||||||
inside = conn.getresponse()
|
inside = conn.getresponse()
|
||||||
|
|
||||||
inside_body = json.loads(inside.read().decode("utf-8"))
|
inside_body = json.loads(inside.read())
|
||||||
assert inside_body[field_to_scrub] == replacement
|
assert inside_body[field_to_scrub] == replacement
|
||||||
|
|||||||
@@ -197,7 +197,7 @@ def test_replace_json_post_data_parameters():
|
|||||||
("six", "doesntexist"),
|
("six", "doesntexist"),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
request_data = json.loads(request.body.decode("utf-8"))
|
request_data = json.loads(request.body)
|
||||||
expected_data = json.loads('{"one": "keep", "three": "tada", "four": "SHOUT"}')
|
expected_data = json.loads('{"one": "keep", "three": "tada", "four": "SHOUT"}')
|
||||||
assert request_data == expected_data
|
assert request_data == expected_data
|
||||||
|
|
||||||
@@ -208,8 +208,8 @@ def test_remove_json_post_data_parameters():
|
|||||||
request = Request("POST", "http://google.com", body, {})
|
request = Request("POST", "http://google.com", body, {})
|
||||||
request.headers["Content-Type"] = "application/json"
|
request.headers["Content-Type"] = "application/json"
|
||||||
remove_post_data_parameters(request, ["id"])
|
remove_post_data_parameters(request, ["id"])
|
||||||
request_body_json = json.loads(request.body.decode("utf-8"))
|
request_body_json = json.loads(request.body)
|
||||||
expected_json = json.loads(b'{"foo": "bar", "baz": "qux"}'.decode("utf-8"))
|
expected_json = json.loads(b'{"foo": "bar", "baz": "qux"}')
|
||||||
assert request_body_json == expected_json
|
assert request_body_json == expected_json
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ def replace_post_data_parameters(request, replacements):
|
|||||||
new_body[k] = rv
|
new_body[k] = rv
|
||||||
request.body = new_body
|
request.body = new_body
|
||||||
elif request.headers.get("Content-Type") == "application/json":
|
elif request.headers.get("Content-Type") == "application/json":
|
||||||
json_data = json.loads(request.body.decode("utf-8"))
|
json_data = json.loads(request.body)
|
||||||
for k, rv in replacements.items():
|
for k, rv in replacements.items():
|
||||||
if k in json_data:
|
if k in json_data:
|
||||||
ov = json_data.pop(k)
|
ov = json_data.pop(k)
|
||||||
|
|||||||
@@ -73,11 +73,8 @@ def _header_checker(value, header="Content-Type"):
|
|||||||
|
|
||||||
|
|
||||||
def _transform_json(body):
|
def _transform_json(body):
|
||||||
# Request body is always a byte string, but json.loads() wants a text
|
|
||||||
# string. RFC 7159 says the default encoding is UTF-8 (although UTF-16
|
|
||||||
# and UTF-32 are also allowed: hmmmmm).
|
|
||||||
if body:
|
if body:
|
||||||
return json.loads(body.decode("utf-8"))
|
return json.loads(body)
|
||||||
|
|
||||||
|
|
||||||
_xml_header_checker = _header_checker("text/xml")
|
_xml_header_checker = _header_checker("text/xml")
|
||||||
|
|||||||
Reference in New Issue
Block a user