tests.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. import asyncio
  2. import sys
  3. import threading
  4. from pathlib import Path
  5. from asgiref.testing import ApplicationCommunicator
  6. from django.contrib.staticfiles.handlers import ASGIStaticFilesHandler
  7. from django.core.asgi import get_asgi_application
  8. from django.core.signals import request_finished, request_started
  9. from django.db import close_old_connections
  10. from django.test import (
  11. AsyncRequestFactory,
  12. SimpleTestCase,
  13. ignore_warnings,
  14. modify_settings,
  15. override_settings,
  16. )
  17. from django.utils.http import http_date
  18. from .urls import sync_waiter, test_filename
  19. TEST_STATIC_ROOT = Path(__file__).parent / "project" / "static"
  20. @override_settings(ROOT_URLCONF="asgi.urls")
  21. class ASGITest(SimpleTestCase):
  22. async_request_factory = AsyncRequestFactory()
  23. def setUp(self):
  24. request_started.disconnect(close_old_connections)
  25. def tearDown(self):
  26. request_started.connect(close_old_connections)
  27. async def test_get_asgi_application(self):
  28. """
  29. get_asgi_application() returns a functioning ASGI callable.
  30. """
  31. application = get_asgi_application()
  32. # Construct HTTP request.
  33. scope = self.async_request_factory._base_scope(path="/")
  34. communicator = ApplicationCommunicator(application, scope)
  35. await communicator.send_input({"type": "http.request"})
  36. # Read the response.
  37. response_start = await communicator.receive_output()
  38. self.assertEqual(response_start["type"], "http.response.start")
  39. self.assertEqual(response_start["status"], 200)
  40. self.assertEqual(
  41. set(response_start["headers"]),
  42. {
  43. (b"Content-Length", b"12"),
  44. (b"Content-Type", b"text/html; charset=utf-8"),
  45. },
  46. )
  47. response_body = await communicator.receive_output()
  48. self.assertEqual(response_body["type"], "http.response.body")
  49. self.assertEqual(response_body["body"], b"Hello World!")
  50. # Allow response.close() to finish.
  51. await communicator.wait()
  52. # Python's file API is not async compatible. A third-party library such
  53. # as https://github.com/Tinche/aiofiles allows passing the file to
  54. # FileResponse as an async interator. With a sync iterator
  55. # StreamingHTTPResponse triggers a warning when iterating the file.
  56. # assertWarnsMessage is not async compatible, so ignore_warnings for the
  57. # test.
  58. @ignore_warnings(module="django.http.response")
  59. async def test_file_response(self):
  60. """
  61. Makes sure that FileResponse works over ASGI.
  62. """
  63. application = get_asgi_application()
  64. # Construct HTTP request.
  65. scope = self.async_request_factory._base_scope(path="/file/")
  66. communicator = ApplicationCommunicator(application, scope)
  67. await communicator.send_input({"type": "http.request"})
  68. # Get the file content.
  69. with open(test_filename, "rb") as test_file:
  70. test_file_contents = test_file.read()
  71. # Read the response.
  72. response_start = await communicator.receive_output()
  73. self.assertEqual(response_start["type"], "http.response.start")
  74. self.assertEqual(response_start["status"], 200)
  75. headers = response_start["headers"]
  76. self.assertEqual(len(headers), 3)
  77. expected_headers = {
  78. b"Content-Length": str(len(test_file_contents)).encode("ascii"),
  79. b"Content-Type": b"text/x-python",
  80. b"Content-Disposition": b'inline; filename="urls.py"',
  81. }
  82. for key, value in headers:
  83. try:
  84. self.assertEqual(value, expected_headers[key])
  85. except AssertionError:
  86. # Windows registry may not be configured with correct
  87. # mimetypes.
  88. if sys.platform == "win32" and key == b"Content-Type":
  89. self.assertEqual(value, b"text/plain")
  90. else:
  91. raise
  92. # Warning ignored here.
  93. response_body = await communicator.receive_output()
  94. self.assertEqual(response_body["type"], "http.response.body")
  95. self.assertEqual(response_body["body"], test_file_contents)
  96. # Allow response.close() to finish.
  97. await communicator.wait()
  98. @modify_settings(INSTALLED_APPS={"append": "django.contrib.staticfiles"})
  99. @override_settings(
  100. STATIC_URL="static/",
  101. STATIC_ROOT=TEST_STATIC_ROOT,
  102. STATICFILES_DIRS=[TEST_STATIC_ROOT],
  103. STATICFILES_FINDERS=[
  104. "django.contrib.staticfiles.finders.FileSystemFinder",
  105. ],
  106. )
  107. @ignore_warnings(module="django.http.response")
  108. async def test_static_file_response(self):
  109. application = ASGIStaticFilesHandler(get_asgi_application())
  110. # Construct HTTP request.
  111. scope = self.async_request_factory._base_scope(path="/static/file.txt")
  112. communicator = ApplicationCommunicator(application, scope)
  113. await communicator.send_input({"type": "http.request"})
  114. # Get the file content.
  115. file_path = TEST_STATIC_ROOT / "file.txt"
  116. with open(file_path, "rb") as test_file:
  117. test_file_contents = test_file.read()
  118. # Read the response.
  119. stat = file_path.stat()
  120. response_start = await communicator.receive_output()
  121. self.assertEqual(response_start["type"], "http.response.start")
  122. self.assertEqual(response_start["status"], 200)
  123. self.assertEqual(
  124. set(response_start["headers"]),
  125. {
  126. (b"Content-Length", str(len(test_file_contents)).encode("ascii")),
  127. (b"Content-Type", b"text/plain"),
  128. (b"Content-Disposition", b'inline; filename="file.txt"'),
  129. (b"Last-Modified", http_date(stat.st_mtime).encode("ascii")),
  130. },
  131. )
  132. response_body = await communicator.receive_output()
  133. self.assertEqual(response_body["type"], "http.response.body")
  134. self.assertEqual(response_body["body"], test_file_contents)
  135. # Allow response.close() to finish.
  136. await communicator.wait()
  137. async def test_headers(self):
  138. application = get_asgi_application()
  139. communicator = ApplicationCommunicator(
  140. application,
  141. self.async_request_factory._base_scope(
  142. path="/meta/",
  143. headers=[
  144. [b"content-type", b"text/plain; charset=utf-8"],
  145. [b"content-length", b"77"],
  146. [b"referer", b"Scotland"],
  147. [b"referer", b"Wales"],
  148. ],
  149. ),
  150. )
  151. await communicator.send_input({"type": "http.request"})
  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", b"19"),
  159. (b"Content-Type", b"text/plain; charset=utf-8"),
  160. },
  161. )
  162. response_body = await communicator.receive_output()
  163. self.assertEqual(response_body["type"], "http.response.body")
  164. self.assertEqual(response_body["body"], b"From Scotland,Wales")
  165. # Allow response.close() to finish
  166. await communicator.wait()
  167. async def test_post_body(self):
  168. application = get_asgi_application()
  169. scope = self.async_request_factory._base_scope(
  170. method="POST",
  171. path="/post/",
  172. query_string="echo=1",
  173. )
  174. communicator = ApplicationCommunicator(application, scope)
  175. await communicator.send_input({"type": "http.request", "body": b"Echo!"})
  176. response_start = await communicator.receive_output()
  177. self.assertEqual(response_start["type"], "http.response.start")
  178. self.assertEqual(response_start["status"], 200)
  179. response_body = await communicator.receive_output()
  180. self.assertEqual(response_body["type"], "http.response.body")
  181. self.assertEqual(response_body["body"], b"Echo!")
  182. async def test_untouched_request_body_gets_closed(self):
  183. application = get_asgi_application()
  184. scope = self.async_request_factory._base_scope(method="POST", path="/post/")
  185. communicator = ApplicationCommunicator(application, scope)
  186. await communicator.send_input({"type": "http.request"})
  187. response_start = await communicator.receive_output()
  188. self.assertEqual(response_start["type"], "http.response.start")
  189. self.assertEqual(response_start["status"], 204)
  190. response_body = await communicator.receive_output()
  191. self.assertEqual(response_body["type"], "http.response.body")
  192. self.assertEqual(response_body["body"], b"")
  193. # Allow response.close() to finish
  194. await communicator.wait()
  195. async def test_get_query_string(self):
  196. application = get_asgi_application()
  197. for query_string in (b"name=Andrew", "name=Andrew"):
  198. with self.subTest(query_string=query_string):
  199. scope = self.async_request_factory._base_scope(
  200. path="/",
  201. query_string=query_string,
  202. )
  203. communicator = ApplicationCommunicator(application, scope)
  204. await communicator.send_input({"type": "http.request"})
  205. response_start = await communicator.receive_output()
  206. self.assertEqual(response_start["type"], "http.response.start")
  207. self.assertEqual(response_start["status"], 200)
  208. response_body = await communicator.receive_output()
  209. self.assertEqual(response_body["type"], "http.response.body")
  210. self.assertEqual(response_body["body"], b"Hello Andrew!")
  211. # Allow response.close() to finish
  212. await communicator.wait()
  213. async def test_disconnect(self):
  214. application = get_asgi_application()
  215. scope = self.async_request_factory._base_scope(path="/")
  216. communicator = ApplicationCommunicator(application, scope)
  217. await communicator.send_input({"type": "http.disconnect"})
  218. with self.assertRaises(asyncio.TimeoutError):
  219. await communicator.receive_output()
  220. async def test_wrong_connection_type(self):
  221. application = get_asgi_application()
  222. scope = self.async_request_factory._base_scope(path="/", type="other")
  223. communicator = ApplicationCommunicator(application, scope)
  224. await communicator.send_input({"type": "http.request"})
  225. msg = "Django can only handle ASGI/HTTP connections, not other."
  226. with self.assertRaisesMessage(ValueError, msg):
  227. await communicator.receive_output()
  228. async def test_non_unicode_query_string(self):
  229. application = get_asgi_application()
  230. scope = self.async_request_factory._base_scope(path="/", query_string=b"\xff")
  231. communicator = ApplicationCommunicator(application, scope)
  232. await communicator.send_input({"type": "http.request"})
  233. response_start = await communicator.receive_output()
  234. self.assertEqual(response_start["type"], "http.response.start")
  235. self.assertEqual(response_start["status"], 400)
  236. response_body = await communicator.receive_output()
  237. self.assertEqual(response_body["type"], "http.response.body")
  238. self.assertEqual(response_body["body"], b"")
  239. async def test_request_lifecycle_signals_dispatched_with_thread_sensitive(self):
  240. class SignalHandler:
  241. """Track threads handler is dispatched on."""
  242. threads = []
  243. def __call__(self, **kwargs):
  244. self.threads.append(threading.current_thread())
  245. signal_handler = SignalHandler()
  246. request_started.connect(signal_handler)
  247. request_finished.connect(signal_handler)
  248. # Perform a basic request.
  249. application = get_asgi_application()
  250. scope = self.async_request_factory._base_scope(path="/")
  251. communicator = ApplicationCommunicator(application, scope)
  252. await communicator.send_input({"type": "http.request"})
  253. response_start = await communicator.receive_output()
  254. self.assertEqual(response_start["type"], "http.response.start")
  255. self.assertEqual(response_start["status"], 200)
  256. response_body = await communicator.receive_output()
  257. self.assertEqual(response_body["type"], "http.response.body")
  258. self.assertEqual(response_body["body"], b"Hello World!")
  259. # Give response.close() time to finish.
  260. await communicator.wait()
  261. # AsyncToSync should have executed the signals in the same thread.
  262. request_started_thread, request_finished_thread = signal_handler.threads
  263. self.assertEqual(request_started_thread, request_finished_thread)
  264. request_started.disconnect(signal_handler)
  265. request_finished.disconnect(signal_handler)
  266. async def test_concurrent_async_uses_multiple_thread_pools(self):
  267. sync_waiter.active_threads.clear()
  268. # Send 2 requests concurrently
  269. application = get_asgi_application()
  270. scope = self.async_request_factory._base_scope(path="/wait/")
  271. communicators = []
  272. for _ in range(2):
  273. communicators.append(ApplicationCommunicator(application, scope))
  274. await communicators[-1].send_input({"type": "http.request"})
  275. # Each request must complete with a status code of 200
  276. # If requests aren't scheduled concurrently, the barrier in the
  277. # sync_wait view will time out, resulting in a 500 status code.
  278. for communicator in communicators:
  279. response_start = await communicator.receive_output()
  280. self.assertEqual(response_start["type"], "http.response.start")
  281. self.assertEqual(response_start["status"], 200)
  282. response_body = await communicator.receive_output()
  283. self.assertEqual(response_body["type"], "http.response.body")
  284. self.assertEqual(response_body["body"], b"Hello World!")
  285. # Give response.close() time to finish.
  286. await communicator.wait()
  287. # The requests should have scheduled on different threads. Note
  288. # active_threads is a set (a thread can only appear once), therefore
  289. # length is a sufficient check.
  290. self.assertEqual(len(sync_waiter.active_threads), 2)
  291. sync_waiter.active_threads.clear()