2015-11-07 23:12:37 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2015-03-08 22:06:23 +08:00
|
|
|
from django.core.handlers.wsgi import WSGIHandler, WSGIRequest, get_script_name
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.signals import request_finished, request_started
|
2013-03-06 18:12:24 +08:00
|
|
|
from django.db import close_old_connections, connection
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.test import (
|
2015-04-18 05:38:20 +08:00
|
|
|
RequestFactory,
|
|
|
|
SimpleTestCase,
|
|
|
|
TransactionTestCase,
|
|
|
|
override_settings,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2011-04-22 20:15:52 +08:00
|
|
|
|
2012-12-30 22:19:22 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class HandlerTests(SimpleTestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
2011-01-15 07:18:21 +08:00
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
def setUp(self):
|
|
|
|
request_started.disconnect(close_old_connections)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
request_started.connect(close_old_connections)
|
|
|
|
|
2016-04-03 21:43:51 +08:00
|
|
|
def test_middleware_initialized(self):
|
2011-01-15 07:18:21 +08:00
|
|
|
handler = WSGIHandler()
|
2018-05-27 23:21:27 +08:00
|
|
|
self.assertIsNotNone(handler._middleware_chain)
|
2011-01-15 07:18:21 +08:00
|
|
|
|
2011-04-22 20:15:52 +08:00
|
|
|
def test_bad_path_info(self):
|
2016-08-12 21:31:18 +08:00
|
|
|
"""
|
|
|
|
A non-UTF-8 path populates PATH_INFO with an URL-encoded path and
|
|
|
|
produces a 404.
|
|
|
|
"""
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory.get("/").environ
|
2016-12-01 18:38:01 +08:00
|
|
|
environ["PATH_INFO"] = "\xed"
|
2011-04-22 20:15:52 +08:00
|
|
|
handler = WSGIHandler()
|
|
|
|
response = handler(environ, lambda *a, **k: None)
|
2016-08-12 21:31:18 +08:00
|
|
|
# The path of the request will be encoded to '/%ED'.
|
|
|
|
self.assertEqual(response.status_code, 404)
|
2012-12-30 22:19:22 +08:00
|
|
|
|
2013-09-08 00:41:34 +08:00
|
|
|
def test_non_ascii_query_string(self):
|
2014-07-13 01:37:59 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Non-ASCII query strings are properly decoded (#20530, #22996).
|
2014-07-13 01:37:59 +08:00
|
|
|
"""
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory.get("/").environ
|
2014-07-13 01:37:59 +08:00
|
|
|
raw_query_strings = [
|
2014-08-21 02:37:33 +08:00
|
|
|
b"want=caf%C3%A9", # This is the proper way to encode 'café'
|
|
|
|
b"want=caf\xc3\xa9", # UA forgot to quote bytes
|
|
|
|
b"want=caf%E9", # UA quoted, but not in UTF-8
|
|
|
|
# UA forgot to convert Latin-1 to UTF-8 and to quote (typical of
|
|
|
|
# MSIE).
|
|
|
|
b"want=caf\xe9",
|
2014-07-13 01:37:59 +08:00
|
|
|
]
|
|
|
|
got = []
|
|
|
|
for raw_query_string in raw_query_strings:
|
2016-12-01 18:38:01 +08:00
|
|
|
# Simulate http.server.BaseHTTPRequestHandler.parse_request
|
|
|
|
# handling of raw request.
|
|
|
|
environ["QUERY_STRING"] = str(raw_query_string, "iso-8859-1")
|
2014-07-13 01:37:59 +08:00
|
|
|
request = WSGIRequest(environ)
|
|
|
|
got.append(request.GET["want"])
|
2020-04-18 22:46:05 +08:00
|
|
|
# %E9 is converted to the Unicode replacement character by parse_qsl
|
2017-03-17 19:51:48 +08:00
|
|
|
self.assertEqual(got, ["café", "café", "caf\ufffd", "café"])
|
2013-09-08 00:41:34 +08:00
|
|
|
|
2013-09-07 23:25:43 +08:00
|
|
|
def test_non_ascii_cookie(self):
|
2016-10-27 15:53:39 +08:00
|
|
|
"""Non-ASCII cookies set in JavaScript are properly decoded (#20557)."""
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory.get("/").environ
|
2016-12-01 18:38:01 +08:00
|
|
|
raw_cookie = 'want="café"'.encode("utf-8").decode("iso-8859-1")
|
2013-09-07 23:43:44 +08:00
|
|
|
environ["HTTP_COOKIE"] = raw_cookie
|
2013-09-07 23:25:43 +08:00
|
|
|
request = WSGIRequest(environ)
|
2017-01-12 06:17:25 +08:00
|
|
|
self.assertEqual(request.COOKIES["want"], "café")
|
2013-09-07 23:25:43 +08:00
|
|
|
|
2014-10-13 02:53:19 +08:00
|
|
|
def test_invalid_unicode_cookie(self):
|
|
|
|
"""
|
|
|
|
Invalid cookie content should result in an absent cookie, but not in a
|
|
|
|
crash while trying to decode it (#23638).
|
|
|
|
"""
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory.get("/").environ
|
2014-10-13 02:53:19 +08:00
|
|
|
environ["HTTP_COOKIE"] = "x=W\x03c(h]\x8e"
|
|
|
|
request = WSGIRequest(environ)
|
2014-10-14 15:26:13 +08:00
|
|
|
# We don't test COOKIES content, as the result might differ between
|
|
|
|
# Python version because parsing invalid content became stricter in
|
|
|
|
# latest versions.
|
|
|
|
self.assertIsInstance(request.COOKIES, dict)
|
2014-10-13 02:53:19 +08:00
|
|
|
|
2014-11-22 04:47:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="handlers.urls")
|
|
|
|
def test_invalid_multipart_boundary(self):
|
|
|
|
"""
|
|
|
|
Invalid boundary string should produce a "Bad Request" response, not a
|
|
|
|
server error (#23887).
|
|
|
|
"""
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory.post("/malformed_post/").environ
|
2014-11-22 04:47:46 +08:00
|
|
|
environ["CONTENT_TYPE"] = "multipart/form-data; boundary=WRONG\x07"
|
|
|
|
handler = WSGIHandler()
|
|
|
|
response = handler(environ, lambda *a, **k: None)
|
|
|
|
# Expect "bad request" response
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
2012-12-30 22:19:22 +08:00
|
|
|
|
2017-02-05 03:19:52 +08:00
|
|
|
@override_settings(ROOT_URLCONF="handlers.urls", MIDDLEWARE=[])
|
2013-03-06 18:12:24 +08:00
|
|
|
class TransactionsPerRequestTests(TransactionTestCase):
|
2013-06-04 14:09:29 +08:00
|
|
|
available_apps = []
|
2013-03-06 18:12:24 +08:00
|
|
|
|
|
|
|
def test_no_transaction(self):
|
|
|
|
response = self.client.get("/in_transaction/")
|
|
|
|
self.assertContains(response, "False")
|
|
|
|
|
|
|
|
def test_auto_transaction(self):
|
|
|
|
old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"]
|
|
|
|
try:
|
|
|
|
connection.settings_dict["ATOMIC_REQUESTS"] = True
|
|
|
|
response = self.client.get("/in_transaction/")
|
|
|
|
finally:
|
|
|
|
connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests
|
|
|
|
self.assertContains(response, "True")
|
|
|
|
|
2020-02-13 06:15:00 +08:00
|
|
|
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
|
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
def test_no_auto_transaction(self):
|
|
|
|
old_atomic_requests = connection.settings_dict["ATOMIC_REQUESTS"]
|
|
|
|
try:
|
|
|
|
connection.settings_dict["ATOMIC_REQUESTS"] = True
|
|
|
|
response = self.client.get("/not_in_transaction/")
|
|
|
|
finally:
|
|
|
|
connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests
|
|
|
|
self.assertContains(response, "False")
|
2022-12-03 10:04:42 +08:00
|
|
|
try:
|
|
|
|
connection.settings_dict["ATOMIC_REQUESTS"] = True
|
|
|
|
response = self.client.get("/not_in_transaction_using_none/")
|
|
|
|
finally:
|
|
|
|
connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests
|
|
|
|
self.assertContains(response, "False")
|
|
|
|
try:
|
|
|
|
connection.settings_dict["ATOMIC_REQUESTS"] = True
|
|
|
|
response = self.client.get("/not_in_transaction_using_text/")
|
|
|
|
finally:
|
|
|
|
connection.settings_dict["ATOMIC_REQUESTS"] = old_atomic_requests
|
|
|
|
# The non_atomic_requests decorator is used for an incorrect table.
|
|
|
|
self.assertContains(response, "True")
|
2013-03-06 18:12:24 +08:00
|
|
|
|
2013-05-16 07:14:28 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="handlers.urls")
|
2015-04-18 05:38:20 +08:00
|
|
|
class SignalsTests(SimpleTestCase):
|
2012-12-30 22:19:22 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.signals = []
|
2013-11-21 09:03:02 +08:00
|
|
|
self.signaled_environ = None
|
2013-02-18 18:37:26 +08:00
|
|
|
request_started.connect(self.register_started)
|
|
|
|
request_finished.connect(self.register_finished)
|
2012-12-30 22:19:22 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-02-18 18:37:26 +08:00
|
|
|
request_started.disconnect(self.register_started)
|
|
|
|
request_finished.disconnect(self.register_finished)
|
2012-12-30 22:19:22 +08:00
|
|
|
|
|
|
|
def register_started(self, **kwargs):
|
|
|
|
self.signals.append("started")
|
2013-11-21 09:03:02 +08:00
|
|
|
self.signaled_environ = kwargs.get("environ")
|
2012-12-30 22:19:22 +08:00
|
|
|
|
|
|
|
def register_finished(self, **kwargs):
|
|
|
|
self.signals.append("finished")
|
|
|
|
|
|
|
|
def test_request_signals(self):
|
|
|
|
response = self.client.get("/regular/")
|
|
|
|
self.assertEqual(self.signals, ["started", "finished"])
|
|
|
|
self.assertEqual(response.content, b"regular content")
|
2013-11-21 09:03:02 +08:00
|
|
|
self.assertEqual(self.signaled_environ, response.wsgi_request.environ)
|
2012-12-30 22:19:22 +08:00
|
|
|
|
|
|
|
def test_request_signals_streaming_response(self):
|
|
|
|
response = self.client.get("/streaming/")
|
|
|
|
self.assertEqual(self.signals, ["started"])
|
2023-02-17 17:35:19 +08:00
|
|
|
self.assertEqual(b"".join(list(response)), b"streaming content")
|
2012-12-30 22:19:22 +08:00
|
|
|
self.assertEqual(self.signals, ["started", "finished"])
|
2013-05-16 07:14:28 +08:00
|
|
|
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
def empty_middleware(get_response):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF="handlers.urls")
|
2016-04-27 01:43:34 +08:00
|
|
|
class HandlerRequestTests(SimpleTestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
2013-05-16 07:14:28 +08:00
|
|
|
|
2020-02-13 06:15:00 +08:00
|
|
|
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)
|
|
|
|
|
2013-05-16 07:14:28 +08:00
|
|
|
def test_suspiciousop_in_view_returns_400(self):
|
|
|
|
response = self.client.get("/suspicious/")
|
|
|
|
self.assertEqual(response.status_code, 400)
|
2014-07-22 20:25:22 +08:00
|
|
|
|
2020-09-07 19:33:47 +08:00
|
|
|
def test_bad_request_in_view_returns_400(self):
|
|
|
|
response = self.client.get("/bad_request/")
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
2014-07-22 20:25:22 +08:00
|
|
|
def test_invalid_urls(self):
|
|
|
|
response = self.client.get("~%A9helloworld")
|
2019-01-04 10:21:55 +08:00
|
|
|
self.assertEqual(response.status_code, 404)
|
2021-01-19 15:35:16 +08:00
|
|
|
self.assertEqual(response.context["request_path"], "/~%25A9helloworld")
|
2014-07-22 20:25:22 +08:00
|
|
|
|
|
|
|
response = self.client.get("d%aao%aaw%aan%aal%aao%aaa%aad%aa/")
|
2019-01-04 10:21:55 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.context["request_path"],
|
|
|
|
"/d%25AAo%25AAw%25AAn%25AAl%25AAo%25AAa%25AAd%25AA",
|
|
|
|
)
|
2014-07-22 20:25:22 +08:00
|
|
|
|
|
|
|
response = self.client.get("/%E2%99%E2%99%A5/")
|
2019-01-04 10:21:55 +08:00
|
|
|
self.assertEqual(response.context["request_path"], "/%25E2%2599%E2%99%A5/")
|
2014-07-22 20:25:22 +08:00
|
|
|
|
|
|
|
response = self.client.get("/%E2%98%8E%E2%A9%E2%99%A5/")
|
2019-01-04 10:21:55 +08:00
|
|
|
self.assertEqual(
|
|
|
|
response.context["request_path"], "/%E2%98%8E%25E2%25A9%E2%99%A5/"
|
|
|
|
)
|
2014-07-22 20:25:22 +08:00
|
|
|
|
|
|
|
def test_environ_path_info_type(self):
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory.get("/%E2%A8%87%87%A5%E2%A8%A0").environ
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(environ["PATH_INFO"], str)
|
2015-03-08 22:06:23 +08:00
|
|
|
|
2016-04-27 01:43:34 +08:00
|
|
|
def test_handle_accepts_httpstatus_enum_value(self):
|
|
|
|
def start_response(status, headers):
|
|
|
|
start_response.status = status
|
|
|
|
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory.get("/httpstatus_enum/").environ
|
2016-04-27 01:43:34 +08:00
|
|
|
WSGIHandler()(environ, start_response)
|
|
|
|
self.assertEqual(start_response.status, "200 OK")
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
@override_settings(MIDDLEWARE=["handlers.tests.empty_middleware"])
|
|
|
|
def test_middleware_returns_none(self):
|
|
|
|
msg = "Middleware factory handlers.tests.empty_middleware returned None."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
self.client.get("/")
|
|
|
|
|
2018-11-24 08:19:02 +08:00
|
|
|
def test_no_response(self):
|
|
|
|
msg = (
|
|
|
|
"The view %s didn't return an HttpResponse object. It returned None "
|
|
|
|
"instead."
|
2022-02-04 15:08:27 +08:00
|
|
|
)
|
2018-11-24 08:19:02 +08:00
|
|
|
tests = (
|
|
|
|
("/no_response_fbv/", "handlers.views.no_response"),
|
|
|
|
("/no_response_cbv/", "handlers.views.NoResponse.__call__"),
|
|
|
|
)
|
|
|
|
for url, view in tests:
|
|
|
|
with self.subTest(url=url), self.assertRaisesMessage(
|
|
|
|
ValueError, msg % view
|
|
|
|
):
|
|
|
|
self.client.get(url)
|
|
|
|
|
2023-02-17 17:35:19 +08:00
|
|
|
def test_streaming(self):
|
|
|
|
response = self.client.get("/streaming/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(b"".join(list(response)), b"streaming content")
|
|
|
|
|
2023-02-16 07:16:51 +08:00
|
|
|
def test_async_streaming(self):
|
|
|
|
response = self.client.get("/async_streaming/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
msg = (
|
|
|
|
"StreamingHttpResponse must consume asynchronous iterators in order to "
|
|
|
|
"serve them synchronously. Use a synchronous iterator instead."
|
|
|
|
)
|
|
|
|
with self.assertWarnsMessage(Warning, msg):
|
|
|
|
self.assertEqual(b"".join(list(response)), b"streaming content")
|
|
|
|
|
2015-03-08 22:06:23 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class ScriptNameTests(SimpleTestCase):
|
2015-03-08 22:06:23 +08:00
|
|
|
def test_get_script_name(self):
|
|
|
|
# Regression test for #23173
|
|
|
|
# Test first without PATH_INFO
|
|
|
|
script_name = get_script_name({"SCRIPT_URL": "/foobar/"})
|
|
|
|
self.assertEqual(script_name, "/foobar/")
|
|
|
|
|
|
|
|
script_name = get_script_name({"SCRIPT_URL": "/foobar/", "PATH_INFO": "/"})
|
|
|
|
self.assertEqual(script_name, "/foobar")
|
2015-10-23 20:53:32 +08:00
|
|
|
|
|
|
|
def test_get_script_name_double_slashes(self):
|
|
|
|
"""
|
|
|
|
WSGI squashes multiple successive slashes in PATH_INFO, get_script_name
|
|
|
|
should take that into account when forming SCRIPT_NAME (#17133).
|
|
|
|
"""
|
|
|
|
script_name = get_script_name(
|
|
|
|
{
|
|
|
|
"SCRIPT_URL": "/mst/milestones//accounts/login//help",
|
|
|
|
"PATH_INFO": "/milestones/accounts/login/help",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
self.assertEqual(script_name, "/mst")
|
2020-02-13 06:15:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
2020-09-07 19:33:47 +08:00
|
|
|
async def test_bad_request_in_view_returns_400(self):
|
|
|
|
response = await self.async_client.get("/bad_request/")
|
|
|
|
self.assertEqual(response.status_code, 400)
|
|
|
|
|
2020-02-13 06:15:00 +08:00
|
|
|
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 = (
|
2020-04-08 02:32:54 +08:00
|
|
|
"The view handlers.views.CoroutineClearingView.__call__ didn't"
|
|
|
|
" return an HttpResponse object. It returned an unawaited"
|
|
|
|
" coroutine instead. You may need to add an 'await'"
|
|
|
|
" into your view."
|
2020-02-13 06:15:00 +08:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
await self.async_client.get("/unawaited/")
|
2023-02-17 17:35:19 +08:00
|
|
|
|
|
|
|
async def test_sync_streaming(self):
|
|
|
|
response = await self.async_client.get("/streaming/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
msg = (
|
|
|
|
"StreamingHttpResponse must consume synchronous iterators in order to "
|
|
|
|
"serve them asynchronously. Use an asynchronous iterator instead."
|
|
|
|
)
|
|
|
|
with self.assertWarnsMessage(Warning, msg):
|
|
|
|
self.assertEqual(
|
|
|
|
b"".join([chunk async for chunk in response]), b"streaming content"
|
|
|
|
)
|
2023-02-16 07:16:51 +08:00
|
|
|
|
|
|
|
async def test_async_streaming(self):
|
|
|
|
response = await self.async_client.get("/async_streaming/")
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(
|
|
|
|
b"".join([chunk async for chunk in response]), b"streaming content"
|
|
|
|
)
|