mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Fixed #31224 -- Added support for asynchronous views and middleware.
This implements support for asynchronous views, asynchronous tests, asynchronous middleware, and an asynchronous test client.
This commit is contained in:
committed by
Mariusz Felisiak
parent
3f7e4b16bf
commit
fc0fa72ff4
@@ -106,6 +106,16 @@ class TransactionsPerRequestTests(TransactionTestCase):
|
||||
connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
|
||||
self.assertContains(response, 'True')
|
||||
|
||||
async def test_auto_transaction_async_view(self):
|
||||
old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
|
||||
try:
|
||||
connection.settings_dict['ATOMIC_REQUESTS'] = True
|
||||
msg = 'You cannot use ATOMIC_REQUESTS with async views.'
|
||||
with self.assertRaisesMessage(RuntimeError, msg):
|
||||
await self.async_client.get('/async_regular/')
|
||||
finally:
|
||||
connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
|
||||
|
||||
def test_no_auto_transaction(self):
|
||||
old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
|
||||
try:
|
||||
@@ -157,6 +167,11 @@ def empty_middleware(get_response):
|
||||
class HandlerRequestTests(SimpleTestCase):
|
||||
request_factory = RequestFactory()
|
||||
|
||||
def test_async_view(self):
|
||||
"""Calling an async view down the normal synchronous path."""
|
||||
response = self.client.get('/async_regular/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_suspiciousop_in_view_returns_400(self):
|
||||
response = self.client.get('/suspicious/')
|
||||
self.assertEqual(response.status_code, 400)
|
||||
@@ -224,3 +239,39 @@ class ScriptNameTests(SimpleTestCase):
|
||||
'PATH_INFO': '/milestones/accounts/login/help',
|
||||
})
|
||||
self.assertEqual(script_name, '/mst')
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF='handlers.urls')
|
||||
class AsyncHandlerRequestTests(SimpleTestCase):
|
||||
"""Async variants of the normal handler request tests."""
|
||||
|
||||
async def test_sync_view(self):
|
||||
"""Calling a sync view down the asynchronous path."""
|
||||
response = await self.async_client.get('/regular/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
async def test_async_view(self):
|
||||
"""Calling an async view down the asynchronous path."""
|
||||
response = await self.async_client.get('/async_regular/')
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
async def test_suspiciousop_in_view_returns_400(self):
|
||||
response = await self.async_client.get('/suspicious/')
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
async def test_no_response(self):
|
||||
msg = (
|
||||
"The view handlers.views.no_response didn't return an "
|
||||
"HttpResponse object. It returned None instead."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
await self.async_client.get('/no_response_fbv/')
|
||||
|
||||
async def test_unawaited_response(self):
|
||||
msg = (
|
||||
"The view handlers.views.async_unawaited didn't return an "
|
||||
"HttpResponse object. It returned an unawaited coroutine instead. "
|
||||
"You may need to add an 'await' into your view."
|
||||
)
|
||||
with self.assertRaisesMessage(ValueError, msg):
|
||||
await self.async_client.get('/unawaited/')
|
||||
|
||||
Reference in New Issue
Block a user