Skip to content

Commit

Permalink
✅ [#34] Add test for logging error response data
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenbal committed Feb 8, 2024
1 parent 8611e8f commit 3fb7733
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 54 additions & 0 deletions tests/test_logging.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Integration tests for the core functionality of the library"""

import datetime
import logging
from unittest.mock import patch

import pytest
import requests
Expand All @@ -10,6 +12,11 @@
from log_outgoing_requests.models import OutgoingRequestsLog


def set_elapsed(response, *args, **kwargs):
response.elapsed = datetime.timedelta(seconds=2)
return response


#
# Local pytest fixtures
#
Expand Down Expand Up @@ -111,6 +118,53 @@ 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)
with patch(
"requests.sessions.default_hooks", return_value={"response": [set_elapsed]}
):
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.status_code == 404
assert request_log.hostname == "example.com:8000"
assert request_log.params == ""
assert request_log.query_params == "version=2.0"
assert request_log.response_ms == 2000
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
#
Expand Down

0 comments on commit 3fb7733

Please sign in to comment.