From 7c5d61077fb2aa9856595df41168724616086152 Mon Sep 17 00:00:00 2001 From: Daniel Mursa Date: Mon, 11 Nov 2024 11:42:19 +0100 Subject: [PATCH 1/2] [#44] Add response_content_length in models and admin --- log_outgoing_requests/admin.py | 3 +++ log_outgoing_requests/models.py | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/log_outgoing_requests/admin.py b/log_outgoing_requests/admin.py index ffb33a7..4b2eb5d 100644 --- a/log_outgoing_requests/admin.py +++ b/log_outgoing_requests/admin.py @@ -25,6 +25,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin): "method", "response_ms", "timestamp", + "response_content_length", ) list_filter = ("method", "timestamp", "status_code", "hostname") search_fields = ("url", "params", "hostname") @@ -57,6 +58,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin): "res_headers", "res_content_type", "res_body_encoding", + "response_content_length", "response_body", ) }, @@ -76,6 +78,7 @@ class OutgoingRequestsLogAdmin(admin.ModelAdmin): "response_ms", "res_headers", "res_content_type", + "response_content_length", "response_body", "trace", ) diff --git a/log_outgoing_requests/models.py b/log_outgoing_requests/models.py index db8f1be..a868d6e 100644 --- a/log_outgoing_requests/models.py +++ b/log_outgoing_requests/models.py @@ -174,6 +174,15 @@ def response_body_decoded(self) -> str: response_body_decoded.short_description = _("Response body") # type: ignore + @cached_property + def response_content_length(self) -> int: + """ + Get Response content length by reading `len(response_body_decoded)`. + """ + return len(self.response_body_decoded) + + response_content_length.short_description = _("Response content length") # type: ignore + def get_default_max_content_length(): """ From 23b483a52d4e10eed79fb0d417208aaefbc800c7 Mon Sep 17 00:00:00 2001 From: Daniel Mursa Date: Mon, 11 Nov 2024 11:43:01 +0100 Subject: [PATCH 2/2] [#44] Fixed tests --- tests/test_admin.py | 77 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/tests/test_admin.py b/tests/test_admin.py index 82ee05c..c49b645 100644 --- a/tests/test_admin.py +++ b/tests/test_admin.py @@ -131,3 +131,80 @@ def test_list_url_is_not_trucated_under_200_chars(admin_client): truncated_url = doc.find(".field-truncated_url").text() assert truncated_url == "/a1b2c3d4e/some-path" + + +@pytest.mark.django_db +def test_response_content_length_empty_changelist_view(admin_client): + """Assert the length of the content of the response is empty in changelist_view""" + + log = OutgoingRequestsLog.objects.create( + id=1, + req_body=b"", + res_body=b"", + timestamp=timezone.now(), + ) + + url = reverse("admin:log_outgoing_requests_outgoingrequestslog_changelist") + + response = admin_client.get(url) + assert response.status_code == 200 + + html = response.content.decode("utf-8") + doc = PyQuery(html) + content_length = doc.find(".field-response_content_length").text() + + assert content_length == "0" + assert log.response_content_length == 0 + + +@pytest.mark.django_db +def test_response_content_length_displayed_changelist_view(admin_client): + """Assert the length of the content of the response is displayed in changelist_view""" + + log = OutgoingRequestsLog.objects.create( + id=1, + req_body=b"Test request list view", + res_body=b"Test Response list view", + timestamp=timezone.now(), + ) + + url = reverse("admin:log_outgoing_requests_outgoingrequestslog_changelist") + + response = admin_client.get(url) + assert response.status_code == 200 + + html = response.content.decode("utf-8") + doc = PyQuery(html) + content_length = doc.find(".field-response_content_length").text() + + assert content_length == "23" + assert log.response_content_length == 23 + + +@pytest.mark.django_db +def test_response_content_length_displayed_change_view(admin_client): + """Assert the length of the content of the response is displayed in change_view""" + + log = OutgoingRequestsLog.objects.create( + id=1, + req_body=b"Test request list view", + res_body=b"Test Response list view", + timestamp=timezone.now(), + ) + url = reverse( + "admin:log_outgoing_requests_outgoingrequestslog_change", args=(log.pk,) + ) + + response = admin_client.get(url) + assert response.status_code == 200 + + html = response.content.decode("utf-8") + doc = PyQuery(html) + request_body = doc.find(".field-request_body .readonly").text() + response_body = doc.find(".field-response_body .readonly").text() + content_length = doc.find(".field-response_content_length .readonly").text() + + assert request_body == "Test request list view" + assert response_body == "Test Response list view" + assert content_length == "23" + assert log.response_content_length == 23