Skip to content
Draft
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
8 changes: 7 additions & 1 deletion src/mock_vws/_services_validators/json_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ def validate_json(*, request_body: bytes, request_path: str) -> None:
return

try:
json.loads(s=request_body.decode())
request_json = json.loads(s=request_body.decode())
except JSONDecodeError as exc:
_LOGGER.warning(msg="The request body is not valid JSON.")
if request_path.endswith("/instances"):
raise BadRequestError from exc
raise FailError(status_code=HTTPStatus.BAD_REQUEST) from exc

if not isinstance(request_json, dict):
_LOGGER.warning(msg="The request body is not a JSON object.")
if request_path.endswith("/instances"):
raise BadRequestError
raise FailError(status_code=HTTPStatus.BAD_REQUEST)
9 changes: 6 additions & 3 deletions tests/mock_vws/test_invalid_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ class TestInvalidJSON:
"""Tests for giving invalid JSON to endpoints."""

@staticmethod
def test_invalid_json(endpoint: Endpoint) -> None:
"""Giving invalid JSON to endpoints returns error responses."""
content = b"a"
@pytest.mark.parametrize(
argnames="content",
argvalues=[b"a", b"[]", b'"hello"', b"5", b"null", b"true"],
)
def test_invalid_json(endpoint: Endpoint, content: bytes) -> None:
"""Giving invalid or non-object JSON returns error responses."""
gmt = ZoneInfo(key="GMT")
now = datetime.now(tz=gmt)
time_to_freeze = now
Expand Down
Loading