diff --git a/tests/conftest.py b/tests/conftest.py index 0cf6fba..84041c1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -69,6 +69,25 @@ def request_mock_kwargs(): } +@pytest.fixture +def request_mock_kwargs_error(): + return { + "url": "http://example.com:8000/some-path-that-doesnt-exist?version=2.0", + "status_code": 404, + "content": b"404 Not Found", + "request_headers": { + "Authorization": "test", + "Content-Type": "application/json", + "Content-Length": "24", + }, + "headers": { + "Date": "Tue, 21 Mar 2023 15:24:08 GMT", + "Content-Type": "text/plain", + "Content-Length": "13", + }, + } + + @pytest.fixture def request_mock_kwargs_binary(): return { diff --git a/tests/test_logging.py b/tests/test_logging.py index 9c62774..a025f4f 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -111,6 +111,49 @@ def test_data_is_saved(request_mock_kwargs, request_variants, expected_headers): assert request_log.res_body_encoding == "utf-8" +@pytest.mark.django_db +@freeze_time("2021-10-18 13:00:00") +def test_data_is_saved_for_error_response( + request_mock_kwargs_error, request_variants, expected_headers +): + for method, request_func, request_mock in request_variants: + request_mock(**request_mock_kwargs_error) + response = request_func( + request_mock_kwargs_error["url"], + headers=request_mock_kwargs_error["request_headers"], + json={"test": "request data"}, + ) + + assert response.status_code == 404 + + request_log = OutgoingRequestsLog.objects.last() + + assert request_log.method == method + assert request_log.hostname == "example.com:8000" + assert request_log.params == "" + assert request_log.query_params == "version=2.0" + assert request_log.response_ms == 0 + assert request_log.trace == "" + assert str(request_log) == "example.com:8000 at 2021-10-18 13:00:00+00:00" + assert ( + request_log.timestamp.strftime("%Y-%m-%d %H:%M:%S") == "2021-10-18 13:00:00" + ) + # headers + assert request_log.req_headers == expected_headers + assert ( + request_log.res_headers == "Date: Tue, 21 Mar 2023 15:24:08 GMT\n" + "Content-Type: text/plain\nContent-Length: 13" + ) + # request body + assert request_log.req_content_type == "application/json" + assert bytes(request_log.req_body) == b'{"test": "request data"}' + assert request_log.req_body_encoding == "utf-8" + # response body + assert request_log.res_content_type == "text/plain" + assert bytes(request_log.res_body) == b"404 Not Found" + assert request_log.res_body_encoding == "utf-8" + + # # test decoding of binary content #