diff options
author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-06-17 16:27:04 +0200 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-06-20 12:29:58 +0200 |
commit | 7f19e3713598a37b0809b5434114140170d12e34 (patch) | |
tree | 7b23fb296d6e56f9386318579839968f88ebe151 /tests/asgi | |
parent | a415ce70bef6d91036b00dd2c8544aed7aeeaaed (diff) | |
download | django-7f19e3713598a37b0809b5434114140170d12e34.tar.gz |
Refs #30451 -- Added more tests for ASGIRequest and ASGIHandler.
Diffstat (limited to 'tests/asgi')
-rw-r--r-- | tests/asgi/tests.py | 83 | ||||
-rw-r--r-- | tests/asgi/urls.py | 15 |
2 files changed, 95 insertions, 3 deletions
diff --git a/tests/asgi/tests.py b/tests/asgi/tests.py index 243e77defb..32520fae29 100644 --- a/tests/asgi/tests.py +++ b/tests/asgi/tests.py @@ -1,3 +1,4 @@ +import asyncio import sys from asgiref.sync import async_to_sync @@ -82,3 +83,85 @@ class ASGITest(SimpleTestCase): response_body = await communicator.receive_output() self.assertEqual(response_body['type'], 'http.response.body') self.assertEqual(response_body['body'], test_file_contents) + + @async_to_sync + async def test_headers(self): + application = get_asgi_application() + communicator = ApplicationCommunicator( + application, + self._get_scope( + 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_to_sync + 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): + communicator = ApplicationCommunicator( + application, + self._get_scope(path='/', query_string=query_string), + ) + 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_to_sync + async def test_disconnect(self): + application = get_asgi_application() + communicator = ApplicationCommunicator(application, self._get_scope(path='/')) + await communicator.send_input({'type': 'http.disconnect'}) + with self.assertRaises(asyncio.TimeoutError): + await communicator.receive_output() + + @async_to_sync + async def test_wrong_connection_type(self): + application = get_asgi_application() + communicator = ApplicationCommunicator( + application, + self._get_scope(path='/', type='other'), + ) + 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_to_sync + async def test_non_unicode_query_string(self): + application = get_asgi_application() + communicator = ApplicationCommunicator( + application, + self._get_scope(path='/', query_string=b'\xff'), + ) + 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'') diff --git a/tests/asgi/urls.py b/tests/asgi/urls.py index 4177ec8c9a..ff8d21ea7c 100644 --- a/tests/asgi/urls.py +++ b/tests/asgi/urls.py @@ -2,14 +2,23 @@ from django.http import FileResponse, HttpResponse from django.urls import path -def helloworld(request): - return HttpResponse('Hello World!') +def hello(request): + name = request.GET.get('name') or 'World' + return HttpResponse('Hello %s!' % name) + + +def hello_meta(request): + return HttpResponse( + 'From %s' % request.META.get('HTTP_REFERER') or '', + content_type=request.META.get('CONTENT_TYPE'), + ) test_filename = __file__ urlpatterns = [ - path('', helloworld), + path('', hello), path('file/', lambda x: FileResponse(open(test_filename, 'rb'))), + path('meta/', hello_meta), ] |