Skip to content

Commit

Permalink
Add tests for large files
Browse files Browse the repository at this point in the history
  • Loading branch information
Archmonger committed Nov 15, 2024
1 parent be39df9 commit 3903ddf
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/servestatic/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(
self.closed = False

async def _execute(self, func, *args):
"""Run a function in a dedicated thread, specific to this instance."""
"""Run a function in a dedicated thread (specific to this class instance)."""
if self.loop is None:
self.loop = asyncio.get_event_loop()
with self.lock:
Expand Down
16 changes: 16 additions & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def test_files():
return Files(
js=str(Path("static") / "app.js"),
index=str(Path("static") / "with-index" / "index.html"),
txt=str(Path("static") / "large-file.txt"),
)


Expand Down Expand Up @@ -125,3 +126,18 @@ def test_wrong_method_type(application, test_files):
send = AsgiSendEmulator()
asyncio.run(application(scope, receive, send))
assert send.status == 405


def test_large_static_file(application, test_files):
scope = AsgiScopeEmulator({
"path": "/static/large-file.txt",
"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.headers[b"content-length"] == str(len(test_files.txt_content)).encode()
assert b"text/plain" in send.headers[b"content-type"]
14 changes: 13 additions & 1 deletion tests/test_django_servestatic.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def get_url_path(base, url):

@pytest.fixture
def static_files():
files = Files("static", js="app.js", nonascii="nonascii\u2713.txt")
files = Files("static", js="app.js", nonascii="nonascii\u2713.txt", txt="large-file.txt")
with override_settings(STATICFILES_DIRS=[files.directory]):
yield files

Expand Down Expand Up @@ -423,3 +423,15 @@ async def executor():
response = asyncio.run(executor())
assert response["status"] == 416
assert dict(response["headers"])[b"Content-Range"] == b"bytes */%d" % len(static_files.js_content)


@pytest.mark.usefixtures("_collect_static")
def test_large_static_file(asgi_application, static_files):
url = storage.staticfiles_storage.url(static_files.txt_path)
scope = AsgiScopeEmulator({"path": url, "headers": []})
receive = AsgiReceiveEmulator()
send = AsgiSendEmulator()
asyncio.run(AsgiAppServer(asgi_application)(scope, receive, send))
assert send.body == static_files.txt_content
assert send.headers[b"Content-Length"] == str(len(static_files.txt_content)).encode()
assert b"text/plain" in send.headers[b"Content-Type"]
1 change: 1 addition & 0 deletions tests/test_files/static/large-file.txt

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ def __init__(self, scope_overrides: dict | None = None):
),
(
b"accept",
b"text/html,application/xhtml+xml,application/xml;q=0.9,image/"
b"avif,image/webp,image/apng,*/*;q=0.8",
b"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8",
),
(b"sec-gpc", b"1"),
(b"sec-fetch-site", b"none"),
Expand Down Expand Up @@ -152,7 +151,7 @@ def __getitem__(self, index):
@property
def body(self):
"""Combine all HTTP body messages into a single bytestring."""
return b"".join([message["body"] for message in self.message if message.get("body")])
return b"".join([msg["body"] for msg in self.message if msg.get("body")])

@property
def headers(self):
Expand Down

0 comments on commit 3903ddf

Please sign in to comment.