From a3120615349ad8d8d76d63f6f0a2da6fc3c75c6b Mon Sep 17 00:00:00 2001 From: Archmonger <16909269+Archmonger@users.noreply.github.com> Date: Wed, 11 Sep 2024 17:38:03 -0700 Subject: [PATCH] Django 5.0 test compat --- tests/test_django_servestatic.py | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/test_django_servestatic.py b/tests/test_django_servestatic.py index 538e0e20..3a19cd65 100644 --- a/tests/test_django_servestatic.py +++ b/tests/test_django_servestatic.py @@ -357,6 +357,7 @@ def test_range_response(server, static_files, _collect_static): # assert response.headers["Content-Length"] == "14" +@pytest.mark.skipif(django.VERSION >= (5, 0), reason="Django <5.0 only") def test_asgi_range_response(asgi_application, static_files, _collect_static): url = storage.staticfiles_storage.url(static_files.js_path) scope = AsgiScopeEmulator({"path": url, "headers": [(b"range", b"bytes=0-13")]}) @@ -372,12 +373,37 @@ def test_asgi_range_response(asgi_application, static_files, _collect_static): assert send.status == 206 +@pytest.mark.skipif(django.VERSION < (5, 0), reason="Django 5.0+ only") +def test_asgi_range_response_2(asgi_application, static_files, _collect_static): + url = storage.staticfiles_storage.url(static_files.js_path) + scope = AsgiScopeEmulator({"path": url, "headers": [(b"range", b"bytes=0-13")]}) + + async def executor(): + communicator = ApplicationCommunicator(asgi_application, scope) + await communicator.send_input(scope) + response_start = await communicator.receive_output() + response_body = await communicator.receive_output() + return response_start | response_body + + response = asyncio.run(executor()) + headers = dict(response["headers"]) + + assert response["body"] == static_files.js_content[:14] + assert ( + headers[b"Content-Range"] + == b"bytes 0-13/" + str(len(static_files.js_content)).encode() + ) + assert headers[b"Content-Length"] == b"14" + assert response["status"] == 206 + + def test_out_of_range_error(server, static_files, _collect_static): url = storage.staticfiles_storage.url(static_files.js_path) response = server.get(url, headers={"Range": "bytes=900-999"}) assert response.status_code == 416 +@pytest.mark.skipif(django.VERSION >= (5, 0), reason="Django <5.0 only") def test_asgi_out_of_range_error(asgi_application, static_files, _collect_static): url = storage.staticfiles_storage.url(static_files.js_path) scope = AsgiScopeEmulator({"path": url, "headers": [(b"range", b"bytes=900-999")]}) @@ -385,3 +411,22 @@ def test_asgi_out_of_range_error(asgi_application, static_files, _collect_static send = AsgiSendEmulator() asyncio.run(AsgiAppServer(asgi_application)(scope, receive, send)) assert send.status == 416 + + +@pytest.mark.skipif(django.VERSION < (5, 0), reason="Django 5.0+ only") +def test_asgi_out_of_range_error_2(asgi_application, static_files, _collect_static): + url = storage.staticfiles_storage.url(static_files.js_path) + scope = AsgiScopeEmulator({"path": url, "headers": [(b"range", b"bytes=900-999")]}) + + async def executor(): + communicator = ApplicationCommunicator(asgi_application, scope) + await communicator.send_input(scope) + response_start = await communicator.receive_output() + response_body = await communicator.receive_output() + return response_start | response_body + + response = asyncio.run(executor()) + assert response["status"] == 416 + assert dict(response["headers"])[b"Content-Range"] == b"bytes */%d" % len( + static_files.js_content + )