tests.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from __future__ import with_statement
  2. from django.core.exceptions import ImproperlyConfigured
  3. from django.core.servers.basehttp import get_internal_wsgi_application
  4. from django.core.wsgi import get_wsgi_application
  5. from django.test import TestCase
  6. from django.test.client import RequestFactory
  7. from django.test.utils import override_settings
  8. from django.utils import unittest
  9. class WSGITest(TestCase):
  10. urls = "regressiontests.wsgi.urls"
  11. def test_get_wsgi_application(self):
  12. """
  13. Verify that ``get_wsgi_application`` returns a functioning WSGI
  14. callable.
  15. """
  16. application = get_wsgi_application()
  17. environ = RequestFactory()._base_environ(
  18. PATH_INFO="/",
  19. CONTENT_TYPE="text/html; charset=utf-8",
  20. REQUEST_METHOD="GET"
  21. )
  22. response_data = {}
  23. def start_response(status, headers):
  24. response_data["status"] = status
  25. response_data["headers"] = headers
  26. response = application(environ, start_response)
  27. self.assertEqual(response_data["status"], "200 OK")
  28. self.assertEqual(
  29. response_data["headers"],
  30. [('Content-Type', 'text/html; charset=utf-8')])
  31. self.assertEqual(
  32. unicode(response),
  33. u"Content-Type: text/html; charset=utf-8\n\nHello World!")
  34. class GetInternalWSGIApplicationTest(unittest.TestCase):
  35. @override_settings(WSGI_APPLICATION="regressiontests.wsgi.wsgi.application")
  36. def test_success(self):
  37. """
  38. If ``WSGI_APPLICATION`` is a dotted path, the referenced object is
  39. returned.
  40. """
  41. app = get_internal_wsgi_application()
  42. from .wsgi import application
  43. self.assertTrue(app is application)
  44. @override_settings(WSGI_APPLICATION=None)
  45. def test_default(self):
  46. """
  47. If ``WSGI_APPLICATION`` is ``None``, the return value of
  48. ``get_wsgi_application`` is returned.
  49. """
  50. # Mock out get_wsgi_application so we know its return value is used
  51. fake_app = object()
  52. def mock_get_wsgi_app():
  53. return fake_app
  54. from django.core.servers import basehttp
  55. _orig_get_wsgi_app = basehttp.get_wsgi_application
  56. basehttp.get_wsgi_application = mock_get_wsgi_app
  57. try:
  58. app = get_internal_wsgi_application()
  59. self.assertTrue(app is fake_app)
  60. finally:
  61. basehttp.get_wsgi_application = _orig_get_wsgi_app
  62. @override_settings(WSGI_APPLICATION="regressiontests.wsgi.noexist.app")
  63. def test_bad_module(self):
  64. with self.assertRaises(ImproperlyConfigured) as cm:
  65. get_internal_wsgi_application()
  66. self.assertEqual(
  67. str(cm.exception),
  68. "WSGI application 'regressiontests.wsgi.noexist.app' could not be loaded; could not import module 'regressiontests.wsgi.noexist': No module named noexist")
  69. @override_settings(WSGI_APPLICATION="regressiontests.wsgi.wsgi.noexist")
  70. def test_bad_name(self):
  71. with self.assertRaises(ImproperlyConfigured) as cm:
  72. get_internal_wsgi_application()
  73. self.assertEqual(
  74. str(cm.exception),
  75. "WSGI application 'regressiontests.wsgi.wsgi.noexist' could not be loaded; can't find 'noexist' in module 'regressiontests.wsgi.wsgi': 'module' object has no attribute 'noexist'")