summaryrefslogtreecommitdiff
path: root/tests/asgi
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2019-04-12 06:15:18 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-06-20 12:29:43 +0200
commita415ce70bef6d91036b00dd2c8544aed7aeeaaed (patch)
tree3583cef22e9b56d2ed52456ab586d9c47620bc51 /tests/asgi
parentcce47ff65a4dd3786c049ec14ee889e128ca7de9 (diff)
Fixed #30451 -- Added ASGI handler and coroutine-safety.
This adds an ASGI handler, asgi.py file for the default project layout, a few async utilities and adds async-safety to many parts of Django.
Diffstat (limited to 'tests/asgi')
-rw-r--r--tests/asgi/__init__.py0
-rw-r--r--tests/asgi/tests.py84
-rw-r--r--tests/asgi/urls.py15
3 files changed, 99 insertions, 0 deletions
diff --git a/tests/asgi/__init__.py b/tests/asgi/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/asgi/__init__.py
diff --git a/tests/asgi/tests.py b/tests/asgi/tests.py
new file mode 100644
index 0000000000..243e77defb
--- /dev/null
+++ b/tests/asgi/tests.py
@@ -0,0 +1,84 @@
+import sys
+
+from asgiref.sync import async_to_sync
+from asgiref.testing import ApplicationCommunicator
+
+from django.core.asgi import get_asgi_application
+from django.core.signals import request_started
+from django.db import close_old_connections
+from django.test import SimpleTestCase, override_settings
+
+from .urls import test_filename
+
+
+@override_settings(ROOT_URLCONF='asgi.urls')
+class ASGITest(SimpleTestCase):
+
+ def setUp(self):
+ request_started.disconnect(close_old_connections)
+
+ def _get_scope(self, **kwargs):
+ return {
+ 'type': 'http',
+ 'asgi': {'version': '3.0', 'spec_version': '2.1'},
+ 'http_version': '1.1',
+ 'method': 'GET',
+ 'query_string': b'',
+ 'server': ('testserver', 80),
+ **kwargs,
+ }
+
+ def tearDown(self):
+ request_started.connect(close_old_connections)
+
+ @async_to_sync
+ async def test_get_asgi_application(self):
+ """
+ get_asgi_application() returns a functioning ASGI callable.
+ """
+ application = get_asgi_application()
+ # Construct HTTP request.
+ communicator = ApplicationCommunicator(application, self._get_scope(path='/'))
+ 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_to_sync
+ async def test_file_response(self):
+ """
+ Makes sure that FileResponse works over ASGI.
+ """
+ application = get_asgi_application()
+ # Construct HTTP request.
+ communicator = ApplicationCommunicator(application, self._get_scope(path='/file/'))
+ 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')),
+ (b'Content-Type', b'text/plain' if sys.platform.startswith('win') else b'text/x-python'),
+ (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)
diff --git a/tests/asgi/urls.py b/tests/asgi/urls.py
new file mode 100644
index 0000000000..4177ec8c9a
--- /dev/null
+++ b/tests/asgi/urls.py
@@ -0,0 +1,15 @@
+from django.http import FileResponse, HttpResponse
+from django.urls import path
+
+
+def helloworld(request):
+ return HttpResponse('Hello World!')
+
+
+test_filename = __file__
+
+
+urlpatterns = [
+ path('', helloworld),
+ path('file/', lambda x: FileResponse(open(test_filename, 'rb'))),
+]