2019-06-17 14:27:04 +00:00
|
|
|
import asyncio
|
2019-04-12 13:15:18 +00:00
|
|
|
import sys
|
2020-05-05 09:55:49 +00:00
|
|
|
import threading
|
2020-05-14 23:26:32 +00:00
|
|
|
from pathlib import Path
|
2019-04-12 13:15:18 +00:00
|
|
|
|
|
|
|
from asgiref.testing import ApplicationCommunicator
|
|
|
|
|
2020-05-14 23:26:32 +00:00
|
|
|
from django.contrib.staticfiles.handlers import ASGIStaticFilesHandler
|
2019-04-12 13:15:18 +00:00
|
|
|
from django.core.asgi import get_asgi_application
|
2020-05-05 09:55:49 +00:00
|
|
|
from django.core.signals import request_finished, request_started
|
2019-04-12 13:15:18 +00:00
|
|
|
from django.db import close_old_connections
|
2020-05-14 23:26:32 +00:00
|
|
|
from django.test import (
|
2022-02-03 19:24:19 +00:00
|
|
|
AsyncRequestFactory,
|
|
|
|
SimpleTestCase,
|
2022-12-13 15:15:25 +00:00
|
|
|
ignore_warnings,
|
2022-02-03 19:24:19 +00:00
|
|
|
modify_settings,
|
|
|
|
override_settings,
|
2020-05-14 23:26:32 +00:00
|
|
|
)
|
|
|
|
from django.utils.http import http_date
|
2019-04-12 13:15:18 +00:00
|
|
|
|
2021-06-30 15:37:10 +00:00
|
|
|
from .urls import sync_waiter, test_filename
|
2019-04-12 13:15:18 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
TEST_STATIC_ROOT = Path(__file__).parent / "project" / "static"
|
2020-05-14 23:26:32 +00:00
|
|
|
|
2019-04-12 13:15:18 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(ROOT_URLCONF="asgi.urls")
|
2019-04-12 13:15:18 +00:00
|
|
|
class ASGITest(SimpleTestCase):
|
2020-02-12 22:15:00 +00:00
|
|
|
async_request_factory = AsyncRequestFactory()
|
2019-04-12 13:15:18 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
request_started.disconnect(close_old_connections)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
request_started.connect(close_old_connections)
|
|
|
|
|
|
|
|
async def test_get_asgi_application(self):
|
|
|
|
"""
|
|
|
|
get_asgi_application() returns a functioning ASGI callable.
|
|
|
|
"""
|
|
|
|
application = get_asgi_application()
|
|
|
|
# Construct HTTP request.
|
2022-02-03 19:24:19 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path="/")
|
2020-02-12 22:15:00 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.request"})
|
2019-04-12 13:15:18 +00:00
|
|
|
# Read the response.
|
|
|
|
response_start = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 200)
|
2019-04-12 13:15:18 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
set(response_start["headers"]),
|
2019-04-12 13:15:18 +00:00
|
|
|
{
|
2022-02-03 19:24:19 +00:00
|
|
|
(b"Content-Length", b"12"),
|
|
|
|
(b"Content-Type", b"text/html; charset=utf-8"),
|
2019-04-12 13:15:18 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
response_body = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], b"Hello World!")
|
2022-08-04 07:30:41 +00:00
|
|
|
# Allow response.close() to finish.
|
|
|
|
await communicator.wait()
|
2019-04-12 13:15:18 +00:00
|
|
|
|
2022-12-13 15:15:25 +00:00
|
|
|
# Python's file API is not async compatible. A third-party library such
|
|
|
|
# as https://github.com/Tinche/aiofiles allows passing the file to
|
|
|
|
# FileResponse as an async interator. With a sync iterator
|
|
|
|
# StreamingHTTPResponse triggers a warning when iterating the file.
|
|
|
|
# assertWarnsMessage is not async compatible, so ignore_warnings for the
|
|
|
|
# test.
|
|
|
|
@ignore_warnings(module="django.http.response")
|
2019-04-12 13:15:18 +00:00
|
|
|
async def test_file_response(self):
|
|
|
|
"""
|
|
|
|
Makes sure that FileResponse works over ASGI.
|
|
|
|
"""
|
|
|
|
application = get_asgi_application()
|
|
|
|
# Construct HTTP request.
|
2022-02-03 19:24:19 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path="/file/")
|
2020-02-12 22:15:00 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.request"})
|
2019-04-12 13:15:18 +00:00
|
|
|
# Get the file content.
|
2022-02-03 19:24:19 +00:00
|
|
|
with open(test_filename, "rb") as test_file:
|
2019-04-12 13:15:18 +00:00
|
|
|
test_file_contents = test_file.read()
|
|
|
|
# Read the response.
|
|
|
|
response_start = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 200)
|
|
|
|
headers = response_start["headers"]
|
2020-11-05 07:18:45 +00:00
|
|
|
self.assertEqual(len(headers), 3)
|
|
|
|
expected_headers = {
|
2022-02-03 19:24:19 +00:00
|
|
|
b"Content-Length": str(len(test_file_contents)).encode("ascii"),
|
|
|
|
b"Content-Type": b"text/x-python",
|
|
|
|
b"Content-Disposition": b'inline; filename="urls.py"',
|
2020-11-05 07:18:45 +00:00
|
|
|
}
|
|
|
|
for key, value in headers:
|
|
|
|
try:
|
|
|
|
self.assertEqual(value, expected_headers[key])
|
|
|
|
except AssertionError:
|
|
|
|
# Windows registry may not be configured with correct
|
|
|
|
# mimetypes.
|
2022-02-03 19:24:19 +00:00
|
|
|
if sys.platform == "win32" and key == b"Content-Type":
|
|
|
|
self.assertEqual(value, b"text/plain")
|
2020-11-05 07:18:45 +00:00
|
|
|
else:
|
|
|
|
raise
|
2022-12-13 15:15:25 +00:00
|
|
|
|
|
|
|
# Warning ignored here.
|
2019-04-12 13:15:18 +00:00
|
|
|
response_body = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], test_file_contents)
|
2020-02-12 22:15:00 +00:00
|
|
|
# Allow response.close() to finish.
|
|
|
|
await communicator.wait()
|
2019-06-17 14:27:04 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@modify_settings(INSTALLED_APPS={"append": "django.contrib.staticfiles"})
|
2020-05-14 23:26:32 +00:00
|
|
|
@override_settings(
|
2022-02-03 19:24:19 +00:00
|
|
|
STATIC_URL="static/",
|
2020-05-14 23:26:32 +00:00
|
|
|
STATIC_ROOT=TEST_STATIC_ROOT,
|
|
|
|
STATICFILES_DIRS=[TEST_STATIC_ROOT],
|
|
|
|
STATICFILES_FINDERS=[
|
2022-02-03 19:24:19 +00:00
|
|
|
"django.contrib.staticfiles.finders.FileSystemFinder",
|
2020-05-14 23:26:32 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_static_file_response(self):
|
|
|
|
application = ASGIStaticFilesHandler(get_asgi_application())
|
|
|
|
# Construct HTTP request.
|
2022-02-03 19:24:19 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path="/static/file.txt")
|
2020-05-14 23:26:32 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.request"})
|
2020-05-14 23:26:32 +00:00
|
|
|
# Get the file content.
|
2022-02-03 19:24:19 +00:00
|
|
|
file_path = TEST_STATIC_ROOT / "file.txt"
|
|
|
|
with open(file_path, "rb") as test_file:
|
2020-05-14 23:26:32 +00:00
|
|
|
test_file_contents = test_file.read()
|
|
|
|
# Read the response.
|
|
|
|
stat = file_path.stat()
|
|
|
|
response_start = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 200)
|
2020-05-14 23:26:32 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
set(response_start["headers"]),
|
2020-05-14 23:26:32 +00:00
|
|
|
{
|
2022-02-03 19:24:19 +00:00
|
|
|
(b"Content-Length", str(len(test_file_contents)).encode("ascii")),
|
|
|
|
(b"Content-Type", b"text/plain"),
|
|
|
|
(b"Content-Disposition", b'inline; filename="file.txt"'),
|
|
|
|
(b"Last-Modified", http_date(stat.st_mtime).encode("ascii")),
|
2020-05-14 23:26:32 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
response_body = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], test_file_contents)
|
2020-05-14 23:26:32 +00:00
|
|
|
# Allow response.close() to finish.
|
|
|
|
await communicator.wait()
|
|
|
|
|
2019-06-17 14:27:04 +00:00
|
|
|
async def test_headers(self):
|
|
|
|
application = get_asgi_application()
|
|
|
|
communicator = ApplicationCommunicator(
|
|
|
|
application,
|
2020-02-12 22:15:00 +00:00
|
|
|
self.async_request_factory._base_scope(
|
2022-02-03 19:24:19 +00:00
|
|
|
path="/meta/",
|
2019-06-17 14:27:04 +00:00
|
|
|
headers=[
|
2022-02-03 19:24:19 +00:00
|
|
|
[b"content-type", b"text/plain; charset=utf-8"],
|
|
|
|
[b"content-length", b"77"],
|
|
|
|
[b"referer", b"Scotland"],
|
|
|
|
[b"referer", b"Wales"],
|
2019-06-17 14:27:04 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.request"})
|
2019-06-17 14:27:04 +00:00
|
|
|
response_start = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 200)
|
2019-06-17 14:27:04 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
set(response_start["headers"]),
|
2019-06-17 14:27:04 +00:00
|
|
|
{
|
2022-02-03 19:24:19 +00:00
|
|
|
(b"Content-Length", b"19"),
|
|
|
|
(b"Content-Type", b"text/plain; charset=utf-8"),
|
2019-06-17 14:27:04 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
response_body = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], b"From Scotland,Wales")
|
2022-08-04 07:30:41 +00:00
|
|
|
# Allow response.close() to finish
|
|
|
|
await communicator.wait()
|
2019-06-17 14:27:04 +00:00
|
|
|
|
2022-05-30 20:45:48 +00:00
|
|
|
async def test_post_body(self):
|
|
|
|
application = get_asgi_application()
|
2022-06-02 21:19:16 +00:00
|
|
|
scope = self.async_request_factory._base_scope(
|
|
|
|
method="POST",
|
|
|
|
path="/post/",
|
|
|
|
query_string="echo=1",
|
|
|
|
)
|
2022-05-30 20:45:48 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
|
|
|
await communicator.send_input({"type": "http.request", "body": b"Echo!"})
|
|
|
|
response_start = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 200)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], b"Echo!")
|
|
|
|
|
2022-06-02 21:19:16 +00:00
|
|
|
async def test_untouched_request_body_gets_closed(self):
|
|
|
|
application = get_asgi_application()
|
|
|
|
scope = self.async_request_factory._base_scope(method="POST", path="/post/")
|
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
|
|
|
await communicator.send_input({"type": "http.request"})
|
|
|
|
response_start = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 204)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], b"")
|
2022-08-04 07:30:41 +00:00
|
|
|
# Allow response.close() to finish
|
|
|
|
await communicator.wait()
|
2022-06-02 21:19:16 +00:00
|
|
|
|
2019-06-17 14:27:04 +00:00
|
|
|
async def test_get_query_string(self):
|
|
|
|
application = get_asgi_application()
|
2022-02-03 19:24:19 +00:00
|
|
|
for query_string in (b"name=Andrew", "name=Andrew"):
|
2019-06-17 14:27:04 +00:00
|
|
|
with self.subTest(query_string=query_string):
|
2020-02-12 22:15:00 +00:00
|
|
|
scope = self.async_request_factory._base_scope(
|
2022-02-03 19:24:19 +00:00
|
|
|
path="/",
|
2020-02-12 22:15:00 +00:00
|
|
|
query_string=query_string,
|
2019-06-17 14:27:04 +00:00
|
|
|
)
|
2020-02-12 22:15:00 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.request"})
|
2019-06-17 14:27:04 +00:00
|
|
|
response_start = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 200)
|
2019-06-17 14:27:04 +00:00
|
|
|
response_body = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], b"Hello Andrew!")
|
2022-08-04 07:30:41 +00:00
|
|
|
# Allow response.close() to finish
|
|
|
|
await communicator.wait()
|
2019-06-17 14:27:04 +00:00
|
|
|
|
|
|
|
async def test_disconnect(self):
|
|
|
|
application = get_asgi_application()
|
2022-02-03 19:24:19 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path="/")
|
2020-02-12 22:15:00 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.disconnect"})
|
2019-06-17 14:27:04 +00:00
|
|
|
with self.assertRaises(asyncio.TimeoutError):
|
|
|
|
await communicator.receive_output()
|
|
|
|
|
|
|
|
async def test_wrong_connection_type(self):
|
|
|
|
application = get_asgi_application()
|
2022-02-03 19:24:19 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path="/", type="other")
|
2020-02-12 22:15:00 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.request"})
|
|
|
|
msg = "Django can only handle ASGI/HTTP connections, not other."
|
2019-06-17 14:27:04 +00:00
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
await communicator.receive_output()
|
|
|
|
|
|
|
|
async def test_non_unicode_query_string(self):
|
|
|
|
application = get_asgi_application()
|
2022-02-03 19:24:19 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path="/", query_string=b"\xff")
|
2020-02-12 22:15:00 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.request"})
|
2019-06-17 14:27:04 +00:00
|
|
|
response_start = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 400)
|
2019-06-17 14:27:04 +00:00
|
|
|
response_body = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], b"")
|
2020-05-05 09:55:49 +00:00
|
|
|
|
|
|
|
async def test_request_lifecycle_signals_dispatched_with_thread_sensitive(self):
|
|
|
|
class SignalHandler:
|
|
|
|
"""Track threads handler is dispatched on."""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2020-05-05 09:55:49 +00:00
|
|
|
threads = []
|
|
|
|
|
|
|
|
def __call__(self, **kwargs):
|
|
|
|
self.threads.append(threading.current_thread())
|
|
|
|
|
|
|
|
signal_handler = SignalHandler()
|
|
|
|
request_started.connect(signal_handler)
|
|
|
|
request_finished.connect(signal_handler)
|
|
|
|
|
|
|
|
# Perform a basic request.
|
|
|
|
application = get_asgi_application()
|
2022-02-03 19:24:19 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path="/")
|
2020-05-05 09:55:49 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicator.send_input({"type": "http.request"})
|
2020-05-05 09:55:49 +00:00
|
|
|
response_start = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 200)
|
2020-05-05 09:55:49 +00:00
|
|
|
response_body = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], b"Hello World!")
|
2020-05-05 09:55:49 +00:00
|
|
|
# Give response.close() time to finish.
|
|
|
|
await communicator.wait()
|
|
|
|
|
2021-06-30 15:37:10 +00:00
|
|
|
# AsyncToSync should have executed the signals in the same thread.
|
2020-05-05 09:55:49 +00:00
|
|
|
request_started_thread, request_finished_thread = signal_handler.threads
|
2021-06-30 15:37:10 +00:00
|
|
|
self.assertEqual(request_started_thread, request_finished_thread)
|
2020-05-05 09:55:49 +00:00
|
|
|
request_started.disconnect(signal_handler)
|
|
|
|
request_finished.disconnect(signal_handler)
|
2021-06-30 15:37:10 +00:00
|
|
|
|
|
|
|
async def test_concurrent_async_uses_multiple_thread_pools(self):
|
|
|
|
sync_waiter.active_threads.clear()
|
|
|
|
|
|
|
|
# Send 2 requests concurrently
|
|
|
|
application = get_asgi_application()
|
2022-02-03 19:24:19 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path="/wait/")
|
2021-06-30 15:37:10 +00:00
|
|
|
communicators = []
|
|
|
|
for _ in range(2):
|
|
|
|
communicators.append(ApplicationCommunicator(application, scope))
|
2022-02-03 19:24:19 +00:00
|
|
|
await communicators[-1].send_input({"type": "http.request"})
|
2021-06-30 15:37:10 +00:00
|
|
|
|
|
|
|
# Each request must complete with a status code of 200
|
|
|
|
# If requests aren't scheduled concurrently, the barrier in the
|
|
|
|
# sync_wait view will time out, resulting in a 500 status code.
|
|
|
|
for communicator in communicators:
|
|
|
|
response_start = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_start["type"], "http.response.start")
|
|
|
|
self.assertEqual(response_start["status"], 200)
|
2021-06-30 15:37:10 +00:00
|
|
|
response_body = await communicator.receive_output()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(response_body["type"], "http.response.body")
|
|
|
|
self.assertEqual(response_body["body"], b"Hello World!")
|
2021-06-30 15:37:10 +00:00
|
|
|
# Give response.close() time to finish.
|
|
|
|
await communicator.wait()
|
|
|
|
|
|
|
|
# The requests should have scheduled on different threads. Note
|
|
|
|
# active_threads is a set (a thread can only appear once), therefore
|
|
|
|
# length is a sufficient check.
|
|
|
|
self.assertEqual(len(sync_waiter.active_threads), 2)
|
|
|
|
|
|
|
|
sync_waiter.active_threads.clear()
|