1
from __future__ import unicode_literals
3
from django.core.exceptions import ImproperlyConfigured
4
from django.core.servers.basehttp import get_internal_wsgi_application
5
from django.core.signals import request_started
6
from django.core.wsgi import get_wsgi_application
7
from django.db import close_old_connections
8
from django.test import TestCase
9
from django.test.client import RequestFactory
10
from django.test.utils import override_settings
11
from django.utils import six, unittest
14
class WSGITest(TestCase):
18
request_started.disconnect(close_old_connections)
21
request_started.connect(close_old_connections)
23
def test_get_wsgi_application(self):
25
Verify that ``get_wsgi_application`` returns a functioning WSGI
29
application = get_wsgi_application()
31
environ = RequestFactory()._base_environ(
33
CONTENT_TYPE="text/html; charset=utf-8",
39
def start_response(status, headers):
40
response_data["status"] = status
41
response_data["headers"] = headers
43
response = application(environ, start_response)
45
self.assertEqual(response_data["status"], "200 OK")
47
response_data["headers"],
48
[('Content-Type', 'text/html; charset=utf-8')])
51
b"Content-Type: text/html; charset=utf-8\r\n\r\nHello World!")
54
class GetInternalWSGIApplicationTest(unittest.TestCase):
55
@override_settings(WSGI_APPLICATION="wsgi.wsgi.application")
56
def test_success(self):
58
If ``WSGI_APPLICATION`` is a dotted path, the referenced object is
62
app = get_internal_wsgi_application()
64
from .wsgi import application
66
self.assertTrue(app is application)
69
@override_settings(WSGI_APPLICATION=None)
70
def test_default(self):
72
If ``WSGI_APPLICATION`` is ``None``, the return value of
73
``get_wsgi_application`` is returned.
76
# Mock out get_wsgi_application so we know its return value is used
78
def mock_get_wsgi_app():
80
from django.core.servers import basehttp
81
_orig_get_wsgi_app = basehttp.get_wsgi_application
82
basehttp.get_wsgi_application = mock_get_wsgi_app
85
app = get_internal_wsgi_application()
87
self.assertTrue(app is fake_app)
89
basehttp.get_wsgi_application = _orig_get_wsgi_app
92
@override_settings(WSGI_APPLICATION="wsgi.noexist.app")
93
def test_bad_module(self):
94
with six.assertRaisesRegex(self,
96
r"^WSGI application 'wsgi.noexist.app' could not be loaded; Error importing.*"):
98
get_internal_wsgi_application()
101
@override_settings(WSGI_APPLICATION="wsgi.wsgi.noexist")
102
def test_bad_name(self):
103
with six.assertRaisesRegex(self,
104
ImproperlyConfigured,
105
r"^WSGI application 'wsgi.wsgi.noexist' could not be loaded; Module.*"):
107
get_internal_wsgi_application()