tests.py 4.1 KB

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