|
3 | 3 | # pylint: disable=unused-variable
|
4 | 4 |
|
5 | 5 | import itertools
|
| 6 | +import json |
6 | 7 |
|
7 | 8 | import pytest
|
8 | 9 | from aiohttp import web
|
|
15 | 16 | HTTPNotModified,
|
16 | 17 | HTTPOk,
|
17 | 18 | )
|
| 19 | +from common_library.error_codes import ErrorCodeStr, create_error_code |
18 | 20 | from servicelib.aiohttp import status
|
19 | 21 | from servicelib.aiohttp.rest_responses import create_http_error, exception_to_response
|
20 | 22 | from servicelib.aiohttp.web_exceptions_extension import (
|
@@ -58,26 +60,40 @@ def test_collected_http_errors_map(status_code: int, http_error_cls: type[HTTPEr
|
58 | 60 |
|
59 | 61 |
|
60 | 62 | @pytest.mark.parametrize("skip_details", [True, False])
|
61 |
| -def tests_exception_to_response(skip_details: bool): |
62 |
| - exception = create_http_error( |
63 |
| - errors=[RuntimeError("foo")], |
64 |
| - reason="Something whent wrong", |
| 63 | +@pytest.mark.parametrize("error_code", [None, create_error_code(Exception("fake"))]) |
| 64 | +def tests_exception_to_response(skip_details: bool, error_code: ErrorCodeStr | None): |
| 65 | + |
| 66 | + expected_reason = "Something whent wrong !" |
| 67 | + expected_exceptions: list[Exception] = [RuntimeError("foo")] |
| 68 | + |
| 69 | + http_error = create_http_error( |
| 70 | + errors=expected_exceptions, |
| 71 | + reason=expected_reason, |
65 | 72 | http_error_cls=web.HTTPInternalServerError,
|
66 | 73 | skip_internal_error_details=skip_details,
|
| 74 | + error_code=error_code, |
67 | 75 | )
|
68 | 76 |
|
69 | 77 | # For now until deprecated SEE https://github.com/aio-libs/aiohttp/issues/2415
|
70 |
| - assert isinstance(exception, Exception) |
71 |
| - assert isinstance(exception, web.Response) |
72 |
| - assert hasattr(exception, "__http_exception__") |
| 78 | + assert isinstance(http_error, Exception) |
| 79 | + assert isinstance(http_error, web.Response) |
| 80 | + assert hasattr(http_error, "__http_exception__") |
73 | 81 |
|
74 | 82 | # until they have exception.make_response(), we user
|
75 |
| - response = exception_to_response(exception) |
| 83 | + response = exception_to_response(http_error) |
76 | 84 | assert isinstance(response, web.Response)
|
77 | 85 | assert not isinstance(response, Exception)
|
78 | 86 | assert not hasattr(response, "__http_exception__")
|
79 | 87 |
|
| 88 | + # checks response components |
80 | 89 | assert response.content_type == MIMETYPE_APPLICATION_JSON
|
81 | 90 | assert response.status == status.HTTP_500_INTERNAL_SERVER_ERROR
|
82 | 91 | assert response.text
|
83 | 92 | assert response.body
|
| 93 | + |
| 94 | + # checks response model |
| 95 | + response_json = json.loads(response.text) |
| 96 | + assert response_json["data"] is None |
| 97 | + assert response_json["error"]["message"] == expected_reason |
| 98 | + assert response_json["error"]["supportId"] == error_code |
| 99 | + assert response_json["error"]["status"] == response.status |
0 commit comments