~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to tests/wsgi/tests.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from __future__ import unicode_literals
 
2
 
 
3
from django.core.exceptions import ImproperlyConfigured
 
4
from django.core.servers.basehttp import get_internal_wsgi_application
 
5
from django.core.signals import request_started
 
6
from django.core.wsgi import get_wsgi_application
 
7
from django.db import close_old_connections
 
8
from django.test import TestCase
 
9
from django.test.client import RequestFactory
 
10
from django.test.utils import override_settings
 
11
from django.utils import six, unittest
 
12
 
 
13
 
 
14
class WSGITest(TestCase):
 
15
    urls = "wsgi.urls"
 
16
 
 
17
    def setUp(self):
 
18
        request_started.disconnect(close_old_connections)
 
19
 
 
20
    def tearDown(self):
 
21
        request_started.connect(close_old_connections)
 
22
 
 
23
    def test_get_wsgi_application(self):
 
24
        """
 
25
        Verify that ``get_wsgi_application`` returns a functioning WSGI
 
26
        callable.
 
27
 
 
28
        """
 
29
        application = get_wsgi_application()
 
30
 
 
31
        environ = RequestFactory()._base_environ(
 
32
            PATH_INFO="/",
 
33
            CONTENT_TYPE="text/html; charset=utf-8",
 
34
            REQUEST_METHOD="GET"
 
35
            )
 
36
 
 
37
        response_data = {}
 
38
 
 
39
        def start_response(status, headers):
 
40
            response_data["status"] = status
 
41
            response_data["headers"] = headers
 
42
 
 
43
        response = application(environ, start_response)
 
44
 
 
45
        self.assertEqual(response_data["status"], "200 OK")
 
46
        self.assertEqual(
 
47
            response_data["headers"],
 
48
            [('Content-Type', 'text/html; charset=utf-8')])
 
49
        self.assertEqual(
 
50
            bytes(response),
 
51
            b"Content-Type: text/html; charset=utf-8\r\n\r\nHello World!")
 
52
 
 
53
 
 
54
class GetInternalWSGIApplicationTest(unittest.TestCase):
 
55
    @override_settings(WSGI_APPLICATION="wsgi.wsgi.application")
 
56
    def test_success(self):
 
57
        """
 
58
        If ``WSGI_APPLICATION`` is a dotted path, the referenced object is
 
59
        returned.
 
60
 
 
61
        """
 
62
        app = get_internal_wsgi_application()
 
63
 
 
64
        from .wsgi import application
 
65
 
 
66
        self.assertTrue(app is application)
 
67
 
 
68
 
 
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
        """
 
76
        # Mock out get_wsgi_application so we know its return value is used
 
77
        fake_app = object()
 
78
        def mock_get_wsgi_app():
 
79
            return fake_app
 
80
        from django.core.servers import basehttp
 
81
        _orig_get_wsgi_app = basehttp.get_wsgi_application
 
82
        basehttp.get_wsgi_application = mock_get_wsgi_app
 
83
 
 
84
        try:
 
85
            app = get_internal_wsgi_application()
 
86
 
 
87
            self.assertTrue(app is fake_app)
 
88
        finally:
 
89
            basehttp.get_wsgi_application = _orig_get_wsgi_app
 
90
 
 
91
 
 
92
    @override_settings(WSGI_APPLICATION="wsgi.noexist.app")
 
93
    def test_bad_module(self):
 
94
        with six.assertRaisesRegex(self,
 
95
            ImproperlyConfigured,
 
96
            r"^WSGI application 'wsgi.noexist.app' could not be loaded; Error importing.*"):
 
97
 
 
98
            get_internal_wsgi_application()
 
99
 
 
100
 
 
101
    @override_settings(WSGI_APPLICATION="wsgi.wsgi.noexist")
 
102
    def test_bad_name(self):
 
103
        with six.assertRaisesRegex(self,
 
104
            ImproperlyConfigured,
 
105
            r"^WSGI application 'wsgi.wsgi.noexist' could not be loaded; Module.*"):
 
106
 
 
107
            get_internal_wsgi_application()