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