File: test_async_asgi.py

package info (click to toggle)
python-engineio 3.0.0%2Bdfsg-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 468 kB
  • sloc: python: 4,688; makefile: 15
file content (245 lines) | stat: -rw-r--r-- 9,500 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import os
import sys
import unittest

import six
if six.PY3:
    from unittest import mock
else:
    import mock

if sys.version_info >= (3, 5):
    import asyncio
    from asyncio import coroutine
    from engineio import async_asgi


def AsyncMock(*args, **kwargs):
    """Return a mock asynchronous function."""
    m = mock.MagicMock(*args, **kwargs)

    @coroutine
    def mock_coro(*args, **kwargs):
        return m(*args, **kwargs)

    mock_coro.mock = m
    return mock_coro


def _run(coro):
    """Run the given coroutine."""
    return asyncio.get_event_loop().run_until_complete(coro)


@unittest.skipIf(sys.version_info < (3, 5), 'only for Python 3.5+')
class AsgiTests(unittest.TestCase):
    def test_create_app(self):
        app = async_asgi.ASGIApp(
            'eio', 'other_app', static_files='static_files',
            engineio_path='/foo')
        self.assertEqual(app.engineio_server, 'eio')
        self.assertEqual(app.other_asgi_app, 'other_app')
        self.assertEqual(app.static_files, 'static_files')
        self.assertEqual(app.engineio_path, 'foo'),

    def test_engineio_routing(self):
        mock_server = mock.MagicMock()
        mock_server.handle_request = AsyncMock()
        app = async_asgi.ASGIApp(mock_server)
        scope = {'type': 'http', 'path': '/engine.io/'}
        handler = app(scope)
        _run(handler('receive', 'send'))
        mock_server.handle_request.mock.assert_called_once_with(
            scope, 'receive', 'send')

    def test_other_app_routing(self):
        other_app = mock.MagicMock()
        app = async_asgi.ASGIApp('eio', other_app)
        scope = {'type': 'http', 'path': '/foo'}
        app(scope)
        other_app.assert_called_once_with(scope)

    def test_static_file_routing(self):
        root_dir = os.path.dirname(__file__)
        app = async_asgi.ASGIApp('eio', static_files={
            '/foo': {'content_type': 'text/html',
                     'filename': root_dir + '/index.html'}
        })
        handler = app({'type': 'http', 'path': '/foo'})
        receive = AsyncMock(return_value={'type': 'http.request'})
        send = AsyncMock()
        _run(handler(receive, send))
        send.mock.assert_called_with({'type': 'http.response.body',
                                      'body': b'<html></html>\n'})

    def test_lifespan_startup(self):
        app = async_asgi.ASGIApp('eio')
        handler = app({'type': 'lifespan'})
        receive = AsyncMock(return_value={'type': 'lifespan.startup'})
        send = AsyncMock()
        _run(handler(receive, send))
        send.mock.assert_called_once_with(
            {'type': 'lifespan.startup.complete'})

    def test_lifespan_shutdown(self):
        app = async_asgi.ASGIApp('eio')
        handler = app({'type': 'lifespan'})
        receive = AsyncMock(return_value={'type': 'lifespan.shutdown'})
        send = AsyncMock()
        _run(handler(receive, send))
        send.mock.assert_called_once_with(
            {'type': 'lifespan.shutdown.complete'})

    def test_lifespan_invalid(self):
        app = async_asgi.ASGIApp('eio')
        handler = app({'type': 'lifespan'})
        receive = AsyncMock(return_value={'type': 'lifespan.foo'})
        send = AsyncMock()
        _run(handler(receive, send))
        send.mock.assert_not_called()

    def test_not_found(self):
        app = async_asgi.ASGIApp('eio')
        handler = app({'type': 'http', 'path': '/foo'})
        receive = AsyncMock(return_value={'type': 'http.request'})
        send = AsyncMock()
        _run(handler(receive, send))
        send.mock.assert_any_call(
            {'type': 'http.response.start', 'status': 404,
             'headers': [(b'Content-Type', b'text/plain')]})
        send.mock.assert_any_call({'type': 'http.response.body',
                                   'body': b'not found'})

    def test_translate_request(self):
        receive = AsyncMock(return_value={'type': 'http.request',
                                          'body': b'hello world'})
        send = AsyncMock()
        environ = _run(async_asgi.translate_request({
            'type': 'http',
            'method': 'PUT',
            'headers': [(b'a', b'b'), (b'c-c', b'd'), (b'c_c', b'e'),
                        (b'content-type', b'application/json'),
                        (b'content-length', b'123')],
            'path': '/foo/bar',
            'query_string': b'baz=1'}, receive, send))
        expected_environ = {
            'REQUEST_METHOD': 'PUT',
            'PATH_INFO': '/foo/bar',
            'QUERY_STRING': 'baz=1',
            'CONTENT_TYPE': 'application/json',
            'CONTENT_LENGTH': '123',
            'HTTP_A': 'b',
            # 'HTTP_C_C': 'd,e',
            'RAW_URI': '/foo/bar?baz=1',
            'SERVER_PROTOCOL': 'HTTP/1.1',
            'asgi.receive': receive,
            'asgi.send': send
        }
        for k, v in expected_environ.items():
            self.assertEqual(v, environ[k])
        self.assertTrue(
            environ['HTTP_C_C'] == 'd,e' or environ['HTTP_C_C'] == 'e,d')
        body = _run(environ['wsgi.input'].read())
        self.assertEqual(body, b'hello world')

    def test_translate_request_no_query_string(self):
        receive = AsyncMock(return_value={'type': 'http.request',
                                          'body': b'hello world'})
        send = AsyncMock()
        environ = _run(async_asgi.translate_request({
            'type': 'http',
            'method': 'PUT',
            'headers': [(b'a', b'b'), (b'c-c', b'd'), (b'c_c', b'e'),
                        (b'content-type', b'application/json'),
                        (b'content-length', b'123')],
            'path': '/foo/bar'}, receive, send))
        expected_environ = {
            'REQUEST_METHOD': 'PUT',
            'PATH_INFO': '/foo/bar',
            'QUERY_STRING': '',
            'CONTENT_TYPE': 'application/json',
            'CONTENT_LENGTH': '123',
            'HTTP_A': 'b',
            # 'HTTP_C_C': 'd,e',
            'RAW_URI': '/foo/bar',
            'SERVER_PROTOCOL': 'HTTP/1.1',
            'asgi.receive': receive,
            'asgi.send': send
        }
        for k, v in expected_environ.items():
            self.assertEqual(v, environ[k])
        self.assertTrue(
            environ['HTTP_C_C'] == 'd,e' or environ['HTTP_C_C'] == 'e,d')
        body = _run(environ['wsgi.input'].read())
        self.assertEqual(body, b'hello world')

    def test_translate_request_with_large_body(self):
        receive = AsyncMock(side_effect=[
            {'type': 'http.request', 'body': b'hello ', 'more_body': True},
            {'type': 'http.request', 'body': b'world', 'more_body': True},
            {'type': 'foo.bar'},  # should stop parsing here
            {'type': 'http.request', 'body': b'!!!'},
        ])
        send = AsyncMock()
        environ = _run(async_asgi.translate_request({
            'type': 'http',
            'method': 'PUT',
            'headers': [(b'a', b'b'), (b'c-c', b'd'), (b'c_c', b'e'),
                        (b'content-type', b'application/json'),
                        (b'content-length', b'123')],
            'path': '/foo/bar',
            'query_string': b'baz=1'}, receive, send))
        expected_environ = {
            'REQUEST_METHOD': 'PUT',
            'PATH_INFO': '/foo/bar',
            'QUERY_STRING': 'baz=1',
            'CONTENT_TYPE': 'application/json',
            'CONTENT_LENGTH': '123',
            'HTTP_A': 'b',
            # 'HTTP_C_C': 'd,e',
            'RAW_URI': '/foo/bar?baz=1',
            'SERVER_PROTOCOL': 'HTTP/1.1',
            'asgi.receive': receive,
            'asgi.send': send
        }
        for k, v in expected_environ.items():
            self.assertEqual(v, environ[k])
        self.assertTrue(
            environ['HTTP_C_C'] == 'd,e' or environ['HTTP_C_C'] == 'e,d')
        body = _run(environ['wsgi.input'].read())
        self.assertEqual(body, b'hello world')

    def test_translate_websocket_request(self):
        receive = AsyncMock(return_value={'type': 'websocket.connect'})
        send = AsyncMock()
        _run(async_asgi.translate_request({
            'type': 'websocket',
            'headers': [(b'a', b'b'), (b'c-c', b'd'), (b'c_c', b'e'),
                        (b'content-type', b'application/json'),
                        (b'content-length', b'123')],
            'path': '/foo/bar',
            'query_string': b'baz=1'}, receive, send))
        send.mock.assert_called_once_with({'type': 'websocket.accept'})

    def test_translate_unknown_request(self):
        receive = AsyncMock(return_value={'type': 'http.foo'})
        send = AsyncMock()
        environ = _run(async_asgi.translate_request({
            'type': 'http',
            'path': '/foo/bar',
            'query_string': b'baz=1'}, receive, send))
        self.assertEqual(environ, {})

    # @mock.patch('async_aiohttp.aiohttp.web.Response')
    def test_make_response(self):
        environ = {
            'asgi.send': AsyncMock()
        }
        _run(async_asgi.make_response('202 ACCEPTED', [('foo', 'bar')],
                                      b'payload', environ))
        print(environ['asgi.send'].mock.call_args_list)
        environ['asgi.send'].mock.assert_any_call(
            {'type': 'http.response.start', 'status': 202,
             'headers': [(b'foo', b'bar')]})
        environ['asgi.send'].mock.assert_any_call(
            {'type': 'http.response.body', 'body': b'payload'})