test(error_handling): cover _json_safe byte decoding branches

This commit is contained in:
Abhimanyu Saharan
2026-02-12 14:44:18 +00:00
parent 75abe0623f
commit 4c9ef7fdf2

View File

@@ -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"