Skip to content

Commit 575a2a3

Browse files
committed
adds tests
1 parent 9dfafa4 commit 575a2a3

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

packages/service-library/src/servicelib/aiohttp/rest_responses.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from ..mimetype_constants import MIMETYPE_APPLICATION_JSON
1111
from ..rest_constants import RESPONSE_MODEL_POLICY
1212
from ..rest_responses import is_enveloped
13-
from ..status_codes_utils import get_code_description, is_error
13+
from ..status_codes_utils import get_code_description, get_code_display_name, is_error
1414

1515

1616
class EnvelopeDict(TypedDict):
@@ -91,9 +91,8 @@ def create_http_error(
9191

9292
is_internal_error = bool(http_error_cls == web.HTTPInternalServerError)
9393

94-
status_reason = status_reason or get_code_description(http_error_cls.status_code)
94+
status_reason = status_reason or get_code_display_name(http_error_cls.status_code)
9595
error_message = error_message or get_code_description(http_error_cls.status_code)
96-
9796
assert len(status_reason) < MAX_STATUS_MESSAGE_LENGTH # nosec
9897

9998
if is_internal_error and skip_internal_error_details:

packages/service-library/tests/aiohttp/test_rest_middlewares.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,73 @@ async def test_raised_unhandled_exception(
269269

270270
# log OEC
271271
assert "OEC:" in caplog.text
272+
273+
274+
async def test_not_implemented_error_is_501(client: TestClient):
275+
"""Test that NotImplementedError is correctly mapped to HTTP 501 NOT IMPLEMENTED."""
276+
response = await client.get(
277+
"/v1/raise_exception", params={"exc": NotImplementedError.__name__}
278+
)
279+
assert response.status == status.HTTP_501_NOT_IMPLEMENTED
280+
281+
# Check that the response is properly enveloped
282+
payload = await response.json()
283+
assert is_enveloped(payload)
284+
285+
# Verify error details
286+
data, error = unwrap_envelope(payload)
287+
assert not data
288+
assert error
289+
assert error.get("status") == status.HTTP_501_NOT_IMPLEMENTED
290+
291+
292+
async def test_timeout_error_is_504(client: TestClient):
293+
"""Test that TimeoutError is correctly mapped to HTTP 504 GATEWAY TIMEOUT."""
294+
response = await client.get(
295+
"/v1/raise_exception", params={"exc": asyncio.TimeoutError.__name__}
296+
)
297+
assert response.status == status.HTTP_504_GATEWAY_TIMEOUT
298+
299+
# Check that the response is properly enveloped
300+
payload = await response.json()
301+
assert is_enveloped(payload)
302+
303+
# Verify error details
304+
data, error = unwrap_envelope(payload)
305+
assert not data
306+
assert error
307+
assert error.get("status") == status.HTTP_504_GATEWAY_TIMEOUT
308+
309+
310+
async def test_exception_in_non_api_route(client: TestClient):
311+
"""Test how exceptions are handled in routes not under the API path."""
312+
response = await client.get("/free/raise_exception")
313+
314+
# This should be a raw exception, not processed by our middleware
315+
assert response.status == status.HTTP_500_INTERNAL_SERVER_ERROR
316+
317+
# Should not be enveloped since it's outside the API path
318+
text = await response.text()
319+
try:
320+
# If it happens to be JSON, check it's not enveloped
321+
payload = json.loads(text)
322+
assert not is_enveloped(payload)
323+
except json.JSONDecodeError:
324+
# If it's not JSON, that's expected too
325+
pass
326+
327+
328+
async def test_http_ok_with_text_is_enveloped(client: TestClient):
329+
"""Test that HTTPOk with text is properly enveloped."""
330+
response = await client.get("/v1/raise_success_with_text")
331+
assert response.status == status.HTTP_200_OK
332+
333+
# Should be enveloped
334+
payload = await response.json()
335+
assert is_enveloped(payload)
336+
337+
# Check the content was preserved
338+
data, error = unwrap_envelope(payload)
339+
assert not error
340+
assert data
341+
assert data.get("ok") is True

0 commit comments

Comments
 (0)