Skip to content

Commit

Permalink
Add body count test
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Nov 15, 2024
1 parent 47603cf commit 0db0190
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 2 additions & 3 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ def test_wrong_method_type(application, test_files):
def test_large_static_file(application, test_files):
scope = AsgiScopeEmulator({
"path": "/static/large-file.txt",
"headers": [
(b"host", b"127.0.0.1:8000"),
],
"headers": [(b"host", b"127.0.0.1:8000")],
})
receive = AsgiReceiveEmulator()
send = AsgiSendEmulator()
asyncio.run(application(scope, receive, send))
assert send.body == test_files.txt_content
assert send.body_count == 2
assert send.headers[b"content-length"] == str(len(test_files.txt_content)).encode()
assert b"text/plain" in send.headers[b"content-type"]
1 change: 1 addition & 0 deletions tests/test_django_servestatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@ def test_large_static_file(asgi_application, static_files):
send = AsgiSendEmulator()
asyncio.run(AsgiAppServer(asgi_application)(scope, receive, send))
assert send.body == static_files.txt_content
assert send.body_count == 2
assert send.headers[b"Content-Length"] == str(len(static_files.txt_content)).encode()
assert b"text/plain" in send.headers[b"Content-Type"]

Expand Down
5 changes: 5 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ def body(self):
"""Combine all HTTP body messages into a single bytestring."""
return b"".join([msg["body"] for msg in self.message if msg.get("body")])

@property
def body_count(self):
"""Return the number of body messages."""
return sum(bool(msg.get("body")) for msg in self.message)

@property
def headers(self):
"""Return the headers from the first event."""
Expand Down

0 comments on commit 0db0190

Please sign in to comment.