File: test_middleware.py

package info (click to toggle)
python-engineio 4.12.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 944 kB
  • sloc: python: 10,594; makefile: 15; sh: 6
file content (196 lines) | stat: -rw-r--r-- 7,524 bytes parent folder | download | duplicates (2)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
from unittest import mock

import engineio


class TestWSGIApp:
    def test_wsgi_routing(self):
        mock_wsgi_app = mock.MagicMock()
        mock_eio_app = 'foo'
        m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
        environ = {'PATH_INFO': '/foo'}
        start_response = "foo"
        m(environ, start_response)
        mock_wsgi_app.assert_called_once_with(environ, start_response)

    def test_eio_routing(self):
        mock_wsgi_app = 'foo'
        mock_eio_app = mock.Mock()
        mock_eio_app.handle_request = mock.MagicMock()
        m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
        environ = {'PATH_INFO': '/engine.io/'}
        start_response = "foo"
        m(environ, start_response)
        mock_eio_app.handle_request.assert_called_once_with(
            environ, start_response
        )

    def test_static_files(self):
        root_dir = os.path.dirname(__file__)
        m = engineio.WSGIApp(
            'foo',
            None,
            static_files={
                '/': root_dir + '/index.html',
                '/foo': {
                    'content_type': 'text/plain',
                    'filename': root_dir + '/index.html',
                },
                '/static': root_dir,
                '/static/test/': root_dir + '/',
                '/static2/test/': {'filename': root_dir + '/',
                                   'content_type': 'image/gif'},
            },
        )

        def check_path(path, status_code, content_type, body):
            environ = {'PATH_INFO': path}
            start_response = mock.MagicMock()
            r = m(environ, start_response)
            assert r == [body.encode('utf-8')]
            start_response.assert_called_once_with(
                status_code, [('Content-Type', content_type)]
            )

        check_path('/', '200 OK', 'text/html', '<html></html>\n')
        check_path('/foo', '200 OK', 'text/plain', '<html></html>\n')
        check_path('/foo/bar', '404 Not Found', 'text/plain', 'Not Found')
        check_path(
            '/static/index.html', '200 OK', 'text/html', '<html></html>\n'
        )
        check_path(
            '/static/foo.bar', '404 Not Found', 'text/plain', 'Not Found'
        )
        check_path(
            '/static/test/index.html', '200 OK', 'text/html', '<html></html>\n'
        )
        check_path('/static/test/', '200 OK', 'text/html', '<html></html>\n')
        check_path('/static/test/index.html', '200 OK', 'text/html',
                   '<html></html>\n')
        check_path('/static/test/files/', '200 OK', 'text/html',
                   '<html>file</html>\n')
        check_path('/static/test/files/file.txt', '200 OK', 'text/plain',
                   'file\n')
        check_path('/static/test/files/x.html', '404 Not Found', 'text/plain',
                   'Not Found')
        check_path('/static2/test/', '200 OK', 'image/gif', '<html></html>\n')
        check_path('/static2/test/index.html', '200 OK', 'image/gif',
                   '<html></html>\n')
        check_path('/static2/test/files/', '200 OK', 'image/gif',
                   '<html>file</html>\n')
        check_path('/static2/test/files/file.txt', '200 OK', 'image/gif',
                   'file\n')
        check_path('/static2/test/files/x.html', '404 Not Found', 'text/plain',
                   'Not Found')
        check_path('/bar/foo', '404 Not Found', 'text/plain', 'Not Found')
        check_path('', '404 Not Found', 'text/plain', 'Not Found')

        m.static_files[''] = 'index.html'
        check_path('/static/test/', '200 OK', 'text/html', '<html></html>\n')

        m.static_files[''] = {'filename': 'index.html'}
        check_path('/static/test/', '200 OK', 'text/html', '<html></html>\n')

        m.static_files[''] = {
            'filename': 'index.html',
            'content_type': 'image/gif',
        }
        check_path('/static/test/', '200 OK', 'image/gif', '<html></html>\n')

        m.static_files[''] = {'filename': 'test.gif'}
        check_path('/static/test/', '404 Not Found', 'text/plain', 'Not Found')

        m.static_files = {}
        check_path(
            '/static/test/index.html',
            '404 Not Found',
            'text/plain',
            'Not Found',
        )

    def test_404(self):
        mock_wsgi_app = None
        mock_eio_app = mock.Mock()
        m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
        environ = {'PATH_INFO': '/foo/bar'}
        start_response = mock.MagicMock()
        r = m(environ, start_response)
        assert r == [b'Not Found']
        start_response.assert_called_once_with(
            "404 Not Found", [('Content-Type', 'text/plain')]
        )

    def test_custom_eio_path(self):
        mock_wsgi_app = None
        mock_eio_app = mock.Mock()
        mock_eio_app.handle_request = mock.MagicMock()
        m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app, engineio_path='foo')
        environ = {'PATH_INFO': '/engine.io/'}
        start_response = mock.MagicMock()
        r = m(environ, start_response)
        assert r == [b'Not Found']
        start_response.assert_called_once_with(
            "404 Not Found", [('Content-Type', 'text/plain')]
        )

        environ = {'PATH_INFO': '/foo/'}
        m(environ, start_response)
        mock_eio_app.handle_request.assert_called_once_with(
            environ, start_response
        )

    def test_custom_eio_path_slashes(self):
        mock_wsgi_app = None
        mock_eio_app = mock.Mock()
        mock_eio_app.handle_request = mock.MagicMock()
        m = engineio.WSGIApp(
            mock_eio_app, mock_wsgi_app, engineio_path='/foo/'
        )
        environ = {'PATH_INFO': '/foo/'}
        start_response = mock.MagicMock()
        m(environ, start_response)
        mock_eio_app.handle_request.assert_called_once_with(
            environ, start_response
        )

    def test_custom_eio_path_leading_slash(self):
        mock_wsgi_app = None
        mock_eio_app = mock.Mock()
        mock_eio_app.handle_request = mock.MagicMock()
        m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app, engineio_path='/foo')
        environ = {'PATH_INFO': '/foo/'}
        start_response = mock.MagicMock()
        m(environ, start_response)
        mock_eio_app.handle_request.assert_called_once_with(
            environ, start_response
        )

    def test_custom_eio_path_trailing_slash(self):
        mock_wsgi_app = None
        mock_eio_app = mock.Mock()
        mock_eio_app.handle_request = mock.MagicMock()
        m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app, engineio_path='foo/')
        environ = {'PATH_INFO': '/foo/'}
        start_response = mock.MagicMock()
        m(environ, start_response)
        mock_eio_app.handle_request.assert_called_once_with(
            environ, start_response
        )

    def test_gunicorn_socket(self):
        mock_wsgi_app = None
        mock_eio_app = mock.Mock()
        m = engineio.WSGIApp(mock_eio_app, mock_wsgi_app)
        environ = {'gunicorn.socket': 123, 'PATH_INFO': '/foo/bar'}
        start_response = mock.MagicMock()
        m(environ, start_response)
        assert 'eventlet.input' in environ
        assert environ['eventlet.input'].get_socket() == 123

    def test_legacy_middleware_class(self):
        m = engineio.Middleware('eio', 'wsgi', 'eio_path')
        assert m.engineio_app == 'eio'
        assert m.wsgi_app == 'wsgi'
        assert m.static_files == {}
        assert m.engineio_path == '/eio_path/'