| 1 |
from django.contrib.staticfiles.handlers import ASGIStaticFilesHandler |
| 2 |
from django.core.handlers.asgi import ASGIHandler |
| 3 |
from django.test import AsyncRequestFactory |
| 4 |
|
| 5 |
from .cases import StaticFilesTestCase |
| 6 |
|
| 7 |
|
| 8 |
class MockApplication: |
| 9 |
"""ASGI application that returns a string indicating that it was called.""" |
| 10 |
|
| 11 |
async def __call__(self, scope, receive, send): |
| 12 |
return "Application called" |
| 13 |
|
| 14 |
|
| 15 |
class TestASGIStaticFilesHandler(StaticFilesTestCase): |
| 16 |
async_request_factory = AsyncRequestFactory() |
| 17 |
|
| 18 |
async def test_get_async_response(self): |
| 19 |
request = self.async_request_factory.get("/static/test/file.txt") |
| 20 |
handler = ASGIStaticFilesHandler(ASGIHandler()) |
| 21 |
response = await handler.get_response_async(request) |
| 22 |
response.close() |
| 23 |
self.assertEqual(response.status_code, 200) |
| 24 |
|
| 25 |
async def test_get_async_response_not_found(self): |
| 26 |
request = self.async_request_factory.get("/static/test/not-found.txt") |
| 27 |
handler = ASGIStaticFilesHandler(ASGIHandler()) |
| 28 |
response = await handler.get_response_async(request) |
| 29 |
self.assertEqual(response.status_code, 404) |
| 30 |
|
| 31 |
async def test_non_http_requests_passed_to_the_wrapped_application(self): |
| 32 |
tests = [ |
| 33 |
"/static/path.txt", |
| 34 |
"/non-static/path.txt", |
| 35 |
] |
| 36 |
for path in tests: |
| 37 |
with self.subTest(path=path): |
| 38 |
scope = {"type": "websocket", "path": path} |
| 39 |
handler = ASGIStaticFilesHandler(MockApplication()) |
| 40 |
response = await handler(scope, None, None) |
| 41 |
self.assertEqual(response, "Application called") |