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