1.3.7
by Luke Faraone
Import upstream version 1.5 |
1 |
from __future__ import unicode_literals |
1.2.12
by Raphaël Hertzog
Import upstream version 1.4 |
2 |
|
3 |
from django.core.exceptions import ImproperlyConfigured |
|
4 |
from django.core.servers.basehttp import get_internal_wsgi_application |
|
5 |
from django.core.wsgi import get_wsgi_application |
|
6 |
from django.test import TestCase |
|
7 |
from django.test.client import RequestFactory |
|
8 |
from django.test.utils import override_settings |
|
1.3.7
by Luke Faraone
Import upstream version 1.5 |
9 |
from django.utils import six, unittest |
1.2.12
by Raphaël Hertzog
Import upstream version 1.4 |
10 |
|
11 |
||
12 |
class WSGITest(TestCase): |
|
13 |
urls = "regressiontests.wsgi.urls" |
|
14 |
||
15 |
def test_get_wsgi_application(self): |
|
16 |
"""
|
|
17 |
Verify that ``get_wsgi_application`` returns a functioning WSGI
|
|
18 |
callable.
|
|
19 |
||
20 |
"""
|
|
21 |
application = get_wsgi_application() |
|
22 |
||
23 |
environ = RequestFactory()._base_environ( |
|
24 |
PATH_INFO="/", |
|
25 |
CONTENT_TYPE="text/html; charset=utf-8", |
|
26 |
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 |
response_data["headers"], |
|
40 |
[('Content-Type', 'text/html; charset=utf-8')]) |
|
41 |
self.assertEqual( |
|
1.3.7
by Luke Faraone
Import upstream version 1.5 |
42 |
bytes(response), |
43 |
b"Content-Type: text/html; charset=utf-8\r\n\r\nHello World!") |
|
1.2.12
by Raphaël Hertzog
Import upstream version 1.4 |
44 |
|
45 |
||
46 |
class GetInternalWSGIApplicationTest(unittest.TestCase): |
|
47 |
@override_settings(WSGI_APPLICATION="regressiontests.wsgi.wsgi.application") |
|
48 |
def test_success(self): |
|
49 |
"""
|
|
50 |
If ``WSGI_APPLICATION`` is a dotted path, the referenced object is
|
|
51 |
returned.
|
|
52 |
||
53 |
"""
|
|
54 |
app = get_internal_wsgi_application() |
|
55 |
||
56 |
from .wsgi import application |
|
57 |
||
58 |
self.assertTrue(app is application) |
|
59 |
||
60 |
||
61 |
@override_settings(WSGI_APPLICATION=None) |
|
62 |
def test_default(self): |
|
63 |
"""
|
|
64 |
If ``WSGI_APPLICATION`` is ``None``, the return value of
|
|
65 |
``get_wsgi_application`` is returned.
|
|
66 |
||
67 |
"""
|
|
68 |
# Mock out get_wsgi_application so we know its return value is used
|
|
69 |
fake_app = object() |
|
70 |
def mock_get_wsgi_app(): |
|
71 |
return fake_app |
|
72 |
from django.core.servers import basehttp |
|
73 |
_orig_get_wsgi_app = basehttp.get_wsgi_application |
|
74 |
basehttp.get_wsgi_application = mock_get_wsgi_app |
|
75 |
||
76 |
try: |
|
77 |
app = get_internal_wsgi_application() |
|
78 |
||
79 |
self.assertTrue(app is fake_app) |
|
80 |
finally: |
|
81 |
basehttp.get_wsgi_application = _orig_get_wsgi_app |
|
82 |
||
83 |
||
84 |
@override_settings(WSGI_APPLICATION="regressiontests.wsgi.noexist.app") |
|
85 |
def test_bad_module(self): |
|
1.3.7
by Luke Faraone
Import upstream version 1.5 |
86 |
with six.assertRaisesRegex(self, |
1.2.12
by Raphaël Hertzog
Import upstream version 1.4 |
87 |
ImproperlyConfigured, |
88 |
r"^WSGI application 'regressiontests.wsgi.noexist.app' could not be loaded; could not import module 'regressiontests.wsgi.noexist':"): |
|
89 |
||
90 |
get_internal_wsgi_application() |
|
91 |
||
92 |
||
93 |
@override_settings(WSGI_APPLICATION="regressiontests.wsgi.wsgi.noexist") |
|
94 |
def test_bad_name(self): |
|
1.3.7
by Luke Faraone
Import upstream version 1.5 |
95 |
with six.assertRaisesRegex(self, |
1.2.12
by Raphaël Hertzog
Import upstream version 1.4 |
96 |
ImproperlyConfigured, |
97 |
r"^WSGI application 'regressiontests.wsgi.wsgi.noexist' could not be loaded; can't find 'noexist' in module 'regressiontests.wsgi.wsgi':"): |
|
98 |
||
99 |
get_internal_wsgi_application() |