summaryrefslogtreecommitdiff
path: root/tests/test_client
diff options
context:
space:
mode:
authorAndrew Godwin <andrew@aeracode.org>2020-02-12 15:15:00 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-03-18 19:59:12 +0100
commitfc0fa72ff4cdbf5861a366e31cb8bbacd44da22d (patch)
treed419ce531586808b0a111664907b859cb6d22862 /tests/test_client
parent3f7e4b16bf58f99c71570ba75dc97db8265071be (diff)
downloaddjango-fc0fa72ff4cdbf5861a366e31cb8bbacd44da22d.tar.gz
Fixed #31224 -- Added support for asynchronous views and middleware.
This implements support for asynchronous views, asynchronous tests, asynchronous middleware, and an asynchronous test client.
Diffstat (limited to 'tests/test_client')
-rw-r--r--tests/test_client/tests.py59
-rw-r--r--tests/test_client/urls.py2
-rw-r--r--tests/test_client/views.py4
3 files changed, 63 insertions, 2 deletions
diff --git a/tests/test_client/tests.py b/tests/test_client/tests.py
index ce9ce40de5..93ac3e37a7 100644
--- a/tests/test_client/tests.py
+++ b/tests/test_client/tests.py
@@ -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)
diff --git a/tests/test_client/urls.py b/tests/test_client/urls.py
index 61cbe00547..16cca52c38 100644
--- a/tests/test_client/urls.py
+++ b/tests/test_client/urls.py
@@ -44,4 +44,6 @@ urlpatterns = [
path('accounts/no_trailing_slash', RedirectView.as_view(url='login/')),
path('accounts/login/', auth_views.LoginView.as_view(template_name='login.html')),
path('accounts/logout/', auth_views.LogoutView.as_view()),
+ # Async views.
+ path('async_get_view/', views.async_get_view, name='async_get_view'),
]
diff --git a/tests/test_client/views.py b/tests/test_client/views.py
index 2d076fafaf..c2aef76508 100644
--- a/tests/test_client/views.py
+++ b/tests/test_client/views.py
@@ -25,6 +25,10 @@ def get_view(request):
return HttpResponse(t.render(c))
+async def async_get_view(request):
+ return HttpResponse(b'GET content.')
+
+
def trace_view(request):
"""
A simple view that expects a TRACE request and echoes its status line.