2011-10-22 12:30:10 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.core.servers.basehttp import get_internal_wsgi_application
|
2013-02-18 18:37:26 +08:00
|
|
|
from django.core.signals import request_started
|
2011-10-22 12:30:10 +08:00
|
|
|
from django.core.wsgi import get_wsgi_application
|
2013-02-18 18:37:26 +08:00
|
|
|
from django.db import close_old_connections
|
2019-07-25 01:06:01 +08:00
|
|
|
from django.http import FileResponse
|
2016-04-08 10:04:45 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2011-10-22 12:30:10 +08:00
|
|
|
from django.test.client import RequestFactory
|
|
|
|
|
|
|
|
|
2016-02-08 22:52:39 +08:00
|
|
|
@override_settings(ROOT_URLCONF="wsgi.urls")
|
2016-04-08 10:04:45 +08:00
|
|
|
class WSGITest(SimpleTestCase):
|
2018-11-27 03:01:27 +08:00
|
|
|
request_factory = RequestFactory()
|
2011-10-22 12:30:10 +08:00
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
def setUp(self):
|
|
|
|
request_started.disconnect(close_old_connections)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
request_started.connect(close_old_connections)
|
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
def test_get_wsgi_application(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
get_wsgi_application() returns a functioning WSGI callable.
|
2011-10-22 12:30:10 +08:00
|
|
|
"""
|
|
|
|
application = get_wsgi_application()
|
|
|
|
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory._base_environ(
|
2011-10-22 12:30:10 +08:00
|
|
|
PATH_INFO="/", CONTENT_TYPE="text/html; charset=utf-8", REQUEST_METHOD="GET"
|
2013-10-18 17:02:43 +08:00
|
|
|
)
|
2011-10-22 12:30:10 +08:00
|
|
|
|
|
|
|
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(
|
2016-06-18 16:51:38 +08:00
|
|
|
set(response_data["headers"]),
|
|
|
|
{("Content-Length", "12"), ("Content-Type", "text/html; charset=utf-8")},
|
|
|
|
)
|
2016-06-28 18:02:51 +08:00
|
|
|
self.assertIn(
|
|
|
|
bytes(response),
|
|
|
|
[
|
2016-06-18 16:51:38 +08:00
|
|
|
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!",
|
|
|
|
],
|
|
|
|
)
|
2011-10-22 12:30:10 +08:00
|
|
|
|
2015-01-04 01:06:24 +08:00
|
|
|
def test_file_wrapper(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
FileResponse uses wsgi.file_wrapper.
|
2015-01-04 01:06:24 +08:00
|
|
|
"""
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class FileWrapper:
|
2019-07-25 01:06:01 +08:00
|
|
|
def __init__(self, filelike, block_size=None):
|
|
|
|
self.block_size = block_size
|
2015-01-04 01:06:24 +08:00
|
|
|
filelike.close()
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-01-04 01:06:24 +08:00
|
|
|
application = get_wsgi_application()
|
2018-11-27 03:01:27 +08:00
|
|
|
environ = self.request_factory._base_environ(
|
2015-01-04 01:06:24 +08:00
|
|
|
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
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2015-01-04 01:06:24 +08:00
|
|
|
response = application(environ, start_response)
|
|
|
|
self.assertEqual(response_data["status"], "200 OK")
|
|
|
|
self.assertIsInstance(response, FileWrapper)
|
2019-07-25 01:06:01 +08:00
|
|
|
self.assertEqual(response.block_size, FileResponse.block_size)
|
2015-01-04 01:06:24 +08:00
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
class GetInternalWSGIApplicationTest(SimpleTestCase):
|
2013-02-26 20:19:18 +08:00
|
|
|
@override_settings(WSGI_APPLICATION="wsgi.wsgi.application")
|
2011-10-22 12:30:10 +08:00
|
|
|
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
|
|
|
|
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIs(app, application)
|
2011-10-22 12:30:10 +08:00
|
|
|
|
|
|
|
@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()
|
2013-10-22 18:21:07 +08:00
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
def mock_get_wsgi_app():
|
|
|
|
return fake_app
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
from django.core.servers import basehttp
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2011-10-22 12:30:10 +08:00
|
|
|
_orig_get_wsgi_app = basehttp.get_wsgi_application
|
|
|
|
basehttp.get_wsgi_application = mock_get_wsgi_app
|
|
|
|
|
|
|
|
try:
|
|
|
|
app = get_internal_wsgi_application()
|
|
|
|
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertIs(app, fake_app)
|
2011-10-22 12:30:10 +08:00
|
|
|
finally:
|
|
|
|
basehttp.get_wsgi_application = _orig_get_wsgi_app
|
|
|
|
|
2013-02-26 20:19:18 +08:00
|
|
|
@override_settings(WSGI_APPLICATION="wsgi.noexist.app")
|
2011-10-22 12:30:10 +08:00
|
|
|
def test_bad_module(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
msg = "WSGI application 'wsgi.noexist.app' could not be loaded; Error importing"
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2011-10-27 05:07:12 +08:00
|
|
|
get_internal_wsgi_application()
|
2011-10-22 12:30:10 +08:00
|
|
|
|
2013-02-26 20:19:18 +08:00
|
|
|
@override_settings(WSGI_APPLICATION="wsgi.wsgi.noexist")
|
2011-10-22 12:30:10 +08:00
|
|
|
def test_bad_name(self):
|
2016-04-08 10:04:45 +08:00
|
|
|
msg = (
|
|
|
|
"WSGI application 'wsgi.wsgi.noexist' could not be loaded; Error importing"
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2016-04-08 10:04:45 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2011-10-27 05:07:12 +08:00
|
|
|
get_internal_wsgi_application()
|