From 4c9ef7fdf2507e9804df64af93335af0704d4f19 Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Thu, 12 Feb 2026 14:44:18 +0000 Subject: [PATCH] test(error_handling): cover _json_safe byte decoding branches --- backend/tests/test_error_handling.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/backend/tests/test_error_handling.py b/backend/tests/test_error_handling.py index d8f1f5ac..cf91e751 100644 --- a/backend/tests/test_error_handling.py +++ b/backend/tests/test_error_handling.py @@ -228,3 +228,19 @@ async def test_http_exception_wrapper_rejects_wrong_exception() -> None: req = Request({"type": "http", "headers": [], "state": {}}) with pytest.raises(TypeError, match="Expected StarletteHTTPException"): await _http_exception_exception_handler(req, Exception("x")) + + +def test_json_safe_covers_bytes_bytearray_and_fallback_str() -> None: + # bytes + assert error_handling._json_safe(b"\xff") == "\ufffd" + + # bytearray/memoryview + assert error_handling._json_safe(bytearray(b"\xff")) == "\ufffd" + assert error_handling._json_safe(memoryview(b"\xff")) == "\ufffd" + + # fallback to str(value) + class Weird: + def __str__(self) -> str: + return "weird" + + assert error_handling._json_safe(Weird()) == "weird"