|
1 | 1 | import asyncio |
| 2 | +import json |
2 | 3 |
|
3 | 4 | import httpx |
4 | 5 | import pytest |
@@ -84,15 +85,31 @@ def test_parse_declared_error_fields(text, expected): |
84 | 85 | ], |
85 | 86 | ids=["ServerError", "SDKError", "HTTPValidationError"], |
86 | 87 | ) |
87 | | -def test_error_classes_expose_declared_fields(build): |
88 | | - assert _declared(build(_response(503, json=DECLARED_BODY))) == DECLARED_FIELDS |
| 88 | +@pytest.mark.parametrize( |
| 89 | + "content", |
| 90 | + [ |
| 91 | + json.dumps(DECLARED_BODY).encode(), |
| 92 | + b"\xef\xbb\xbf" + json.dumps(DECLARED_BODY).encode(), |
| 93 | + json.dumps(DECLARED_BODY).encode("utf-16"), |
| 94 | + ], |
| 95 | + ids=["utf-8", "utf-8-bom", "utf-16"], |
| 96 | +) |
| 97 | +def test_error_classes_expose_declared_fields(build, content): |
| 98 | + response = _response(503, content=content, headers={"Content-Type": "application/json"}) |
| 99 | + |
| 100 | + assert _declared(build(response)) == DECLARED_FIELDS |
| 101 | + |
89 | 102 |
|
| 103 | +def test_unread_stream_falls_back_to_body(): |
| 104 | + class Stream(httpx.SyncByteStream): |
| 105 | + def __iter__(self): |
| 106 | + pytest.fail("error construction read an unread stream") |
| 107 | + yield b"" |
90 | 108 |
|
91 | | -def test_explicit_body_takes_precedence_over_response_text(): |
92 | | - response = _response(500, json={"detail": "x"}) |
93 | | - error = SDKError("API error occurred", response, '{"retryable":false}') |
| 109 | + response = _response(503, stream=Stream()) |
| 110 | + error = SDKError("API error occurred", response, json.dumps(DECLARED_BODY)) |
94 | 111 |
|
95 | | - assert error.retryable is False |
| 112 | + assert _declared(error) == DECLARED_FIELDS |
96 | 113 |
|
97 | 114 |
|
98 | 115 | def test_success_response_declares_nothing(): |
|
0 commit comments