-
Notifications
You must be signed in to change notification settings - Fork 63
/
test_anyio.py
588 lines (437 loc) · 16.1 KB
/
test_anyio.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# pylint: disable=wrong-import-position
import ssl
from os import path
from functools import partial
from pathlib import Path
import pytest
from anyio import create_task_group, aopen
import curio
from overly import (
Server,
ssl_socket_wrapper,
default_ssl_cert,
send_200,
send_303,
send_400,
send_500,
delay,
send_request_as_json,
accept_cookies_and_respond,
send_gzip,
send_deflate,
send_chunked,
send_200_blank_headers,
finish,
HttpMethods,
)
import asks
from asks.errors import TooManyRedirects, BadStatus, RequestTimeout
_TEST_LOC = ("localhost", 25001)
_SSL_CONTEXT = ssl.create_default_context(cadata=default_ssl_cert)
def curio_run(func):
def func_wrapper(*args, **kwargs):
kernel = curio.Kernel()
kernel.run(func(*args, **kwargs))
kernel.run(shutdown=True)
return func_wrapper
@Server(_TEST_LOC, steps=[send_200, finish])
@curio_run
async def test_http_get(server):
r = await asks.get(server.http_test_url)
assert r.status_code == 200
# GET tests
@Server(_TEST_LOC, steps=[send_200, finish], socket_wrapper=ssl_socket_wrapper)
@curio_run
async def test_https_get(server):
# If we use ssl_context= to trust the CA, then we can successfully do a
# GET over https.
r = await asks.get(server.https_test_url, ssl_context=_SSL_CONTEXT)
assert r.status_code == 200
@Server(_TEST_LOC, steps=[send_200, finish], socket_wrapper=ssl_socket_wrapper)
@curio_run
async def test_https_get_checks_cert(server):
try:
expected_error = ssl.SSLCertVerificationError
except AttributeError:
# If we're running in Python <3.7, we won't have the specific error
# that will be raised, but we can expect it to raise an SSLError
# nonetheless
expected_error = ssl.SSLError
# The server's certificate isn't signed by any real CA. By default, asks
# should notice that, and raise an error.
with pytest.raises(expected_error):
await asks.get(server.https_test_url)
# @curio_run
# async def test_bad_www_and_schema_get():
# r = await asks.get('http://reddit.com')
# assert r.status_code == 200
@Server(_TEST_LOC, steps=[send_400, finish])
@curio_run
async def test_http_get_client_error(server):
r = await asks.get(server.http_test_url)
with pytest.raises(BadStatus) as excinfo:
r.raise_for_status()
assert excinfo.match("400 Client Error: BAD REQUEST")
assert excinfo.value.status_code == 400
@Server(_TEST_LOC, steps=[send_500, finish])
@curio_run
async def test_http_get_server_error(server):
r = await asks.get(server.http_test_url)
with pytest.raises(BadStatus) as excinfo:
r.raise_for_status()
assert excinfo.match("500 Server Error: INTERNAL SERVER ERROR")
assert excinfo.value.status_code == 500
# Redirect tests
@Server(
_TEST_LOC,
max_requests=4,
steps=[
[(HttpMethods.GET, "/redirect_1"), send_303, finish],
[(HttpMethods.GET, "/"), send_200, finish],
[(HttpMethods.GET, "/redirect_1"), send_303, finish],
[(HttpMethods.GET, "/"), send_200, finish],
],
ordered_steps=True,
)
@curio_run
async def test_http_redirect(server):
r = await asks.get(server.http_test_url + "/redirect_1")
assert len(r.history) == 1
# make sure history doesn't persist across responses
r.history.append("not a response obj")
r = await asks.get(server.http_test_url + "/redirect_1")
assert len(r.history) == 1
@Server(
_TEST_LOC,
max_requests=3,
steps=[
[
(HttpMethods.GET, "/redirect_max"),
partial(send_303, headers=[("location", "redirect_max1")]),
finish,
],
[
(HttpMethods.GET, "/redirect_max1"),
partial(send_303, headers=[("location", "redirect_max")]),
finish,
],
],
)
@curio_run
async def test_http_max_redirect_error(server):
with pytest.raises(TooManyRedirects):
await asks.get(server.http_test_url + "/redirect_max", max_redirects=1)
@Server(
_TEST_LOC,
max_requests=2,
steps=[
[
(HttpMethods.GET, "/path/redirect"),
partial(send_303, headers=[("location", "../foo/bar")]),
finish,
],
[(HttpMethods.GET, "/foo/bar"), send_200, finish],
],
)
@curio_run
async def test_redirect_relative_url(server):
r = await asks.get(server.http_test_url + "/path/redirect", max_redirects=1)
assert len(r.history) == 1
assert r.url == "http://{0}:{1}/foo/bar".format(*_TEST_LOC)
@Server(
_TEST_LOC,
max_requests=2,
steps=[
[
(HttpMethods.GET, "/redirect_once"),
partial(send_303, headers=[("location", "/")]),
finish,
],
[(HttpMethods.GET, "/"), send_200, finish],
],
)
@curio_run
async def test_http_under_max_redirect(server):
r = await asks.get(server.http_test_url + "/redirect_once", max_redirects=2)
assert r.status_code == 200
# Timeout tests
@Server(_TEST_LOC, steps=[delay(2), send_200, finish])
@curio_run
async def test_http_timeout_error(server):
with pytest.raises(RequestTimeout):
await asks.get(server.http_test_url, timeout=1)
@Server(_TEST_LOC, steps=[send_200, finish])
@curio_run
async def test_http_timeout(server):
r = await asks.get(server.http_test_url, timeout=10)
assert r.status_code == 200
# Param set test
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_param_dict_set(server):
r = await asks.get(server.http_test_url, params={"cheese": "the best"})
j = r.json()
assert next(v == "the best" for k, v in j["params"] if k == "cheese")
# Data set test
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_data_dict_set(server):
r = await asks.post(server.http_test_url, data={"cheese": "please bby"})
j = r.json()
assert next(v == "please bby" for k, v in j["form"] if k == "cheese")
# Cookie send test
@Server(_TEST_LOC, steps=[accept_cookies_and_respond, finish])
@curio_run
async def test_cookie_dict_send(server):
cookies = {"Test-Cookie": "Test Cookie Value", "koooookie": "pie"}
r = await asks.get(server.http_test_url, cookies=cookies)
for cookie in r.cookies:
assert cookie.name in cookies
if " " in cookie.value:
assert cookie.value == '"' + cookies[cookie.name] + '"'
else:
assert cookie.value == cookies[cookie.name]
# Custom headers test
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_header_set(server):
r = await asks.get(
server.http_test_url, headers={"Asks-Header": "Test Header Value"}
)
j = r.json()
assert any(k == "asks-header" for k, _ in j["headers"])
assert "cOntenT-tYPe" in r.headers
# File send test
TEST_DIR = path.dirname(path.abspath(__file__))
TEST_FILE1 = path.join(TEST_DIR, "test_file1.txt")
TEST_FILE2 = path.join(TEST_DIR, "test_file2")
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_file_send_single(server):
r = await asks.post(server.http_test_url, files={"file_1": TEST_FILE1})
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
file_data = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
assert file_data["file"] == "Compooper"
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_file_send_double(server):
r = await asks.post(
server.http_test_url, files={"file_1": TEST_FILE1, "file_2": TEST_FILE2}
)
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
assert any(file_data["name"] == "file_2" for file_data in j["files"])
file_data_1 = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
file_data_2 = next(
file_data for file_data in j["files"] if file_data["name"] == "file_2"
)
assert file_data_1["file"] == "Compooper"
assert file_data_2["file"] == "My slug <3"
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_file_send_file_and_form_data(server):
r = await asks.post(
server.http_test_url,
files={"file_1": TEST_FILE1, "data_1": "watwatwatwat=yesyesyes"},
)
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
assert any(form_data["name"] == "data_1" for form_data in j["forms"])
file_data_1 = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
assert file_data_1["file"] == "Compooper"
form_data_1 = next(
form_data for form_data in j["forms"] if form_data["name"] == "data_1"
)
assert form_data_1["form_data"] == "watwatwatwat=yesyesyes"
# File send test new multipart API
TEST_DIR = path.dirname(path.abspath(__file__))
TEST_FILE1 = path.join(TEST_DIR, "test_file1.txt")
TEST_FILE2 = path.join(TEST_DIR, "test_file2")
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_multipart_send_single(server):
r = await asks.post(server.http_test_url, multipart={"file_1": Path(TEST_FILE1)})
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
file_data = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
assert file_data["file"] == "Compooper"
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_multipart_send_single_already_open(server):
with open(TEST_FILE1, "rb") as f:
r = await asks.post(server.http_test_url, multipart={"file_1": f})
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
file_data = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
assert file_data["file"] == "Compooper"
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_multipart_send_single_already_open_async(server):
async with await aopen(TEST_FILE1, "rb") as f:
r = await asks.post(server.http_test_url, multipart={"file_1": f})
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
file_data = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
assert file_data["file"] == "Compooper"
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_multipart_send_raw_bytes(server):
r = await asks.post(
server.http_test_url,
multipart={
"file_1": asks.multipart.MultipartData(
b"Compooper", basename="in_memory.txt",
)
},
)
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
file_data = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
assert file_data["file"] == "Compooper"
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_multipart_send_double(server):
r = await asks.post(
server.http_test_url,
multipart={"file_1": Path(TEST_FILE1), "file_2": Path(TEST_FILE2)},
)
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
assert any(file_data["name"] == "file_2" for file_data in j["files"])
file_data_1 = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
file_data_2 = next(
file_data for file_data in j["files"] if file_data["name"] == "file_2"
)
assert file_data_1["file"] == "Compooper"
assert file_data_2["file"] == "My slug <3"
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_multipart_send_file_and_form_data(server):
r = await asks.post(
server.http_test_url,
multipart={"file_1": Path(TEST_FILE1), "data_1": "watwatwatwat=yesyesyes"},
)
j = r.json()
assert any(file_data["name"] == "file_1" for file_data in j["files"])
assert any(form_data["name"] == "data_1" for form_data in j["forms"])
file_data_1 = next(
file_data for file_data in j["files"] if file_data["name"] == "file_1"
)
assert file_data_1["file"] == "Compooper"
form_data_1 = next(
form_data for form_data in j["forms"] if form_data["name"] == "data_1"
)
assert form_data_1["form_data"] == "watwatwatwat=yesyesyes"
# JSON send test
@Server(_TEST_LOC, steps=[send_request_as_json, finish])
@curio_run
async def test_json_send(server):
r = await asks.post(
server.http_test_url, json={"key_1": True, "key_2": "cheesestring"}
)
j = r.json()
json_1 = next(iter(j["json"]))
assert json_1["json"]["key_1"] is True
assert json_1["json"]["key_2"] == "cheesestring"
# Test decompression
@Server(_TEST_LOC, steps=[partial(send_gzip, data="wolowolowolo"), finish])
@curio_run
async def test_gzip(server):
r = await asks.get(server.http_test_url)
assert r.text == "wolowolowolo"
@Server(_TEST_LOC, steps=[partial(send_deflate, data="wolowolowolo"), finish])
@curio_run
async def test_deflate(server):
r = await asks.get(server.http_test_url)
assert r.text == "wolowolowolo"
# Test chunks and streaming
@Server(_TEST_LOC, steps=[partial(send_chunked, data=["ham "] * 10), finish])
@curio_run
async def test_chunked(server):
r = await asks.get(server.http_test_url)
assert r.text == "ham ham ham ham ham ham ham ham ham ham "
@Server(_TEST_LOC, steps=[partial(send_chunked, data=["ham "] * 10), finish])
@curio_run
async def test_stream(server):
data = b""
r = await asks.get(server.http_test_url, stream=True)
async for chunk in r.body:
data += chunk
assert data == b"ham ham ham ham ham ham ham ham ham ham "
# Test callback
@Server(_TEST_LOC, steps=[partial(send_chunked, data=["ham "] * 10), finish])
@curio_run
async def test_callback(server):
async def callback_example(chunk):
nonlocal callback_data
callback_data += chunk
callback_data = b""
await asks.get(server.http_test_url, callback=callback_example)
assert callback_data == b"ham ham ham ham ham ham ham ham ham ham "
# Test connection close without content-length and transfer-encoding
@Server(
_TEST_LOC,
steps=[partial(send_200_blank_headers, headers=[("connection", "close")]), finish],
)
@curio_run
async def test_connection_close_no_content_len(server):
r = await asks.get(server.http_test_url)
assert r.text == "200"
# Session Tests
# =============
# Test Session with two pooled connections on ten get requests.
@Server(
_TEST_LOC,
steps=[partial(send_200_blank_headers, headers=[("connection", "close")]), finish],
max_requests=10,
)
@curio_run
async def test_session_smallpool(server):
async def worker(s):
r = await s.get(path="/get")
assert r.status_code == 200
s = asks.Session(server.http_test_url, connections=2)
async with create_task_group() as g:
for _ in range(10):
await g.spawn(worker, s)
# Test stateful Session
# TODO check the "" quoting of cookies here (probably in overly)
@Server(_TEST_LOC, steps=[accept_cookies_and_respond, finish])
@curio_run
async def test_session_stateful(server):
s = asks.Session(server.http_test_url, persist_cookies=True)
await s.get(cookies={"Test-Cookie": "Test Cookie Value"})
assert ":".join(str(x) for x in _TEST_LOC) in s._cookie_tracker.domain_dict.keys()
assert (
s._cookie_tracker.domain_dict[":".join(str(x) for x in _TEST_LOC)][0].value
== '"Test Cookie Value"'
)
# Test session instantiates outside event loop
def test_instantiate_session_outside_of_event_loop():
try:
asks.Session()
except RuntimeError:
pytest.fail("Could not instantiate Session outside of event loop")
@curio_run
async def test_session_unknown_kwargs():
with pytest.raises(TypeError, match="request\(\) got .*"):
session = asks.Session("https://httpbin.org/get")
await session.request("GET", ko=7, foo=0, bar=3, shite=3)
pytest.fail("Passing unknown kwargs does not raise TypeError")