File: test_wsgi.py

package info (click to toggle)
python-pyramid 1.10.2+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,052 kB
  • sloc: python: 42,982; makefile: 41
file content (139 lines) | stat: -rw-r--r-- 4,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import unittest


class WSGIAppTests(unittest.TestCase):
    def _callFUT(self, app):
        from pyramid.wsgi import wsgiapp

        return wsgiapp(app)

    def test_wsgiapp_none(self):
        self.assertRaises(ValueError, self._callFUT, None)

    def test_decorator(self):
        context = DummyContext()
        request = DummyRequest()
        decorator = self._callFUT(dummyapp)
        response = decorator(context, request)
        self.assertEqual(response, dummyapp)

    def test_decorator_object_instance(self):
        context = DummyContext()
        request = DummyRequest()
        app = DummyApp()
        decorator = self._callFUT(app)
        response = decorator(context, request)
        self.assertEqual(response, app)


class WSGIApp2Tests(unittest.TestCase):
    def _callFUT(self, app):
        from pyramid.wsgi import wsgiapp2

        return wsgiapp2(app)

    def test_wsgiapp2_none(self):
        self.assertRaises(ValueError, self._callFUT, None)

    def test_decorator_with_subpath_and_view_name(self):
        context = DummyContext()
        request = DummyRequest()
        request.subpath = ('subpath',)
        request.environ = {
            'SCRIPT_NAME': '/foo',
            'PATH_INFO': '/b/view_name/subpath',
        }
        decorator = self._callFUT(dummyapp)
        response = decorator(context, request)
        self.assertEqual(response, dummyapp)
        self.assertEqual(request.environ['PATH_INFO'], '/subpath')
        self.assertEqual(request.environ['SCRIPT_NAME'], '/foo/b/view_name')

    def test_decorator_with_subpath_no_view_name(self):
        context = DummyContext()
        request = DummyRequest()
        request.subpath = ('subpath',)
        request.environ = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/b/subpath'}
        decorator = self._callFUT(dummyapp)
        response = decorator(context, request)
        self.assertEqual(response, dummyapp)
        self.assertEqual(request.environ['PATH_INFO'], '/subpath')
        self.assertEqual(request.environ['SCRIPT_NAME'], '/foo/b')

    def test_decorator_no_subpath_with_view_name(self):
        context = DummyContext()
        request = DummyRequest()
        request.subpath = ()
        request.environ = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/b/view_name'}
        decorator = self._callFUT(dummyapp)
        response = decorator(context, request)
        self.assertEqual(response, dummyapp)
        self.assertEqual(request.environ['PATH_INFO'], '/')
        self.assertEqual(request.environ['SCRIPT_NAME'], '/foo/b/view_name')

    def test_decorator_traversed_empty_with_view_name(self):
        context = DummyContext()
        request = DummyRequest()
        request.subpath = ()
        request.environ = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/view_name'}
        decorator = self._callFUT(dummyapp)
        response = decorator(context, request)
        self.assertEqual(response, dummyapp)
        self.assertEqual(request.environ['PATH_INFO'], '/')
        self.assertEqual(request.environ['SCRIPT_NAME'], '/foo/view_name')

    def test_decorator_traversed_empty_no_view_name(self):
        context = DummyContext()
        request = DummyRequest()
        request.subpath = ()
        request.environ = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/'}
        decorator = self._callFUT(dummyapp)
        response = decorator(context, request)
        self.assertEqual(response, dummyapp)
        self.assertEqual(request.environ['PATH_INFO'], '/')
        self.assertEqual(request.environ['SCRIPT_NAME'], '/foo')

    def test_decorator_traversed_empty_no_view_name_no_script_name(self):
        context = DummyContext()
        request = DummyRequest()
        request.subpath = ()
        request.environ = {'SCRIPT_NAME': '', 'PATH_INFO': '/'}
        decorator = self._callFUT(dummyapp)
        response = decorator(context, request)
        self.assertEqual(response, dummyapp)
        self.assertEqual(request.environ['PATH_INFO'], '/')
        self.assertEqual(request.environ['SCRIPT_NAME'], '')

    def test_decorator_on_callable_object_instance(self):
        context = DummyContext()
        request = DummyRequest()
        request.subpath = ()
        request.environ = {'SCRIPT_NAME': '/foo', 'PATH_INFO': '/'}
        app = DummyApp()
        decorator = self._callFUT(app)
        response = decorator(context, request)
        self.assertEqual(response, app)
        self.assertEqual(request.environ['PATH_INFO'], '/')
        self.assertEqual(request.environ['SCRIPT_NAME'], '/foo')


def dummyapp(environ, start_response):
    """ """


class DummyApp(object):
    def __call__(self, environ, start_response):
        """ """


class DummyContext:
    pass


class DummyRequest:
    def get_response(self, application):
        return application

    def copy(self):
        self.copied = True
        return self