-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_response.py
74 lines (60 loc) · 2.09 KB
/
test_response.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
from curio.meta import finalize
from curequests import get
from utils import run_with_curio
@run_with_curio
async def test_response_iter_stream(httpbin):
r = await get(httpbin + f'/bytes/{80*1024}', stream=True)
body = []
async with finalize(r.__aiter__()) as gen:
async for chunk in gen:
body.append(chunk)
assert r.connection.closed
assert len(b''.join(body)) == 80 * 1024
@run_with_curio
async def test_response_iter_not_stream(httpbin):
r = await get(httpbin + f'/bytes/{80*1024}')
body = []
async with finalize(r.__aiter__()) as gen:
async for chunk in gen:
body.append(chunk)
assert r.connection.closed
assert len(b''.join(body)) == 80 * 1024
@run_with_curio
async def test_response_iter_content(httpbin):
r = await get(httpbin + f'/bytes/{80*1024}', stream=True)
body = []
async with finalize(r.iter_content(1024)) as gen:
async for chunk in gen:
body.append(chunk)
assert r.connection.closed
assert len(b''.join(body)) == 80 * 1024
@run_with_curio
async def test_response_iter_lines(httpbin):
r = await get(httpbin + f'/get', stream=True)
body = []
async with finalize(r.iter_lines()) as gen:
async for chunk in gen:
body.append(chunk)
assert r.connection.closed
@run_with_curio
async def test_response_content(httpbin):
r = await get(httpbin + f'/bytes/{80*1024}')
assert r.connection.closed
assert len(r.content) == 80 * 1024
@run_with_curio
async def test_decode_unicode(httpbin):
r = await get(httpbin + f'/encoding/utf8', stream=True)
body = []
async with finalize(r.iter_content(decode_unicode=True)) as gen:
async for chunk in gen:
body.append(chunk)
assert r.connection.closed
body = ''.join(body).encode('utf-8')
assert len(body) == int(r.headers['content-length'])
@run_with_curio
async def test_response_close(httpbin):
r = await get(httpbin + f'/bytes/{80*1024}', stream=True)
assert not r.connection.closed
async with r:
pass
assert r.connection.closed