MCPcopy
hub / github.com/django/django / test_file_response

Method test_file_response

tests/asgi/tests.py:101–141  ·  view source on GitHub ↗

Makes sure that FileResponse works over ASGI.

(self)

Source from the content-addressed store, hash-verified

99 # test.
100 @ignore_warnings(module="django.core.handlers.asgi")
101 async def test_file_response(self):
102 """
103 Makes sure that FileResponse works over ASGI.
104 """
105 application = get_asgi_application()
106 # Construct HTTP request.
107 scope = self.async_request_factory._base_scope(path="/file/")
108 communicator = ApplicationCommunicator(application, scope)
109 await communicator.send_input({"type": "http.request"})
110 # Get the file content.
111 with open(test_filename, "rb") as test_file:
112 test_file_contents = test_file.read()
113 # Read the response.
114 with captured_stderr():
115 response_start = await communicator.receive_output()
116 self.assertEqual(response_start["type"], "http.response.start")
117 self.assertEqual(response_start["status"], 200)
118 headers = response_start["headers"]
119 self.assertEqual(len(headers), 3)
120 expected_headers = {
121 b"Content-Length": str(len(test_file_contents)).encode("ascii"),
122 b"Content-Type": b"text/x-python",
123 b"Content-Disposition": b'inline; filename="urls.py"',
124 }
125 for key, value in headers:
126 try:
127 self.assertEqual(value, expected_headers[key])
128 except AssertionError:
129 # Windows registry may not be configured with correct
130 # mimetypes.
131 if sys.platform == "win32" and key == b"Content-Type":
132 self.assertEqual(value, b"text/plain")
133 else:
134 raise
135
136 # Warning ignored here.
137 response_body = await communicator.receive_output()
138 self.assertEqual(response_body["type"], "http.response.body")
139 self.assertEqual(response_body["body"], test_file_contents)
140 # Allow response.close() to finish.
141 await communicator.wait()
142
143 @modify_settings(INSTALLED_APPS={"append": "django.contrib.staticfiles"})
144 @override_settings(

Callers

nothing calls this directly

Calls 5

get_asgi_applicationFunction · 0.90
captured_stderrFunction · 0.90
_base_scopeMethod · 0.80
readMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected