| 1 |
import asyncio |
| 2 |
import sys |
| 3 |
import threading |
| 4 |
import time |
| 5 |
from io import BytesIO |
| 6 |
from pathlib import Path |
| 7 |
|
| 8 |
from asgiref.sync import sync_to_async |
| 9 |
from asgiref.testing import ApplicationCommunicator |
| 10 |
|
| 11 |
from django.contrib.staticfiles.handlers import ASGIStaticFilesHandler |
| 12 |
from django.core.asgi import get_asgi_application |
| 13 |
from django.core.exceptions import RequestDataTooBig |
| 14 |
from django.core.files.uploadedfile import InMemoryUploadedFile |
| 15 |
from django.core.handlers.asgi import ASGIHandler, ASGIRequest |
| 16 |
from django.core.signals import request_finished, request_started |
| 17 |
from django.db import close_old_connections |
| 18 |
from django.http import HttpResponse, StreamingHttpResponse |
| 19 |
from django.test import ( |
| 20 |
AsyncRequestFactory, |
| 21 |
SimpleTestCase, |
| 22 |
ignore_warnings, |
| 23 |
modify_settings, |
| 24 |
override_settings, |
| 25 |
) |
| 26 |
from django.test.utils import captured_stderr |
| 27 |
from django.urls import path |
| 28 |
from django.utils.http import http_date |
| 29 |
from django.views.decorators.csrf import csrf_exempt |
| 30 |
|
| 31 |
from .urls import sync_waiter, test_filename |
| 32 |
|
| 33 |
TEST_STATIC_ROOT = Path(__file__).parent / "project" / "static" |
| 34 |
TOO_MUCH_DATA_MSG = "Request body exceeded settings.DATA_UPLOAD_MAX_MEMORY_SIZE." |
| 35 |
|
| 36 |
|
| 37 |
class SignalHandler: |
| 38 |
"""Helper class to track threads and kwargs when signals are dispatched.""" |
| 39 |
|
| 40 |
def __init__(self): |
| 41 |
super().__init__() |
| 42 |
self.calls = [] |
| 43 |
|
| 44 |
def __call__(self, signal, **kwargs): |
| 45 |
self.calls.append({"thread": threading.current_thread(), "kwargs": kwargs}) |
| 46 |
|
| 47 |
|
| 48 |
@override_settings(ROOT_URLCONF="asgi.urls") |
| 49 |
class ASGITest(SimpleTestCase): |
| 50 |
async_request_factory = AsyncRequestFactory() |
| 51 |
|
| 52 |
def setUp(self): |
| 53 |
request_started.disconnect(close_old_connections) |
| 54 |
self.addCleanup(request_started.connect, close_old_connections) |
| 55 |
|
| 56 |
async def test_get_asgi_application(self): |
| 57 |
""" |
| 58 |
get_asgi_application() returns a functioning ASGI callable. |
| 59 |
""" |
| 60 |
application = get_asgi_application() |
| 61 |
# Construct HTTP request. |
| 62 |
scope = self.async_request_factory._base_scope(path="/") |
| 63 |
communicator = ApplicationCommunicator(application, scope) |
| 64 |
await communicator.send_input({"type": "http.request"}) |
| 65 |
# Read the response. |
| 66 |
response_start = await communicator.receive_output() |
| 67 |
self.assertEqual(response_start["type"], "http.response.start") |
| 68 |
self.assertEqual(response_start["status"], 200) |
| 69 |
self.assertEqual( |
| 70 |
set(response_start["headers"]), |
| 71 |
{ |
| 72 |
(b"Content-Length", b"12"), |
| 73 |
(b"Content-Type", b"text/html; charset=utf-8"), |
| 74 |
}, |
| 75 |
) |
| 76 |
response_body = await communicator.receive_output() |
| 77 |
self.assertEqual(response_body["type"], "http.response.body") |
| 78 |
self.assertEqual(response_body["body"], b"Hello World!") |
| 79 |
# Allow response.close() to finish. |
| 80 |
await communicator.wait() |
| 81 |
|
| 82 |
# Python's file API is not async compatible. A third-party library such |
| 83 |
# as https://github.com/Tinche/aiofiles allows passing the file to |
| 84 |
# FileResponse as an async iterator. With a sync iterator |
| 85 |
# StreamingHTTPResponse triggers a warning when iterating the file. |
| 86 |
# assertWarnsMessage is not async compatible, so ignore_warnings for the |
| 87 |
# test. |
| 88 |
@ignore_warnings(module="django.http.response") |
| 89 |
async def test_file_response(self): |
| 90 |
""" |
| 91 |
Makes sure that FileResponse works over ASGI. |
| 92 |
""" |
| 93 |
application = get_asgi_application() |
| 94 |
# Construct HTTP request. |
| 95 |
scope = self.async_request_factory._base_scope(path="/file/") |
| 96 |
communicator = ApplicationCommunicator(application, scope) |
| 97 |
await communicator.send_input({"type": "http.request"}) |
| 98 |
# Get the file content. |
| 99 |
with open(test_filename, "rb") as test_file: |
| 100 |
test_file_contents = test_file.read() |
| 101 |
# Read the response. |
| 102 |
with captured_stderr(): |
| 103 |
response_start = await communicator.receive_output() |
| 104 |
self.assertEqual(response_start["type"], "http.response.start") |
| 105 |
self.assertEqual(response_start["status"], 200) |
| 106 |
headers = response_start["headers"] |
| 107 |
self.assertEqual(len(headers), 3) |
| 108 |
expected_headers = { |
| 109 |
b"Content-Length": str(len(test_file_contents)).encode("ascii"), |
| 110 |
b"Content-Type": b"text/x-python", |
| 111 |
b"Content-Disposition": b'inline; filename="urls.py"', |
| 112 |
} |
| 113 |
for key, value in headers: |
| 114 |
try: |
| 115 |
self.assertEqual(value, expected_headers[key]) |
| 116 |
except AssertionError: |
| 117 |
# Windows registry may not be configured with correct |
| 118 |
# mimetypes. |
| 119 |
if sys.platform == "win32" and key == b"Content-Type": |
| 120 |
self.assertEqual(value, b"text/plain") |
| 121 |
else: |
| 122 |
raise |
| 123 |
|
| 124 |
# Warning ignored here. |
| 125 |
response_body = await communicator.receive_output() |
| 126 |
self.assertEqual(response_body["type"], "http.response.body") |
| 127 |
self.assertEqual(response_body["body"], test_file_contents) |
| 128 |
# Allow response.close() to finish. |
| 129 |
await communicator.wait() |
| 130 |
|
| 131 |
@modify_settings(INSTALLED_APPS={"append": "django.contrib.staticfiles"}) |
| 132 |
@override_settings( |
| 133 |
STATIC_URL="static/", |
| 134 |
STATIC_ROOT=TEST_STATIC_ROOT, |
| 135 |
STATICFILES_DIRS=[TEST_STATIC_ROOT], |
| 136 |
STATICFILES_FINDERS=[ |
| 137 |
"django.contrib.staticfiles.finders.FileSystemFinder", |
| 138 |
], |
| 139 |
) |
| 140 |
async def test_static_file_response(self): |
| 141 |
application = ASGIStaticFilesHandler(get_asgi_application()) |
| 142 |
# Construct HTTP request. |
| 143 |
scope = self.async_request_factory._base_scope(path="/static/file.txt") |
| 144 |
communicator = ApplicationCommunicator(application, scope) |
| 145 |
await communicator.send_input({"type": "http.request"}) |
| 146 |
# Get the file content. |
| 147 |
file_path = TEST_STATIC_ROOT / "file.txt" |
| 148 |
with open(file_path, "rb") as test_file: |
| 149 |
test_file_contents = test_file.read() |
| 150 |
# Read the response. |
| 151 |
stat = file_path.stat() |
| 152 |
response_start = await communicator.receive_output() |
| 153 |
self.assertEqual(response_start["type"], "http.response.start") |
| 154 |
self.assertEqual(response_start["status"], 200) |
| 155 |
self.assertEqual( |
| 156 |
set(response_start["headers"]), |
| 157 |
{ |
| 158 |
(b"Content-Length", str(len(test_file_contents)).encode("ascii")), |
| 159 |
(b"Content-Type", b"text/plain"), |
| 160 |
(b"Content-Disposition", b'inline; filename="file.txt"'), |
| 161 |
(b"Last-Modified", http_date(stat.st_mtime).encode("ascii")), |
| 162 |
}, |
| 163 |
) |
| 164 |
response_body = await communicator.receive_output() |
| 165 |
self.assertEqual(response_body["type"], "http.response.body") |
| 166 |
self.assertEqual(response_body["body"], test_file_contents) |
| 167 |
# Allow response.close() to finish. |
| 168 |
await communicator.wait() |
| 169 |
|
| 170 |
async def test_headers(self): |
| 171 |
application = get_asgi_application() |
| 172 |
communicator = ApplicationCommunicator( |
| 173 |
application, |
| 174 |
self.async_request_factory._base_scope( |
| 175 |
path="/meta/", |
| 176 |
headers=[ |
| 177 |
[b"content-type", b"text/plain; charset=utf-8"], |
| 178 |
[b"content-length", b"77"], |
| 179 |
[b"referer", b"Scotland"], |
| 180 |
[b"referer", b"Wales"], |
| 181 |
], |
| 182 |
), |
| 183 |
) |
| 184 |
await communicator.send_input({"type": "http.request"}) |
| 185 |
response_start = await communicator.receive_output() |
| 186 |
self.assertEqual(response_start["type"], "http.response.start") |
| 187 |
self.assertEqual(response_start["status"], 200) |
| 188 |
self.assertEqual( |
| 189 |
set(response_start["headers"]), |
| 190 |
{ |
| 191 |
(b"Content-Length", b"19"), |
| 192 |
(b"Content-Type", b"text/plain; charset=utf-8"), |
| 193 |
}, |
| 194 |
) |
| 195 |
response_body = await communicator.receive_output() |
| 196 |
self.assertEqual(response_body["type"], "http.response.body") |
| 197 |
self.assertEqual(response_body["body"], b"From Scotland,Wales") |
| 198 |
# Allow response.close() to finish |
| 199 |
await communicator.wait() |
| 200 |
|
| 201 |
async def test_post_body(self): |
| 202 |
application = get_asgi_application() |
| 203 |
scope = self.async_request_factory._base_scope( |
| 204 |
method="POST", |
| 205 |
path="/post/", |
| 206 |
query_string="echo=1", |
| 207 |
) |
| 208 |
communicator = ApplicationCommunicator(application, scope) |
| 209 |
await communicator.send_input({"type": "http.request", "body": b"Echo!"}) |
| 210 |
response_start = await communicator.receive_output() |
| 211 |
self.assertEqual(response_start["type"], "http.response.start") |
| 212 |
self.assertEqual(response_start["status"], 200) |
| 213 |
response_body = await communicator.receive_output() |
| 214 |
self.assertEqual(response_body["type"], "http.response.body") |
| 215 |
self.assertEqual(response_body["body"], b"Echo!") |
| 216 |
|
| 217 |
async def test_request_too_big_request_error(self): |
| 218 |
# Track request_finished signal. |
| 219 |
signal_handler = SignalHandler() |
| 220 |
request_finished.connect(signal_handler) |
| 221 |
self.addCleanup(request_finished.disconnect, signal_handler) |
| 222 |
|
| 223 |
# Request class that always fails creation with RequestDataTooBig. |
| 224 |
class TestASGIRequest(ASGIRequest): |
| 225 |
|
| 226 |
def __init__(self, scope, body_file): |
| 227 |
super().__init__(scope, body_file) |
| 228 |
raise RequestDataTooBig() |
| 229 |
|
| 230 |
# Handler to use the custom request class. |
| 231 |
class TestASGIHandler(ASGIHandler): |
| 232 |
request_class = TestASGIRequest |
| 233 |
|
| 234 |
application = TestASGIHandler() |
| 235 |
scope = self.async_request_factory._base_scope(path="/not-important/") |
| 236 |
communicator = ApplicationCommunicator(application, scope) |
| 237 |
|
| 238 |
# Initiate request. |
| 239 |
await communicator.send_input({"type": "http.request"}) |
| 240 |
# Give response.close() time to finish. |
| 241 |
await communicator.wait() |
| 242 |
|
| 243 |
self.assertEqual(len(signal_handler.calls), 1) |
| 244 |
self.assertNotEqual( |
| 245 |
signal_handler.calls[0]["thread"], threading.current_thread() |
| 246 |
) |
| 247 |
|
| 248 |
async def test_meta_not_modified_with_repeat_headers(self): |
| 249 |
scope = self.async_request_factory._base_scope(path="/", http_version="2.0") |
| 250 |
scope["headers"] = [(b"foo", b"bar")] * 200_000 |
| 251 |
|
| 252 |
setitem_count = 0 |
| 253 |
|
| 254 |
class InstrumentedDict(dict): |
| 255 |
def __setitem__(self, *args, **kwargs): |
| 256 |
nonlocal setitem_count |
| 257 |
setitem_count += 1 |
| 258 |
super().__setitem__(*args, **kwargs) |
| 259 |
|
| 260 |
class InstrumentedASGIRequest(ASGIRequest): |
| 261 |
@property |
| 262 |
def META(self): |
| 263 |
return self._meta |
| 264 |
|
| 265 |
@META.setter |
| 266 |
def META(self, value): |
| 267 |
self._meta = InstrumentedDict(**value) |
| 268 |
|
| 269 |
request = InstrumentedASGIRequest(scope, None) |
| 270 |
|
| 271 |
self.assertEqual(len(request.headers["foo"].split(",")), 200_000) |
| 272 |
self.assertLessEqual(setitem_count, 100) |
| 273 |
|
| 274 |
async def test_underscores_in_headers_ignored(self): |
| 275 |
scope = self.async_request_factory._base_scope(path="/", http_version="2.0") |
| 276 |
scope["headers"] = [(b"some_header", b"1")] |
| 277 |
request = ASGIRequest(scope, None) |
| 278 |
# No form of the header exists anywhere. |
| 279 |
self.assertNotIn("Some_Header", request.headers) |
| 280 |
self.assertNotIn("Some-Header", request.headers) |
| 281 |
self.assertNotIn("SOME_HEADER", request.META) |
| 282 |
self.assertNotIn("SOME-HEADER", request.META) |
| 283 |
self.assertNotIn("HTTP_SOME_HEADER", request.META) |
| 284 |
|
| 285 |
async def test_cancel_post_request_with_sync_processing(self): |
| 286 |
""" |
| 287 |
The request.body object should be available and readable in view |
| 288 |
code, even if the ASGIHandler cancels processing part way through. |
| 289 |
""" |
| 290 |
loop = asyncio.get_event_loop() |
| 291 |
# Events to monitor the view processing from the parent test code. |
| 292 |
view_started_event = asyncio.Event() |
| 293 |
view_finished_event = asyncio.Event() |
| 294 |
# Record received request body or exceptions raised in the test view |
| 295 |
outcome = [] |
| 296 |
|
| 297 |
# This view will run in a new thread because it is wrapped in |
| 298 |
# sync_to_async. The view consumes the POST body data after a short |
| 299 |
# delay. The test will cancel the request using http.disconnect during |
| 300 |
# the delay, but because this is a sync view the code runs to |
| 301 |
# completion. There should be no exceptions raised inside the view |
| 302 |
# code. |
| 303 |
@csrf_exempt |
| 304 |
@sync_to_async |
| 305 |
def post_view(request): |
| 306 |
try: |
| 307 |
loop.call_soon_threadsafe(view_started_event.set) |
| 308 |
time.sleep(0.1) |
| 309 |
# Do something to read request.body after pause |
| 310 |
outcome.append({"request_body": request.body}) |
| 311 |
return HttpResponse("ok") |
| 312 |
except Exception as e: |
| 313 |
outcome.append({"exception": e}) |
| 314 |
finally: |
| 315 |
loop.call_soon_threadsafe(view_finished_event.set) |
| 316 |
|
| 317 |
# Request class to use the view. |
| 318 |
class TestASGIRequest(ASGIRequest): |
| 319 |
urlconf = (path("post/", post_view),) |
| 320 |
|
| 321 |
# Handler to use request class. |
| 322 |
class TestASGIHandler(ASGIHandler): |
| 323 |
request_class = TestASGIRequest |
| 324 |
|
| 325 |
application = TestASGIHandler() |
| 326 |
scope = self.async_request_factory._base_scope( |
| 327 |
method="POST", |
| 328 |
path="/post/", |
| 329 |
) |
| 330 |
communicator = ApplicationCommunicator(application, scope) |
| 331 |
|
| 332 |
await communicator.send_input({"type": "http.request", "body": b"Body data!"}) |
| 333 |
|
| 334 |
# Wait until the view code has started, then send http.disconnect. |
| 335 |
await view_started_event.wait() |
| 336 |
await communicator.send_input({"type": "http.disconnect"}) |
| 337 |
# Wait until view code has finished. |
| 338 |
await view_finished_event.wait() |
| 339 |
with self.assertRaises(asyncio.TimeoutError): |
| 340 |
await communicator.receive_output() |
| 341 |
|
| 342 |
self.assertEqual(outcome, [{"request_body": b"Body data!"}]) |
| 343 |
|
| 344 |
async def test_untouched_request_body_gets_closed(self): |
| 345 |
application = get_asgi_application() |
| 346 |
scope = self.async_request_factory._base_scope(method="POST", path="/post/") |
| 347 |
communicator = ApplicationCommunicator(application, scope) |
| 348 |
await communicator.send_input({"type": "http.request"}) |
| 349 |
response_start = await communicator.receive_output() |
| 350 |
self.assertEqual(response_start["type"], "http.response.start") |
| 351 |
self.assertEqual(response_start["status"], 204) |
| 352 |
response_body = await communicator.receive_output() |
| 353 |
self.assertEqual(response_body["type"], "http.response.body") |
| 354 |
self.assertEqual(response_body["body"], b"") |
| 355 |
# Allow response.close() to finish |
| 356 |
await communicator.wait() |
| 357 |
|
| 358 |
async def test_get_query_string(self): |
| 359 |
application = get_asgi_application() |
| 360 |
for query_string in (b"name=Andrew", "name=Andrew"): |
| 361 |
with self.subTest(query_string=query_string): |
| 362 |
scope = self.async_request_factory._base_scope( |
| 363 |
path="/", |
| 364 |
query_string=query_string, |
| 365 |
) |
| 366 |
communicator = ApplicationCommunicator(application, scope) |
| 367 |
await communicator.send_input({"type": "http.request"}) |
| 368 |
response_start = await communicator.receive_output() |
| 369 |
self.assertEqual(response_start["type"], "http.response.start") |
| 370 |
self.assertEqual(response_start["status"], 200) |
| 371 |
response_body = await communicator.receive_output() |
| 372 |
self.assertEqual(response_body["type"], "http.response.body") |
| 373 |
self.assertEqual(response_body["body"], b"Hello Andrew!") |
| 374 |
# Allow response.close() to finish |
| 375 |
await communicator.wait() |
| 376 |
|
| 377 |
async def test_disconnect(self): |
| 378 |
application = get_asgi_application() |
| 379 |
scope = self.async_request_factory._base_scope(path="/") |
| 380 |
communicator = ApplicationCommunicator(application, scope) |
| 381 |
await communicator.send_input({"type": "http.disconnect"}) |
| 382 |
with self.assertRaises(asyncio.TimeoutError): |
| 383 |
await communicator.receive_output() |
| 384 |
|
| 385 |
async def test_disconnect_both_return(self): |
| 386 |
# Force both the disconnect listener and the task that sends the |
| 387 |
# response to finish at the same time. |
| 388 |
application = get_asgi_application() |
| 389 |
scope = self.async_request_factory._base_scope(path="/") |
| 390 |
communicator = ApplicationCommunicator(application, scope) |
| 391 |
await communicator.send_input({"type": "http.request", "body": b"some body"}) |
| 392 |
# Fetch response headers (this yields to asyncio and causes |
| 393 |
# ASGHandler.send_response() to dump the body of the response in the |
| 394 |
# queue). |
| 395 |
await communicator.receive_output() |
| 396 |
# Fetch response body (there's already some data queued up, so this |
| 397 |
# doesn't actually yield to the event loop, it just succeeds |
| 398 |
# instantly). |
| 399 |
await communicator.receive_output() |
| 400 |
# Send disconnect at the same time that response finishes (this just |
| 401 |
# puts some info in a queue, it doesn't have to yield to the event |
| 402 |
# loop). |
| 403 |
await communicator.send_input({"type": "http.disconnect"}) |
| 404 |
# Waiting for the communicator _does_ yield to the event loop, since |
| 405 |
# ASGIHandler.send_response() is still waiting to do response.close(). |
| 406 |
# It so happens that there are enough remaining yield points in both |
| 407 |
# tasks that they both finish while the loop is running. |
| 408 |
await communicator.wait() |
| 409 |
|
| 410 |
async def test_disconnect_with_body(self): |
| 411 |
application = get_asgi_application() |
| 412 |
scope = self.async_request_factory._base_scope(path="/") |
| 413 |
communicator = ApplicationCommunicator(application, scope) |
| 414 |
await communicator.send_input({"type": "http.request", "body": b"some body"}) |
| 415 |
await communicator.send_input({"type": "http.disconnect"}) |
| 416 |
with self.assertRaises(asyncio.TimeoutError): |
| 417 |
await communicator.receive_output() |
| 418 |
|
| 419 |
async def test_assert_in_listen_for_disconnect(self): |
| 420 |
application = get_asgi_application() |
| 421 |
scope = self.async_request_factory._base_scope(path="/") |
| 422 |
communicator = ApplicationCommunicator(application, scope) |
| 423 |
await communicator.send_input({"type": "http.request"}) |
| 424 |
await communicator.send_input({"type": "http.not_a_real_message"}) |
| 425 |
msg = "Invalid ASGI message after request body: http.not_a_real_message" |
| 426 |
with self.assertRaisesMessage(AssertionError, msg): |
| 427 |
await communicator.wait() |
| 428 |
|
| 429 |
async def test_delayed_disconnect_with_body(self): |
| 430 |
application = get_asgi_application() |
| 431 |
scope = self.async_request_factory._base_scope(path="/delayed_hello/") |
| 432 |
communicator = ApplicationCommunicator(application, scope) |
| 433 |
await communicator.send_input({"type": "http.request", "body": b"some body"}) |
| 434 |
await communicator.send_input({"type": "http.disconnect"}) |
| 435 |
with self.assertRaises(asyncio.TimeoutError): |
| 436 |
await communicator.receive_output() |
| 437 |
|
| 438 |
async def test_wrong_connection_type(self): |
| 439 |
application = get_asgi_application() |
| 440 |
scope = self.async_request_factory._base_scope(path="/", type="other") |
| 441 |
communicator = ApplicationCommunicator(application, scope) |
| 442 |
await communicator.send_input({"type": "http.request"}) |
| 443 |
msg = "Django can only handle ASGI/HTTP connections, not other." |
| 444 |
with self.assertRaisesMessage(ValueError, msg): |
| 445 |
await communicator.receive_output() |
| 446 |
|
| 447 |
async def test_non_unicode_query_string(self): |
| 448 |
application = get_asgi_application() |
| 449 |
scope = self.async_request_factory._base_scope(path="/", query_string=b"\xff") |
| 450 |
communicator = ApplicationCommunicator(application, scope) |
| 451 |
await communicator.send_input({"type": "http.request"}) |
| 452 |
response_start = await communicator.receive_output() |
| 453 |
self.assertEqual(response_start["type"], "http.response.start") |
| 454 |
self.assertEqual(response_start["status"], 400) |
| 455 |
response_body = await communicator.receive_output() |
| 456 |
self.assertEqual(response_body["type"], "http.response.body") |
| 457 |
self.assertEqual(response_body["body"], b"") |
| 458 |
|
| 459 |
async def test_request_lifecycle_signals_dispatched_with_thread_sensitive(self): |
| 460 |
# Track request_started and request_finished signals. |
| 461 |
signal_handler = SignalHandler() |
| 462 |
request_started.connect(signal_handler) |
| 463 |
self.addCleanup(request_started.disconnect, signal_handler) |
| 464 |
request_finished.connect(signal_handler) |
| 465 |
self.addCleanup(request_finished.disconnect, signal_handler) |
| 466 |
|
| 467 |
# Perform a basic request. |
| 468 |
application = get_asgi_application() |
| 469 |
scope = self.async_request_factory._base_scope(path="/") |
| 470 |
communicator = ApplicationCommunicator(application, scope) |
| 471 |
await communicator.send_input({"type": "http.request"}) |
| 472 |
response_start = await communicator.receive_output() |
| 473 |
self.assertEqual(response_start["type"], "http.response.start") |
| 474 |
self.assertEqual(response_start["status"], 200) |
| 475 |
response_body = await communicator.receive_output() |
| 476 |
self.assertEqual(response_body["type"], "http.response.body") |
| 477 |
self.assertEqual(response_body["body"], b"Hello World!") |
| 478 |
# Give response.close() time to finish. |
| 479 |
await communicator.wait() |
| 480 |
|
| 481 |
# AsyncToSync should have executed the signals in the same thread. |
| 482 |
self.assertEqual(len(signal_handler.calls), 2) |
| 483 |
request_started_call, request_finished_call = signal_handler.calls |
| 484 |
self.assertEqual( |
| 485 |
request_started_call["thread"], request_finished_call["thread"] |
| 486 |
) |
| 487 |
|
| 488 |
async def test_concurrent_async_uses_multiple_thread_pools(self): |
| 489 |
sync_waiter.active_threads.clear() |
| 490 |
|
| 491 |
# Send 2 requests concurrently |
| 492 |
application = get_asgi_application() |
| 493 |
scope = self.async_request_factory._base_scope(path="/wait/") |
| 494 |
communicators = [] |
| 495 |
for _ in range(2): |
| 496 |
communicators.append(ApplicationCommunicator(application, scope)) |
| 497 |
await communicators[-1].send_input({"type": "http.request"}) |
| 498 |
|
| 499 |
# Each request must complete with a status code of 200 |
| 500 |
# If requests aren't scheduled concurrently, the barrier in the |
| 501 |
# sync_wait view will time out, resulting in a 500 status code. |
| 502 |
for communicator in communicators: |
| 503 |
response_start = await communicator.receive_output() |
| 504 |
self.assertEqual(response_start["type"], "http.response.start") |
| 505 |
self.assertEqual(response_start["status"], 200) |
| 506 |
response_body = await communicator.receive_output() |
| 507 |
self.assertEqual(response_body["type"], "http.response.body") |
| 508 |
self.assertEqual(response_body["body"], b"Hello World!") |
| 509 |
# Give response.close() time to finish. |
| 510 |
await communicator.wait() |
| 511 |
|
| 512 |
# The requests should have scheduled on different threads. Note |
| 513 |
# active_threads is a set (a thread can only appear once), therefore |
| 514 |
# length is a sufficient check. |
| 515 |
self.assertEqual(len(sync_waiter.active_threads), 2) |
| 516 |
|
| 517 |
sync_waiter.active_threads.clear() |
| 518 |
|
| 519 |
async def test_asyncio_cancel_error(self): |
| 520 |
view_started = asyncio.Event() |
| 521 |
# Flag to check if the view was cancelled. |
| 522 |
view_did_cancel = False |
| 523 |
# Track request_finished signal. |
| 524 |
signal_handler = SignalHandler() |
| 525 |
request_finished.connect(signal_handler) |
| 526 |
self.addCleanup(request_finished.disconnect, signal_handler) |
| 527 |
|
| 528 |
# A view that will listen for the cancelled error. |
| 529 |
async def view(request): |
| 530 |
nonlocal view_did_cancel |
| 531 |
view_started.set() |
| 532 |
try: |
| 533 |
await asyncio.sleep(0.1) |
| 534 |
return HttpResponse("Hello World!") |
| 535 |
except asyncio.CancelledError: |
| 536 |
# Set the flag. |
| 537 |
view_did_cancel = True |
| 538 |
raise |
| 539 |
|
| 540 |
# Request class to use the view. |
| 541 |
class TestASGIRequest(ASGIRequest): |
| 542 |
urlconf = (path("cancel/", view),) |
| 543 |
|
| 544 |
# Handler to use request class. |
| 545 |
class TestASGIHandler(ASGIHandler): |
| 546 |
request_class = TestASGIRequest |
| 547 |
|
| 548 |
# Request cycle should complete since no disconnect was sent. |
| 549 |
application = TestASGIHandler() |
| 550 |
scope = self.async_request_factory._base_scope(path="/cancel/") |
| 551 |
communicator = ApplicationCommunicator(application, scope) |
| 552 |
await communicator.send_input({"type": "http.request"}) |
| 553 |
response_start = await communicator.receive_output() |
| 554 |
self.assertEqual(response_start["type"], "http.response.start") |
| 555 |
self.assertEqual(response_start["status"], 200) |
| 556 |
response_body = await communicator.receive_output() |
| 557 |
self.assertEqual(response_body["type"], "http.response.body") |
| 558 |
self.assertEqual(response_body["body"], b"Hello World!") |
| 559 |
# Give response.close() time to finish. |
| 560 |
await communicator.wait() |
| 561 |
self.assertIs(view_did_cancel, False) |
| 562 |
# Exactly one call to request_finished handler. |
| 563 |
self.assertEqual(len(signal_handler.calls), 1) |
| 564 |
handler_call = signal_handler.calls.pop() |
| 565 |
# It was NOT on the async thread. |
| 566 |
self.assertNotEqual(handler_call["thread"], threading.current_thread()) |
| 567 |
# The signal sender is the handler class. |
| 568 |
self.assertEqual(handler_call["kwargs"], {"sender": TestASGIHandler}) |
| 569 |
view_started.clear() |
| 570 |
|
| 571 |
# Request cycle with a disconnect before the view can respond. |
| 572 |
application = TestASGIHandler() |
| 573 |
scope = self.async_request_factory._base_scope(path="/cancel/") |
| 574 |
communicator = ApplicationCommunicator(application, scope) |
| 575 |
await communicator.send_input({"type": "http.request"}) |
| 576 |
# Let the view actually start. |
| 577 |
await view_started.wait() |
| 578 |
# Disconnect the client. |
| 579 |
await communicator.send_input({"type": "http.disconnect"}) |
| 580 |
# The handler should not send a response. |
| 581 |
with self.assertRaises(asyncio.TimeoutError): |
| 582 |
await communicator.receive_output() |
| 583 |
await communicator.wait() |
| 584 |
self.assertIs(view_did_cancel, True) |
| 585 |
# Exactly one call to request_finished handler. |
| 586 |
self.assertEqual(len(signal_handler.calls), 1) |
| 587 |
handler_call = signal_handler.calls.pop() |
| 588 |
# It was NOT on the async thread. |
| 589 |
self.assertNotEqual(handler_call["thread"], threading.current_thread()) |
| 590 |
# The signal sender is the handler class. |
| 591 |
self.assertEqual(handler_call["kwargs"], {"sender": TestASGIHandler}) |
| 592 |
|
| 593 |
async def test_asyncio_streaming_cancel_error(self): |
| 594 |
# Similar to test_asyncio_cancel_error(), but during a streaming |
| 595 |
# response. |
| 596 |
view_did_cancel = False |
| 597 |
# Track request_finished signals. |
| 598 |
signal_handler = SignalHandler() |
| 599 |
request_finished.connect(signal_handler) |
| 600 |
self.addCleanup(request_finished.disconnect, signal_handler) |
| 601 |
|
| 602 |
async def streaming_response(): |
| 603 |
nonlocal view_did_cancel |
| 604 |
try: |
| 605 |
await asyncio.sleep(0.2) |
| 606 |
yield b"Hello World!" |
| 607 |
except asyncio.CancelledError: |
| 608 |
# Set the flag. |
| 609 |
view_did_cancel = True |
| 610 |
raise |
| 611 |
|
| 612 |
async def view(request): |
| 613 |
return StreamingHttpResponse(streaming_response()) |
| 614 |
|
| 615 |
class TestASGIRequest(ASGIRequest): |
| 616 |
urlconf = (path("cancel/", view),) |
| 617 |
|
| 618 |
class TestASGIHandler(ASGIHandler): |
| 619 |
request_class = TestASGIRequest |
| 620 |
|
| 621 |
# With no disconnect, the request cycle should complete in the same |
| 622 |
# manner as the non-streaming response. |
| 623 |
application = TestASGIHandler() |
| 624 |
scope = self.async_request_factory._base_scope(path="/cancel/") |
| 625 |
communicator = ApplicationCommunicator(application, scope) |
| 626 |
await communicator.send_input({"type": "http.request"}) |
| 627 |
response_start = await communicator.receive_output() |
| 628 |
self.assertEqual(response_start["type"], "http.response.start") |
| 629 |
self.assertEqual(response_start["status"], 200) |
| 630 |
response_body = await communicator.receive_output() |
| 631 |
self.assertEqual(response_body["type"], "http.response.body") |
| 632 |
self.assertEqual(response_body["body"], b"Hello World!") |
| 633 |
await communicator.wait() |
| 634 |
self.assertIs(view_did_cancel, False) |
| 635 |
# Exactly one call to request_finished handler. |
| 636 |
self.assertEqual(len(signal_handler.calls), 1) |
| 637 |
handler_call = signal_handler.calls.pop() |
| 638 |
# It was NOT on the async thread. |
| 639 |
self.assertNotEqual(handler_call["thread"], threading.current_thread()) |
| 640 |
# The signal sender is the handler class. |
| 641 |
self.assertEqual(handler_call["kwargs"], {"sender": TestASGIHandler}) |
| 642 |
|
| 643 |
# Request cycle with a disconnect. |
| 644 |
application = TestASGIHandler() |
| 645 |
scope = self.async_request_factory._base_scope(path="/cancel/") |
| 646 |
communicator = ApplicationCommunicator(application, scope) |
| 647 |
await communicator.send_input({"type": "http.request"}) |
| 648 |
response_start = await communicator.receive_output() |
| 649 |
# Fetch the start of response so streaming can begin |
| 650 |
self.assertEqual(response_start["type"], "http.response.start") |
| 651 |
self.assertEqual(response_start["status"], 200) |
| 652 |
await asyncio.sleep(0.1) |
| 653 |
# Now disconnect the client. |
| 654 |
await communicator.send_input({"type": "http.disconnect"}) |
| 655 |
# This time the handler should not send a response. |
| 656 |
with self.assertRaises(asyncio.TimeoutError): |
| 657 |
await communicator.receive_output() |
| 658 |
await communicator.wait() |
| 659 |
self.assertIs(view_did_cancel, True) |
| 660 |
# Exactly one call to request_finished handler. |
| 661 |
self.assertEqual(len(signal_handler.calls), 1) |
| 662 |
handler_call = signal_handler.calls.pop() |
| 663 |
# It was NOT on the async thread. |
| 664 |
self.assertNotEqual(handler_call["thread"], threading.current_thread()) |
| 665 |
# The signal sender is the handler class. |
| 666 |
self.assertEqual(handler_call["kwargs"], {"sender": TestASGIHandler}) |
| 667 |
|
| 668 |
async def test_streaming(self): |
| 669 |
scope = self.async_request_factory._base_scope( |
| 670 |
path="/streaming/", query_string=b"sleep=0.001" |
| 671 |
) |
| 672 |
application = get_asgi_application() |
| 673 |
communicator = ApplicationCommunicator(application, scope) |
| 674 |
await communicator.send_input({"type": "http.request"}) |
| 675 |
# Fetch http.response.start. |
| 676 |
await communicator.receive_output(timeout=1) |
| 677 |
# Fetch the 'first' and 'last'. |
| 678 |
first_response = await communicator.receive_output(timeout=1) |
| 679 |
self.assertEqual(first_response["body"], b"first\n") |
| 680 |
second_response = await communicator.receive_output(timeout=1) |
| 681 |
self.assertEqual(second_response["body"], b"last\n") |
| 682 |
# Fetch the rest of the response so that coroutines are cleaned up. |
| 683 |
await communicator.receive_output(timeout=1) |
| 684 |
with self.assertRaises(asyncio.TimeoutError): |
| 685 |
await communicator.receive_output(timeout=1) |
| 686 |
|
| 687 |
async def test_streaming_disconnect(self): |
| 688 |
scope = self.async_request_factory._base_scope( |
| 689 |
path="/streaming/", query_string=b"sleep=0.1" |
| 690 |
) |
| 691 |
application = get_asgi_application() |
| 692 |
communicator = ApplicationCommunicator(application, scope) |
| 693 |
await communicator.send_input({"type": "http.request"}) |
| 694 |
await communicator.receive_output(timeout=1) |
| 695 |
first_response = await communicator.receive_output(timeout=1) |
| 696 |
self.assertEqual(first_response["body"], b"first\n") |
| 697 |
# Disconnect the client. |
| 698 |
await communicator.send_input({"type": "http.disconnect"}) |
| 699 |
# 'last\n' isn't sent. |
| 700 |
with self.assertRaises(asyncio.TimeoutError): |
| 701 |
await communicator.receive_output(timeout=0.2) |
| 702 |
|
| 703 |
|
| 704 |
class MaxMemorySizeASGITests(SimpleTestCase): |
| 705 |
def make_request( |
| 706 |
self, |
| 707 |
body, |
| 708 |
content_type=b"application/octet-stream", |
| 709 |
content_length=None, |
| 710 |
stream=None, |
| 711 |
): |
| 712 |
scope = AsyncRequestFactory()._base_scope(method="POST", path="/") |
| 713 |
scope["headers"] = [(b"content-type", content_type)] |
| 714 |
if content_length is not None: |
| 715 |
scope["headers"].append((b"content-length", str(content_length).encode())) |
| 716 |
return ASGIRequest(scope, stream if stream is not None else BytesIO(body)) |
| 717 |
|
| 718 |
def test_body_size_not_exceeded_without_content_length(self): |
| 719 |
body = b"x" * 5 |
| 720 |
with self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=5): |
| 721 |
self.assertEqual(self.make_request(body).body, body) |
| 722 |
|
| 723 |
def test_body_size_exceeded_without_content_length(self): |
| 724 |
request = self.make_request(b"x" * 10) |
| 725 |
with ( |
| 726 |
self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=5), |
| 727 |
self.assertRaisesMessage(RequestDataTooBig, TOO_MUCH_DATA_MSG), |
| 728 |
): |
| 729 |
request.body |
| 730 |
|
| 731 |
def test_body_size_check_fires_before_read(self): |
| 732 |
# The seekable size check rejects oversized bodies before reading |
| 733 |
# them into memory (i.e. before calling self.read()). |
| 734 |
class TrackingBytesIO(BytesIO): |
| 735 |
calls = [] |
| 736 |
|
| 737 |
def read(self, *args, **kwargs): |
| 738 |
self.calls.append((args, kwargs)) |
| 739 |
return super().read(*args, **kwargs) |
| 740 |
|
| 741 |
stream = TrackingBytesIO(b"x" * 10) |
| 742 |
request = self.make_request(b"x" * 10, stream=stream) |
| 743 |
with ( |
| 744 |
self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=5), |
| 745 |
self.assertRaisesMessage(RequestDataTooBig, TOO_MUCH_DATA_MSG), |
| 746 |
): |
| 747 |
request.body |
| 748 |
|
| 749 |
self.assertEqual(stream.calls, []) |
| 750 |
|
| 751 |
def test_post_size_exceeded_without_content_length(self): |
| 752 |
request = self.make_request( |
| 753 |
b"a=" + b"x" * 10, |
| 754 |
content_type=b"application/x-www-form-urlencoded", |
| 755 |
) |
| 756 |
with ( |
| 757 |
self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=5), |
| 758 |
self.assertRaisesMessage(RequestDataTooBig, TOO_MUCH_DATA_MSG), |
| 759 |
): |
| 760 |
request.POST |
| 761 |
|
| 762 |
def test_no_limit(self): |
| 763 |
body = b"x" * 100 |
| 764 |
with self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=None): |
| 765 |
self.assertEqual(self.make_request(body).body, body) |
| 766 |
|
| 767 |
async def test_read_body_no_limit(self): |
| 768 |
chunks = [ |
| 769 |
{"type": "http.request", "body": b"x" * 100, "more_body": True}, |
| 770 |
{"type": "http.request", "body": b"x" * 100, "more_body": False}, |
| 771 |
] |
| 772 |
|
| 773 |
async def receive(): |
| 774 |
return chunks.pop(0) |
| 775 |
|
| 776 |
handler = ASGIHandler() |
| 777 |
with self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=None): |
| 778 |
body_file = await handler.read_body(receive) |
| 779 |
self.addCleanup(body_file.close) |
| 780 |
|
| 781 |
body_file.seek(0) |
| 782 |
self.assertEqual(body_file.read(), b"x" * 200) |
| 783 |
|
| 784 |
def test_non_multipart_body_size_enforced(self): |
| 785 |
# DATA_UPLOAD_MAX_MEMORY_SIZE is enforced on non-multipart bodies. |
| 786 |
request = self.make_request(b"x" * 100) |
| 787 |
with ( |
| 788 |
self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=10), |
| 789 |
self.assertRaisesMessage(RequestDataTooBig, TOO_MUCH_DATA_MSG), |
| 790 |
): |
| 791 |
request.body |
| 792 |
|
| 793 |
def test_multipart_file_upload_not_limited_by_data_upload_max(self): |
| 794 |
# DATA_UPLOAD_MAX_MEMORY_SIZE applies to non-file fields only; a file |
| 795 |
# upload whose total body exceeds the limit must still succeed. |
| 796 |
boundary = "testboundary" |
| 797 |
file_content = b"x" * 100 |
| 798 |
body = ( |
| 799 |
( |
| 800 |
f"--{boundary}\r\n" |
| 801 |
f'Content-Disposition: form-data; name="file"; filename="test.txt"\r\n' |
| 802 |
f"Content-Type: application/octet-stream\r\n" |
| 803 |
f"\r\n" |
| 804 |
).encode() |
| 805 |
+ file_content |
| 806 |
+ f"\r\n--{boundary}--\r\n".encode() |
| 807 |
) |
| 808 |
request = self.make_request( |
| 809 |
body, |
| 810 |
content_type=f"multipart/form-data; boundary={boundary}".encode(), |
| 811 |
content_length=len(body), |
| 812 |
) |
| 813 |
with self.settings( |
| 814 |
DATA_UPLOAD_MAX_MEMORY_SIZE=10, FILE_UPLOAD_MAX_MEMORY_SIZE=10 |
| 815 |
): |
| 816 |
files = request.FILES |
| 817 |
self.assertEqual(len(files), 1) |
| 818 |
uploaded = files["file"] |
| 819 |
self.addCleanup(uploaded.close) |
| 820 |
self.assertEqual(uploaded.read(), file_content) |
| 821 |
|
| 822 |
def test_multipart_file_upload_limited_by_file_upload_max(self): |
| 823 |
boundary = "testboundary" |
| 824 |
file_content = b"x" * 100 |
| 825 |
body = ( |
| 826 |
( |
| 827 |
f"--{boundary}\r\n" |
| 828 |
f'Content-Disposition: form-data; name="file"; filename="test.txt"\r\n' |
| 829 |
f"Content-Type: application/octet-stream\r\n" |
| 830 |
f"\r\n" |
| 831 |
).encode() |
| 832 |
+ file_content |
| 833 |
+ f"\r\n--{boundary}--\r\n".encode() |
| 834 |
) |
| 835 |
# Provide an understated content-length. |
| 836 |
request = self.make_request( |
| 837 |
body, |
| 838 |
content_type=f"multipart/form-data; boundary={boundary}".encode(), |
| 839 |
content_length=9, |
| 840 |
) |
| 841 |
with self.settings(FILE_UPLOAD_MAX_MEMORY_SIZE=10): |
| 842 |
files = request.FILES |
| 843 |
self.assertEqual(len(files), 1) |
| 844 |
uploaded = files["file"] |
| 845 |
# The file is not loaded into memory. |
| 846 |
self.assertNotIsInstance(uploaded, InMemoryUploadedFile) |
| 847 |
self.addCleanup(uploaded.close) |
| 848 |
self.assertEqual(uploaded.read(), file_content) |
| 849 |
|
| 850 |
async def test_read_body_buffers_all_chunks(self): |
| 851 |
# read_body() consumes all chunks regardless of |
| 852 |
# DATA_UPLOAD_MAX_MEMORY_SIZE; the limit is enforced later when |
| 853 |
# HttpRequest.body is accessed. |
| 854 |
chunks = [ |
| 855 |
{"type": "http.request", "body": b"x" * 10, "more_body": True}, |
| 856 |
{"type": "http.request", "body": b"y" * 10, "more_body": True}, |
| 857 |
{"type": "http.request", "body": b"z" * 10, "more_body": False}, |
| 858 |
] |
| 859 |
|
| 860 |
async def receive(): |
| 861 |
return chunks.pop(0) |
| 862 |
|
| 863 |
handler = ASGIHandler() |
| 864 |
with self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=15): |
| 865 |
body_file = await handler.read_body(receive) |
| 866 |
self.addCleanup(body_file.close) |
| 867 |
|
| 868 |
self.assertEqual(len(chunks), 0) # All chunks were consumed. |
| 869 |
body_file.seek(0) |
| 870 |
self.assertEqual(body_file.read(), b"x" * 10 + b"y" * 10 + b"z" * 10) |
| 871 |
|
| 872 |
async def test_read_body_multipart_not_limited(self): |
| 873 |
# All chunks are consumed regardless of DATA_UPLOAD_MAX_MEMORY_SIZE; |
| 874 |
# multipart size enforcement happens inside MultiPartParser, not here. |
| 875 |
chunks = [ |
| 876 |
{"type": "http.request", "body": b"x" * 10, "more_body": True}, |
| 877 |
{"type": "http.request", "body": b"y" * 10, "more_body": True}, |
| 878 |
{"type": "http.request", "body": b"z" * 10, "more_body": False}, |
| 879 |
] |
| 880 |
|
| 881 |
async def receive(): |
| 882 |
return chunks.pop(0) |
| 883 |
|
| 884 |
handler = ASGIHandler() |
| 885 |
with self.settings(DATA_UPLOAD_MAX_MEMORY_SIZE=15): |
| 886 |
body_file = await handler.read_body(receive) |
| 887 |
self.addCleanup(body_file.close) |
| 888 |
|
| 889 |
self.assertEqual(len(chunks), 0) # All chunks were consumed. |
| 890 |
body_file.seek(0) |
| 891 |
self.assertEqual(body_file.read(), b"x" * 10 + b"y" * 10 + b"z" * 10) |