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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421import pytest
from pytest import param
from apistar import Route, http, test
from apistar.server.app import App, ASyncApp
# HTTP Components as parameters
def get_request(request: http.Request):
return {
'method': request.method,
'url': request.url,
'headers': dict(request.headers),
'body': request.body.decode('utf-8')
}
def get_method(method: http.Method):
return {'method': method}
def get_scheme(scheme: http.Scheme):
return {'scheme': scheme}
def get_host(host: http.Host):
return {'host': host}
def get_port(port: http.Port):
return {'port': port}
def get_path(path: http.Path):
return {'path': path}
def get_query_string(query_string: http.QueryString):
return {'query_string': query_string}
def get_query_params(query_string: http.QueryString, query_params: http.QueryParams):
return {'query_params': dict(query_params)}
def get_page_query_param(page: http.QueryParam):
return {'page': page}
def get_url(url: http.URL):
return {'url': url, 'url.components': url.components}
def get_body(body: http.Body):
return {'body': body.decode('utf-8')}
def get_headers(headers: http.Headers):
return {'headers': dict(headers)}
def get_accept_header(accept: http.Header):
return {'accept': accept}
def get_missing_header(missing: http.Header):
return {'missing': missing}
def get_path_params(params: http.PathParams):
return {'params': params}
def get_request_data(data: http.RequestData):
if isinstance(data, dict):
data = {
key: value if not hasattr(value, 'filename') else {
'filename': value.filename,
'content': value.read().decode('utf-8'),
} for key, value in data.items()
}
return {'data': data}
def get_multikey_request_data(data: http.RequestData):
return {'data': sorted([
(key, value) for key, value in data.items(multi=True)
if not hasattr(value, 'filename')
])}
def return_string(data: http.RequestData) -> str:
return '<html><body>example content</body></html>'
def return_data(data: http.RequestData) -> dict:
return {'example': 'content'}
def return_response(data: http.RequestData) -> http.Response:
return http.JSONResponse({'example': 'content'})
def return_unserializable_json() -> dict:
class Dummy:
pass
return {'dummy': Dummy()}
routes = [
Route('/request/', 'GET', get_request),
Route('/method/', 'GET', get_method),
Route('/method/', 'POST', get_method, name='post_method'),
Route('/scheme/', 'GET', get_scheme),
Route('/host/', 'GET', get_host),
Route('/port/', 'GET', get_port),
Route('/path/', 'GET', get_path),
Route('/query_string/', 'GET', get_query_string),
Route('/query_params/', 'GET', get_query_params),
Route('/page_query_param/', 'GET', get_page_query_param),
Route('/url/', 'GET', get_url),
Route('/body/', 'POST', get_body),
Route('/headers/', 'GET', get_headers),
Route('/headers/', 'POST', get_headers, name='post_headers'),
Route('/accept_header/', 'GET', get_accept_header),
Route('/missing_header/', 'GET', get_missing_header),
Route('/path_params/{example}/', 'GET', get_path_params),
Route('/full_path_params/{+example}', 'GET', get_path_params, name='full_path_params'),
Route('/request_data/', 'POST', get_request_data),
Route('/multikey_request_data/', 'POST', get_multikey_request_data),
Route('/return_string/', 'GET', return_string),
Route('/return_data/', 'GET', return_data),
Route('/return_response/', 'GET', return_response),
Route('/return_unserializable_json/', 'GET', return_unserializable_json),
]
@pytest.fixture(scope='module', params=['wsgi', 'asgi'])
def client(request):
if request.param == 'asgi':
app = ASyncApp(routes=routes)
else:
app = App(routes=routes)
return test.TestClient(app)
def test_request(client):
response = client.get('/request/')
assert response.json() == {
'method': 'GET',
'url': 'http://testserver/request/',
'headers': {
'accept': '*/*',
'accept-encoding': 'gzip, deflate',
'connection': 'keep-alive',
'host': 'testserver',
'user-agent': 'testclient'
},
'body': ''
}
def test_method(client):
response = client.get('/method/')
assert response.json() == {'method': 'GET'}
response = client.post('/method/')
assert response.json() == {'method': 'POST'}
def test_scheme(client):
response = client.get('http://example.com/scheme/')
assert response.json() == {'scheme': 'http'}
response = client.get('https://example.com/scheme/')
assert response.json() == {'scheme': 'https'}
def test_host(client):
response = client.get('http://example.com/host/')
assert response.json() == {'host': 'example.com'}
def test_port(client):
response = client.get('http://example.com/port/')
assert response.json() == {'port': 80}
response = client.get('https://example.com/port/')
assert response.json() == {'port': 443}
response = client.get('http://example.com:123/port/')
assert response.json() == {'port': 123}
response = client.get('https://example.com:123/port/')
assert response.json() == {'port': 123}
def test_path(client):
response = client.get('/path/')
assert response.json() == {'path': '/path/'}
def test_query_string(client):
response = client.get('/query_string/')
assert response.json() == {'query_string': ''}
response = client.get('/query_string/?a=1&a=2&b=3')
assert response.json() == {'query_string': 'a=1&a=2&b=3'}
def test_query_params(client):
response = client.get('/query_params/')
assert response.json() == {'query_params': {}}
response = client.get('/query_params/?a=1&a=2&b=3')
assert response.json() == {
'query_params': {'a': '1', 'b': '3'}
}
def test_single_query_param(client):
response = client.get('/page_query_param/')
assert response.json() == {'page': None}
response = client.get('/page_query_param/?page=123')
assert response.json() == {'page': '123'}
response = client.get('/page_query_param/?page=123&page=456')
assert response.json() == {'page': '123'}
def test_url(client):
response = client.get('http://example.com/url/')
assert response.json() == {
'url': 'http://example.com/url/',
'url.components': ['http', 'example.com', '/url/', '', '', '']
}
response = client.get('https://example.com/url/')
assert response.json() == {
'url': 'https://example.com/url/',
'url.components': ['https', 'example.com', '/url/', '', '', '']
}
response = client.get('http://example.com:123/url/')
assert response.json() == {
'url': 'http://example.com:123/url/',
'url.components': ['http', 'example.com:123', '/url/', '', '', '']
}
response = client.get('https://example.com:123/url/')
assert response.json() == {
'url': 'https://example.com:123/url/',
'url.components': ['https', 'example.com:123', '/url/', '', '', '']
}
response = client.get('http://example.com/url/?a=1')
assert response.json() == {
'url': 'http://example.com/url/?a=1',
'url.components': ['http', 'example.com', '/url/', '', 'a=1', '']
}
def test_body(client):
response = client.post('/body/', data="content")
assert response.json() == {'body': 'content'}
def test_headers(client):
response = client.get('http://example.com/headers/')
assert response.json() == {'headers': {
'accept': '*/*',
'accept-encoding': 'gzip, deflate',
'connection': 'keep-alive',
'host': 'example.com',
'user-agent': 'testclient'
}}
response = client.get('http://example.com/headers/', headers={
'X-Example-Header': 'example'
})
assert response.json() == {'headers': {
'accept': '*/*',
'accept-encoding': 'gzip, deflate',
'connection': 'keep-alive',
'host': 'example.com',
'user-agent': 'testclient',
'x-example-header': 'example'
}}
response = client.post('http://example.com/headers/', data={'a': 1})
assert response.json() == {'headers': {
'accept': '*/*',
'accept-encoding': 'gzip, deflate',
'connection': 'keep-alive',
'content-length': '3',
'content-type': 'application/x-www-form-urlencoded',
'host': 'example.com',
'user-agent': 'testclient'
}}
def test_accept_header(client):
response = client.get('/accept_header/')
assert response.json() == {'accept': '*/*'}
def test_missing_header(client):
response = client.get('/missing_header/')
assert response.json() == {'missing': None}
def test_path_params(client):
response = client.get('/path_params/abc/')
assert response.json() == {'params': {'example': 'abc'}}
response = client.get('/path_params/a%20b%20c/')
assert response.json() == {'params': {'example': 'a b c'}}
response = client.get('/path_params/abc/def/')
assert response.status_code == 404
def test_full_path_params(client):
response = client.get('/full_path_params/abc/def/')
assert response.json() == {'params': {'example': 'abc/def/'}}
@pytest.mark.parametrize('request_params,response_status,response_json', [
# JSON
param({'json': {'abc': 123}}, 200, {'data': {'abc': 123}}, id='valid json body'),
param({}, 200, {'data': None}, id='empty json body'),
# Urlencoding
param({'data': {'abc': 123}}, 200, {'data': {'abc': '123'}}, id='valid urlencoded body'),
param(
{'headers': {'content-type': 'application/x-www-form-urlencoded'}}, 200, {'data': None},
id='empty urlencoded body',
),
# Misc
param({'data': b'...', 'headers': {'content-type': 'unknown'}}, 415, None, id='unknown body type'),
param({'data': b'...', 'headers': {'content-type': 'application/json'}}, 400, None, id='json parse failure'),
])
def test_request_data(request_params, response_status, response_json, client):
response = client.post('/request_data/', **request_params)
assert response.status_code == response_status
if response_json is not None:
assert response.json() == response_json
def test_multipart_request_data(client):
response = client.post('/request_data/', files={'a': ('b', '123')}, data={'b': '42'})
assert response.status_code == 200
assert response.json() == {
'data': {
'a': {
'filename': 'b',
'content': '123',
},
'b': '42',
}
}
response = client.post('/multikey_request_data/', files={'a': ('b', '123')}, data={'b': ['41', '42']})
assert response.status_code == 200
assert response.json() == {
'data': [
['b', '41'],
['b', '42'],
]
}
def test_return_string(client):
response = client.get('/return_string/')
assert response.text == '<html><body>example content</body></html>'
def test_return_data(client):
response = client.get('/return_data/')
assert response.json() == {'example': 'content'}
def test_return_response(client):
response = client.get('/return_response/')
assert response.json() == {'example': 'content'}
def test_return_unserializable_json(client):
with pytest.raises(TypeError) as excinfo:
client.get('/return_unserializable_json/')
assert str(excinfo).endswith("Object of type 'Dummy' is not JSON serializable.")
def test_headers_type(client):
h = http.Headers([('a', '123'), ('A', '456'), ('b', '789')])
assert 'a' in h
assert 'A' in h
assert 'b' in h
assert 'B' in h
assert 'c' not in h
assert h['a'] == '123'
assert h.get_list('a') == ['123', '456']
assert h.keys() == ['a', 'a', 'b']
assert h.values() == ['123', '456', '789']
assert h.items() == [('a', '123'), ('a', '456'), ('b', '789')]
assert list(h) == [('a', '123'), ('a', '456'), ('b', '789')]
assert dict(h) == {'a': '123', 'b': '789'}
assert repr(h) == "Headers([('a', '123'), ('a', '456'), ('b', '789')])"
assert http.Headers({'a': '123', 'b': '456'}) == http.Headers([('a', '123'), ('b', '456')])
assert http.Headers({'a': '123', 'b': '456'}) == {'B': '456', 'a': '123'}
assert http.Headers({'a': '123', 'b': '456'}) == [('B', '456'), ('a', '123')]
assert {'B': '456', 'a': '123'} == http.Headers({'a': '123', 'b': '456'})
assert [('B', '456'), ('a', '123')] == http.Headers({'a': '123', 'b': '456'})
def test_queryparams_type(client):
q = http.QueryParams([('a', '123'), ('a', '456'), ('b', '789')])
assert 'a' in q
assert 'A' not in q
assert 'c' not in q
assert q['a'] == '123'
assert q.get_list('a') == ['123', '456']
assert q.keys() == ['a', 'a', 'b']
assert q.values() == ['123', '456', '789']
assert q.items() == [('a', '123'), ('a', '456'), ('b', '789')]
assert list(q) == [('a', '123'), ('a', '456'), ('b', '789')]
assert dict(q) == {'a': '123', 'b': '789'}
assert repr(q) == "QueryParams([('a', '123'), ('a', '456'), ('b', '789')])"
assert http.QueryParams({'a': '123', 'b': '456'}) == http.QueryParams([('a', '123'), ('b', '456')])
assert http.QueryParams({'a': '123', 'b': '456'}) == {'b': '456', 'a': '123'}
assert http.QueryParams({'a': '123', 'b': '456'}) == [('b', '456'), ('a', '123')]
assert {'b': '456', 'a': '123'} == http.QueryParams({'a': '123', 'b': '456'})
assert [('b', '456'), ('a', '123')] == http.QueryParams({'a': '123', 'b': '456'})