Skip to content

Commit

Permalink
Merge branch 'feature/44-show-content-size' of github.com:maykinmedia…
Browse files Browse the repository at this point in the history
…/django-log-outgoing-requests into feature/44-show-content-size
  • Loading branch information
danielmursa-dev committed Nov 7, 2024
2 parents 19182b4 + 5f69a48 commit d453a96
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 10 deletions.
3 changes: 2 additions & 1 deletion log_outgoing_requests/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,9 @@ def request_body(self, obj) -> str:
def response_body(self, obj) -> str:
return obj.response_body_decoded or "-"

@admin.display(description=_("Response content length"))
def response_content_length(self, obj):
return obj.response_content_length or "-"
return obj.response_content_length

def truncated_url(self, obj):
parsed_url = urlparse(obj.url)
Expand Down
12 changes: 3 additions & 9 deletions log_outgoing_requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,11 @@ def response_body_decoded(self) -> str:
@cached_property
def response_content_length(self) -> str:
"""
Get Response content length
Try `Content-Length` header first. If not present, try to
determine the size by reading `len(body)`.
Get Response content length by reading `len(body)`.
"""
content_length = loads_headers(self.res_headers).get("Content-Length", "")
if not content_length and self.response_body_decoded:
content_length = str(len(self.response_body_decoded))
return content_length
return str(len(self.response_body_decoded))

response_content_length.short_description = _("Content length") # type: ignore
response_content_length.short_description = _("Response content length") # type: ignore


def get_default_max_content_length():
Expand Down
57 changes: 57 additions & 0 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,60 @@ 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(admin_client):
"""Assert that the length of the content of the response is empty"""

log = OutgoingRequestsLog.objects.create(
id=1,
req_body=b"",
res_body=b"",
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 == "-"
assert response_body == "-"
assert content_length == "0"


@pytest.mark.django_db
def test_response_content_length_displayed(admin_client):
"""Assert the length of the content of the response is displayed"""

log = OutgoingRequestsLog.objects.create(
id=1,
req_body=b"Test request",
res_body=b"Test Response",
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"
assert response_body == "Test Response"
assert content_length == "13"
assert content_length == log.response_content_length

0 comments on commit d453a96

Please sign in to comment.