-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.py
332 lines (238 loc) · 7.79 KB
/
tests.py
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
from uhttp import Application, MultiDict, Response
class TestApplication(Application):
async def test(
self, method, path, query_string=b'', headers=None, body=b''
):
response = {}
state = {}
http_scope = {
'type': 'http',
'method': method,
'path': path,
'query_string': query_string,
'headers': headers or [],
'state': state
}
async def http_receive():
return {'body': body, 'more_body': False}
async def http_send(event):
if event['type'] == 'http.response.start':
response['status'] = event['status']
response['headers'] = MultiDict([
[k.decode(), v.decode()] for k, v in event['headers']
])
elif event['type'] == 'http.response.body':
response['body'] = event['body']
lifespan_scope = {'type': 'lifespan', 'state': state}
async def lifespan_receive():
if not response:
return {'type': 'lifespan.startup'}
elif 'body' in response:
return {'type': 'lifespan.shutdown'}
else:
return {'type': ''}
async def lifespan_send(event):
if event['type'] == 'lifespan.startup.complete':
await self(http_scope, http_receive, http_send)
elif 'message' in event:
message = event['message'].encode()
response['status'] = 500
response['headers'] = MultiDict({
'content-length': str(len(message))
})
response['body'] = message
await self(lifespan_scope, lifespan_receive, lifespan_send)
return response
async def test_lifespan_startup_fail():
app = TestApplication()
@app.startup
def fail(state):
1 / 0
response = await app.test('GET', '/')
assert response['status'] == 500
assert response['body'] == b'ZeroDivisionError: division by zero'
async def test_lifespan_shutdown_fail():
app = TestApplication()
@app.shutdown
def fail(state):
1 / 0
response = await app.test('GET', '/')
assert response['status'] == 500
assert response['body'] == b'ZeroDivisionError: division by zero'
async def test_lifespan_startup():
app = TestApplication()
@app.startup
def startup(state):
state['msg'] = 'HI!'
@app.get('/')
def say_hi(request):
return request.state.get('msg')
response = await app.test('GET', '/')
assert response['body'] == b'HI!'
async def test_lifespan_shutdown():
app = TestApplication()
msgs = ['HI!']
@app.startup
def startup(state):
state['msgs'] = msgs
@app.shutdown
def shutdown(state):
state['msgs'].append('BYE!')
await app.test('GET', '/')
assert msgs[-1] == 'BYE!'
async def test_204():
app = TestApplication()
@app.get('/')
def nop(request):
pass
response = await app.test('GET', '/')
assert response['status'] == 204
assert response['body'] == b''
async def test_404():
app = TestApplication()
response = await app.test('GET', '/')
assert response['status'] == 404
async def test_405():
app = TestApplication()
@app.route('/', methods=('GET', 'POST'))
def index(request):
pass
response = await app.test('PUT', '/')
assert response['status'] == 405
assert response['headers'].get('allow') == 'GET, POST'
async def test_413():
app = TestApplication()
response = await app.test(
'POST', '/', body=b' '*(app._max_content + 1)
)
assert response['status'] == 413
async def test_methods():
app = TestApplication()
methods = ('GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS')
@app.route('/', methods=methods)
def index(request):
return request.method
for method in methods:
response = await app.test(method, '/')
assert response['body'] == method.encode()
async def test_path_parameters():
app = TestApplication()
@app.get(r'/hello/(?P<name>\w+)')
def hello(request):
return f'Hello, {request.params.get("name")}!'
response = await app.test('GET', '/hello/john')
assert response['status'] == 200
assert response['body'] == b'Hello, john!'
async def test_query_args():
app = TestApplication()
args = {}
@app.get('/')
def index(request):
args.update(request.args)
await app.test(
'GET', '/', query_string=b'tag=music&tag=rock&type=book'
)
assert args == {'tag': ['music', 'rock'], 'type': ['book']}
async def test_headers():
app = TestApplication()
headers = {}
@app.get('/')
def hello(request):
headers.update(request.headers)
await app.test('GET', '/', headers=[[b'from', b'[email protected]']])
assert headers == {'from': ['[email protected]']}
async def test_cookie():
app = TestApplication()
@app.get('/')
def index(request):
return request.cookies.output(header='Cookie:')
response = await app.test(
'GET', '/', headers=[[b'cookie', b'id=1;name=john']]
)
assert response['body'] == b'Cookie: id=1\r\nCookie: name=john'
async def test_set_cookie():
app = TestApplication()
@app.get('/')
def index(request):
return Response(status=204, cookies={'id': 2, 'name': 'jane'})
response = await app.test('GET', '/')
assert response['headers']._get('set-cookie') == ['id=2', 'name=jane']
async def test_bad_json():
app = TestApplication()
response = await app.test(
'POST',
'/',
headers=[[b'content-type', b'application/json']],
body=b'{"some": 1'
)
assert response['status'] == 400
async def test_good_json():
app = TestApplication()
json = {}
@app.post('/')
def index(request):
json.update(request.json)
await app.test(
'POST',
'/',
headers=[[b'content-type', b'application/json']],
body=b'{"some": 1}'
)
assert json == {'some': 1}
async def test_json_response():
app = TestApplication()
@app.get('/')
def json_hello(request):
return {'hello': 'world'}
response = await app.test('GET', '/')
assert response['status'] == 200
assert response['headers']['content-type'] == 'application/json'
assert response['body'] == b'{"hello": "world"}'
async def test_form():
app = TestApplication()
form = {}
@app.post('/')
def submit(request):
form.update(request.form)
await app.test(
'POST',
'/',
headers=[[b'content-type', b'application/x-www-form-urlencoded']],
body=b'name=john&age=27'
)
assert form == {'name': ['john'], 'age': ['27']}
async def test_early_response():
app = TestApplication()
@app.before
def early(request):
return "Hi! I'm early!"
@app.route('/')
def index(request):
return 'Maybe?'
response = await app.test('GET', '/')
assert response['status'] == 200
assert response['body'] == b"Hi! I'm early!"
async def test_late_early_response():
app = TestApplication()
@app.after
def early(request, response):
response.status = 200
response.body = b'Am I early?'
response = await app.test('POST', '/')
assert response['status'] == 200
assert response['body'] == b'Am I early?'
assert response['headers'].get('content-length') == '11'
async def test_app_mount():
app1 = TestApplication()
app2 = TestApplication()
@app1.route('/')
def app1_index(request):
pass
@app2.route('/')
def app2_index(request):
pass
app2.mount(app1, '/app1')
assert app2._routes == {
'/': {'GET': app2_index},
'/app1/': {'GET': app1_index}
}