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-10-24 08:43:05 +00:00
|
|
|
from unittest import skipIf
|
2019-04-12 13:15:18 +00:00
|
|
|
|
2020-05-05 09:55:49 +00:00
|
|
|
from asgiref.sync import SyncToAsync
|
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 (
|
|
|
|
AsyncRequestFactory, SimpleTestCase, modify_settings, override_settings,
|
|
|
|
)
|
|
|
|
from django.utils.http import http_date
|
2019-04-12 13:15:18 +00:00
|
|
|
|
|
|
|
from .urls import test_filename
|
|
|
|
|
2020-05-14 23:26:32 +00:00
|
|
|
TEST_STATIC_ROOT = Path(__file__).parent / 'project' / 'static'
|
|
|
|
|
2019-04-12 13:15:18 +00:00
|
|
|
|
2019-10-24 08:43:05 +00:00
|
|
|
@skipIf(sys.platform == 'win32' and (3, 8, 0) < sys.version_info < (3, 8, 1), 'https://bugs.python.org/issue38563')
|
2019-04-12 13:15:18 +00:00
|
|
|
@override_settings(ROOT_URLCONF='asgi.urls')
|
|
|
|
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.
|
2020-02-12 22:15:00 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path='/')
|
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2019-04-12 13:15:18 +00:00
|
|
|
await communicator.send_input({'type': 'http.request'})
|
|
|
|
# Read the response.
|
|
|
|
response_start = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_start['type'], 'http.response.start')
|
|
|
|
self.assertEqual(response_start['status'], 200)
|
|
|
|
self.assertEqual(
|
|
|
|
set(response_start['headers']),
|
|
|
|
{
|
|
|
|
(b'Content-Length', b'12'),
|
|
|
|
(b'Content-Type', b'text/html; charset=utf-8'),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_body['type'], 'http.response.body')
|
|
|
|
self.assertEqual(response_body['body'], b'Hello World!')
|
|
|
|
|
|
|
|
async def test_file_response(self):
|
|
|
|
"""
|
|
|
|
Makes sure that FileResponse works over ASGI.
|
|
|
|
"""
|
|
|
|
application = get_asgi_application()
|
|
|
|
# Construct HTTP request.
|
2020-02-12 22:15:00 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path='/file/')
|
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2019-04-12 13:15:18 +00:00
|
|
|
await communicator.send_input({'type': 'http.request'})
|
|
|
|
# Get the file content.
|
|
|
|
with open(test_filename, 'rb') as test_file:
|
|
|
|
test_file_contents = test_file.read()
|
|
|
|
# Read the response.
|
|
|
|
response_start = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_start['type'], 'http.response.start')
|
|
|
|
self.assertEqual(response_start['status'], 200)
|
|
|
|
self.assertEqual(
|
|
|
|
set(response_start['headers']),
|
|
|
|
{
|
|
|
|
(b'Content-Length', str(len(test_file_contents)).encode('ascii')),
|
2019-11-06 14:14:30 +00:00
|
|
|
(b'Content-Type', b'text/plain' if sys.platform == 'win32' else b'text/x-python'),
|
2019-04-12 13:15:18 +00:00
|
|
|
(b'Content-Disposition', b'inline; filename="urls.py"'),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
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
|
|
|
|
2020-05-14 23:26:32 +00:00
|
|
|
@modify_settings(INSTALLED_APPS={'append': 'django.contrib.staticfiles'})
|
|
|
|
@override_settings(
|
|
|
|
STATIC_URL='/static/',
|
|
|
|
STATIC_ROOT=TEST_STATIC_ROOT,
|
|
|
|
STATICFILES_DIRS=[TEST_STATIC_ROOT],
|
|
|
|
STATICFILES_FINDERS=[
|
|
|
|
'django.contrib.staticfiles.finders.FileSystemFinder',
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_static_file_response(self):
|
|
|
|
application = ASGIStaticFilesHandler(get_asgi_application())
|
|
|
|
# Construct HTTP request.
|
|
|
|
scope = self.async_request_factory._base_scope(path='/static/file.txt')
|
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
|
|
|
await communicator.send_input({'type': 'http.request'})
|
|
|
|
# Get the file content.
|
|
|
|
file_path = TEST_STATIC_ROOT / 'file.txt'
|
|
|
|
with open(file_path, 'rb') as test_file:
|
|
|
|
test_file_contents = test_file.read()
|
|
|
|
# Read the response.
|
|
|
|
stat = file_path.stat()
|
|
|
|
response_start = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_start['type'], 'http.response.start')
|
|
|
|
self.assertEqual(response_start['status'], 200)
|
|
|
|
self.assertEqual(
|
|
|
|
set(response_start['headers']),
|
|
|
|
{
|
|
|
|
(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')),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_body['type'], 'http.response.body')
|
|
|
|
self.assertEqual(response_body['body'], test_file_contents)
|
|
|
|
# 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(
|
2019-06-17 14:27:04 +00:00
|
|
|
path='/meta/',
|
|
|
|
headers=[
|
|
|
|
[b'content-type', b'text/plain; charset=utf-8'],
|
|
|
|
[b'content-length', b'77'],
|
|
|
|
[b'referer', b'Scotland'],
|
|
|
|
[b'referer', b'Wales'],
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
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'], 200)
|
|
|
|
self.assertEqual(
|
|
|
|
set(response_start['headers']),
|
|
|
|
{
|
|
|
|
(b'Content-Length', b'19'),
|
|
|
|
(b'Content-Type', b'text/plain; charset=utf-8'),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_body['type'], 'http.response.body')
|
|
|
|
self.assertEqual(response_body['body'], b'From Scotland,Wales')
|
|
|
|
|
|
|
|
async def test_get_query_string(self):
|
|
|
|
application = get_asgi_application()
|
|
|
|
for query_string in (b'name=Andrew', 'name=Andrew'):
|
|
|
|
with self.subTest(query_string=query_string):
|
2020-02-12 22:15:00 +00:00
|
|
|
scope = self.async_request_factory._base_scope(
|
|
|
|
path='/',
|
|
|
|
query_string=query_string,
|
2019-06-17 14:27:04 +00:00
|
|
|
)
|
2020-02-12 22:15:00 +00:00
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2019-06-17 14:27:04 +00:00
|
|
|
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'], 200)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_body['type'], 'http.response.body')
|
|
|
|
self.assertEqual(response_body['body'], b'Hello Andrew!')
|
|
|
|
|
|
|
|
async def test_disconnect(self):
|
|
|
|
application = get_asgi_application()
|
2020-02-12 22:15:00 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path='/')
|
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2019-06-17 14:27:04 +00:00
|
|
|
await communicator.send_input({'type': 'http.disconnect'})
|
|
|
|
with self.assertRaises(asyncio.TimeoutError):
|
|
|
|
await communicator.receive_output()
|
|
|
|
|
|
|
|
async def test_wrong_connection_type(self):
|
|
|
|
application = get_asgi_application()
|
2020-02-12 22:15:00 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path='/', type='other')
|
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2019-06-17 14:27:04 +00:00
|
|
|
await communicator.send_input({'type': 'http.request'})
|
|
|
|
msg = 'Django can only handle ASGI/HTTP connections, not other.'
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
await communicator.receive_output()
|
|
|
|
|
|
|
|
async def test_non_unicode_query_string(self):
|
|
|
|
application = get_asgi_application()
|
2020-02-12 22:15:00 +00:00
|
|
|
scope = self.async_request_factory._base_scope(path='/', query_string=b'\xff')
|
|
|
|
communicator = ApplicationCommunicator(application, scope)
|
2019-06-17 14:27:04 +00:00
|
|
|
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'], 400)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
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."""
|
|
|
|
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()
|
|
|
|
scope = self.async_request_factory._base_scope(path='/')
|
|
|
|
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'], 200)
|
|
|
|
response_body = await communicator.receive_output()
|
|
|
|
self.assertEqual(response_body['type'], 'http.response.body')
|
|
|
|
self.assertEqual(response_body['body'], b'Hello World!')
|
|
|
|
# Give response.close() time to finish.
|
|
|
|
await communicator.wait()
|
|
|
|
|
|
|
|
# At this point, AsyncToSync does not have a current executor. Thus
|
|
|
|
# SyncToAsync falls-back to .single_thread_executor.
|
|
|
|
target_thread = next(iter(SyncToAsync.single_thread_executor._threads))
|
|
|
|
request_started_thread, request_finished_thread = signal_handler.threads
|
|
|
|
self.assertEqual(request_started_thread, target_thread)
|
|
|
|
self.assertEqual(request_finished_thread, target_thread)
|
|
|
|
request_started.disconnect(signal_handler)
|
|
|
|
request_finished.disconnect(signal_handler)
|