Debian Codegraph @

tests/wsgi/tests.py

raw
1 from django.core.exceptions import ImproperlyConfigured
2 from django.core.servers.basehttp import get_internal_wsgi_application
3 from django.core.signals import request_started
4 from django.core.wsgi import get_wsgi_application
5 from django.db import close_old_connections
6 from django.http import FileResponse
7 from django.test import SimpleTestCase, override_settings
8 from django.test.client import RequestFactory
9
10
11 @override_settings(ROOT_URLCONF="wsgi.urls")
12 class WSGITest(SimpleTestCase):
13 request_factory = RequestFactory()
14
15 def setUp(self):
16 request_started.disconnect(close_old_connections)
17 self.addCleanup(request_started.connect, close_old_connections)
18
19 def test_get_wsgi_application(self):
20 """
21 get_wsgi_application() returns a functioning WSGI callable.
22 """
23 application = get_wsgi_application()
24
25 environ = self.request_factory._base_environ(
26 PATH_INFO="/", CONTENT_TYPE="text/html; charset=utf-8", REQUEST_METHOD="GET"
27 )
28
29 response_data = {}
30
31 def start_response(status, headers):
32 response_data["status"] = status
33 response_data["headers"] = headers
34
35 response = application(environ, start_response)
36
37 self.assertEqual(response_data["status"], "200 OK")
38 self.assertEqual(
39 set(response_data["headers"]),
40 {("Content-Length", "12"), ("Content-Type", "text/html; charset=utf-8")},
41 )
42 self.assertIn(
43 bytes(response),
44 [
45 b"Content-Length: 12\r\nContent-Type: text/html; "
46 b"charset=utf-8\r\n\r\nHello World!",
47 b"Content-Type: text/html; "
48 b"charset=utf-8\r\nContent-Length: 12\r\n\r\nHello World!",
49 ],
50 )
51
52 def test_file_wrapper(self):
53 """
54 FileResponse uses wsgi.file_wrapper.
55 """
56
57 class FileWrapper:
58 def __init__(self, filelike, block_size=None):
59 self.block_size = block_size
60 filelike.close()
61
62 application = get_wsgi_application()
63 environ = self.request_factory._base_environ(
64 PATH_INFO="/file/",
65 REQUEST_METHOD="GET",
66 **{"wsgi.file_wrapper": FileWrapper},
67 )
68 response_data = {}
69
70 def start_response(status, headers):
71 response_data["status"] = status
72 response_data["headers"] = headers
73
74 response = application(environ, start_response)
75 self.assertEqual(response_data["status"], "200 OK")
76 self.assertIsInstance(response, FileWrapper)
77 self.assertEqual(response.block_size, FileResponse.block_size)
78
79
80 class GetInternalWSGIApplicationTest(SimpleTestCase):
81 @override_settings(WSGI_APPLICATION="wsgi.wsgi.application")
82 def test_success(self):
83 """
84 If ``WSGI_APPLICATION`` is a dotted path, the referenced object is
85 returned.
86 """
87 app = get_internal_wsgi_application()
88
89 from .wsgi import application
90
91 self.assertIs(app, application)
92
93 @override_settings(WSGI_APPLICATION=None)
94 def test_default(self):
95 """
96 If ``WSGI_APPLICATION`` is ``None``, the return value of
97 ``get_wsgi_application`` is returned.
98 """
99 # Mock out get_wsgi_application so we know its return value is used
100 fake_app = object()
101
102 def mock_get_wsgi_app():
103 return fake_app
104
105 from django.core.servers import basehttp
106
107 _orig_get_wsgi_app = basehttp.get_wsgi_application
108 basehttp.get_wsgi_application = mock_get_wsgi_app
109
110 try:
111 app = get_internal_wsgi_application()
112
113 self.assertIs(app, fake_app)
114 finally:
115 basehttp.get_wsgi_application = _orig_get_wsgi_app
116
117 @override_settings(WSGI_APPLICATION="wsgi.noexist.app")
118 def test_bad_module(self):
119 msg = "WSGI application 'wsgi.noexist.app' could not be loaded; Error importing"
120 with self.assertRaisesMessage(ImproperlyConfigured, msg):
121 get_internal_wsgi_application()
122
123 @override_settings(WSGI_APPLICATION="wsgi.wsgi.noexist")
124 def test_bad_name(self):
125 msg = (
126 "WSGI application 'wsgi.wsgi.noexist' could not be loaded; Error importing"
127 )
128 with self.assertRaisesMessage(ImproperlyConfigured, msg):
129 get_internal_wsgi_application()