2012-06-07 16:08:47 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2013-07-01 20:49:07 +00:00
|
|
|
import unittest
|
|
|
|
|
2011-10-22 04:30:10 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
|
|
|
from django.core.servers.basehttp import get_internal_wsgi_application
|
2013-02-18 10:37:26 +00:00
|
|
|
from django.core.signals import request_started
|
2011-10-22 04:30:10 +00:00
|
|
|
from django.core.wsgi import get_wsgi_application
|
2013-02-18 10:37:26 +00:00
|
|
|
from django.db import close_old_connections
|
2013-12-23 15:01:13 +00:00
|
|
|
from django.test import TestCase, override_settings
|
2011-10-22 04:30:10 +00:00
|
|
|
from django.test.client import RequestFactory
|
2013-07-01 20:49:07 +00:00
|
|
|
from django.utils import six
|
2011-10-22 04:30:10 +00:00
|
|
|
|
|
|
|
|
2016-02-08 14:52:39 +00:00
|
|
|
@override_settings(ROOT_URLCONF='wsgi.urls')
|
2011-10-22 04:30:10 +00:00
|
|
|
class WSGITest(TestCase):
|
|
|
|
|
2013-02-18 10:37:26 +00:00
|
|
|
def setUp(self):
|
|
|
|
request_started.disconnect(close_old_connections)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
request_started.connect(close_old_connections)
|
|
|
|
|
2011-10-22 04:30:10 +00:00
|
|
|
def test_get_wsgi_application(self):
|
|
|
|
"""
|
|
|
|
Verify that ``get_wsgi_application`` returns a functioning WSGI
|
|
|
|
callable.
|
|
|
|
"""
|
|
|
|
application = get_wsgi_application()
|
|
|
|
|
|
|
|
environ = RequestFactory()._base_environ(
|
|
|
|
PATH_INFO="/",
|
|
|
|
CONTENT_TYPE="text/html; charset=utf-8",
|
|
|
|
REQUEST_METHOD="GET"
|
2013-10-18 09:02:43 +00:00
|
|
|
)
|
2011-10-22 04:30:10 +00: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(
|
|
|
|
response_data["headers"],
|
|
|
|
[('Content-Type', 'text/html; charset=utf-8')])
|
|
|
|
self.assertEqual(
|
2012-08-14 07:44:04 +00:00
|
|
|
bytes(response),
|
|
|
|
b"Content-Type: text/html; charset=utf-8\r\n\r\nHello World!")
|
2011-10-22 04:30:10 +00:00
|
|
|
|
2015-01-03 17:06:24 +00:00
|
|
|
def test_file_wrapper(self):
|
|
|
|
"""
|
|
|
|
Verify that FileResponse uses wsgi.file_wrapper.
|
|
|
|
"""
|
|
|
|
class FileWrapper(object):
|
|
|
|
def __init__(self, filelike, blksize=8192):
|
|
|
|
filelike.close()
|
|
|
|
application = get_wsgi_application()
|
|
|
|
environ = RequestFactory()._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)
|
|
|
|
|
2011-10-22 04:30:10 +00:00
|
|
|
|
|
|
|
class GetInternalWSGIApplicationTest(unittest.TestCase):
|
2013-02-26 12:19:18 +00:00
|
|
|
@override_settings(WSGI_APPLICATION="wsgi.wsgi.application")
|
2011-10-22 04:30:10 +00: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 10:02:56 +00:00
|
|
|
self.assertIs(app, application)
|
2011-10-22 04:30:10 +00: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 10:21:07 +00:00
|
|
|
|
2011-10-22 04:30:10 +00:00
|
|
|
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()
|
|
|
|
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertIs(app, fake_app)
|
2011-10-22 04:30:10 +00:00
|
|
|
finally:
|
|
|
|
basehttp.get_wsgi_application = _orig_get_wsgi_app
|
|
|
|
|
2013-02-26 12:19:18 +00:00
|
|
|
@override_settings(WSGI_APPLICATION="wsgi.noexist.app")
|
2011-10-22 04:30:10 +00:00
|
|
|
def test_bad_module(self):
|
2012-09-07 17:17:09 +00:00
|
|
|
with six.assertRaisesRegex(self,
|
2013-11-26 09:43:46 +00:00
|
|
|
ImproperlyConfigured,
|
|
|
|
r"^WSGI application 'wsgi.noexist.app' could not be loaded; Error importing.*"):
|
2011-10-22 04:30:10 +00:00
|
|
|
|
2011-10-26 21:07:12 +00:00
|
|
|
get_internal_wsgi_application()
|
2011-10-22 04:30:10 +00:00
|
|
|
|
2013-02-26 12:19:18 +00:00
|
|
|
@override_settings(WSGI_APPLICATION="wsgi.wsgi.noexist")
|
2011-10-22 04:30:10 +00:00
|
|
|
def test_bad_name(self):
|
2012-09-07 17:17:09 +00:00
|
|
|
with six.assertRaisesRegex(self,
|
2013-11-26 09:43:46 +00:00
|
|
|
ImproperlyConfigured,
|
2014-01-20 20:15:14 +00:00
|
|
|
r"^WSGI application 'wsgi.wsgi.noexist' could not be loaded; Error importing.*"):
|
2011-10-22 04:30:10 +00:00
|
|
|
|
2011-10-26 21:07:12 +00:00
|
|
|
get_internal_wsgi_application()
|