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:
Andrew Godwin 2020-02-12 15:15:00 -07:00 committed by Mariusz Felisiak
parent 3f7e4b16bf
commit fc0fa72ff4
30 changed files with 1344 additions and 214 deletions

View file

@ -25,9 +25,10 @@ from unittest import mock
from django.contrib.auth.models import User
from django.core import mail
from django.http import HttpResponse
from django.http import HttpResponse, HttpResponseNotAllowed
from django.test import (
Client, RequestFactory, SimpleTestCase, TestCase, override_settings,
AsyncRequestFactory, Client, RequestFactory, SimpleTestCase, TestCase,
override_settings,
)
from django.urls import reverse_lazy
@ -918,3 +919,57 @@ class RequestFactoryTest(SimpleTestCase):
protocol = request.META["SERVER_PROTOCOL"]
echoed_request_line = "TRACE {} {}".format(url_path, protocol)
self.assertContains(response, echoed_request_line)
@override_settings(ROOT_URLCONF='test_client.urls')
class AsyncClientTest(TestCase):
async def test_response_resolver_match(self):
response = await self.async_client.get('/async_get_view/')
self.assertTrue(hasattr(response, 'resolver_match'))
self.assertEqual(response.resolver_match.url_name, 'async_get_view')
async def test_follow_parameter_not_implemented(self):
msg = 'AsyncClient request methods do not accept the follow parameter.'
tests = (
'get',
'post',
'put',
'patch',
'delete',
'head',
'options',
'trace',
)
for method_name in tests:
with self.subTest(method=method_name):
method = getattr(self.async_client, method_name)
with self.assertRaisesMessage(NotImplementedError, msg):
await method('/redirect_view/', follow=True)
@override_settings(ROOT_URLCONF='test_client.urls')
class AsyncRequestFactoryTest(SimpleTestCase):
request_factory = AsyncRequestFactory()
async def test_request_factory(self):
tests = (
'get',
'post',
'put',
'patch',
'delete',
'head',
'options',
'trace',
)
for method_name in tests:
with self.subTest(method=method_name):
async def async_generic_view(request):
if request.method.lower() != method_name:
return HttpResponseNotAllowed(method_name)
return HttpResponse(status=200)
method = getattr(self.request_factory, method_name)
request = method('/somewhere/')
response = await async_generic_view(request)
self.assertEqual(response.status_code, 200)