Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google/auth/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def _parse_request_body(body: Optional[bytes], content_type: str = "") -> Any:
if not content_type or "application/json" in content_type:
try:
return json.loads(body_str)
except (json.JSONDecodeError, TypeError):
except (TypeError, ValueError):
return body_str
if "application/x-www-form-urlencoded" in content_type:
parsed_query = urllib.parse.parse_qs(body_str)
Expand Down
28 changes: 28 additions & 0 deletions tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,34 @@ def test_parse_request_body_other_type():
assert _helpers._parse_request_body("string") is None


def test_parse_request_body_json_type_error():
body = b'{"key": "value"}'
with mock.patch("json.loads", side_effect=TypeError):
# json.loads should raise a TypeError, and the function should return the
# original string
assert _helpers._parse_request_body(body, "application/json") == body.decode(
"utf-8"
)


def test_parse_request_body_json_value_error():
body = b'{"key": "value"}'
content_type = "application/json"
with mock.patch("json.loads", side_effect=ValueError):
# json.loads should raise a ValueError, and the function should return the
# original string
assert _helpers._parse_request_body(body, content_type) == body.decode("utf-8")


def test_parse_request_body_json_decode_error():
body = b'{"key": "value"}'
content_type = "application/json"
with mock.patch("json.loads", side_effect=json.JSONDecodeError("msg", "doc", 0)):
# json.loads should raise a JSONDecodeError, and the function should return the
# original string
assert _helpers._parse_request_body(body, content_type) == body.decode("utf-8")


def test_parse_response_json_valid():
class MockResponse:
def json(self):
Expand Down