forked from aio-libs/aiohttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_web_server.py
118 lines (81 loc) · 3.27 KB
/
test_web_server.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
import asyncio
from unittest import mock
import pytest
from aiohttp import client, web
async def test_simple_server(raw_test_server, test_client):
async def handler(request):
return web.Response(text=str(request.rel_url))
server = await raw_test_server(handler)
cli = await test_client(server)
resp = await cli.get('/path/to')
assert resp.status == 200
txt = await resp.text()
assert txt == '/path/to'
async def test_raw_server_not_http_exception(raw_test_server, test_client):
exc = RuntimeError("custom runtime error")
async def handler(request):
raise exc
logger = mock.Mock()
server = await raw_test_server(handler, logger=logger)
cli = await test_client(server)
resp = await cli.get('/path/to')
assert resp.status == 500
txt = await resp.text()
assert "<h1>500 Internal Server Error</h1>" in txt
logger.exception.assert_called_with(
"Error handling request",
exc_info=exc)
async def test_raw_server_handler_timeout(raw_test_server, test_client):
exc = asyncio.TimeoutError("error")
async def handler(request):
raise exc
logger = mock.Mock()
server = await raw_test_server(handler, logger=logger)
cli = await test_client(server)
resp = await cli.get('/path/to')
assert resp.status == 504
await resp.text()
logger.debug.assert_called_with("Request handler timed out.")
async def test_raw_server_do_not_swallow_exceptions(raw_test_server,
test_client):
async def handler(request):
raise asyncio.CancelledError()
logger = mock.Mock()
server = await raw_test_server(handler, logger=logger)
cli = await test_client(server)
with pytest.raises(client.ServerDisconnectedError):
await cli.get('/path/to')
logger.debug.assert_called_with('Ignored premature client disconnection')
async def test_raw_server_cancelled_in_write_eof(raw_test_server, test_client):
async def handler(request):
resp = web.Response(text=str(request.rel_url))
resp.write_eof = mock.Mock(side_effect=asyncio.CancelledError("error"))
return resp
logger = mock.Mock()
server = await raw_test_server(handler, logger=logger)
cli = await test_client(server)
resp = await cli.get('/path/to')
with pytest.raises(client.ClientPayloadError):
await resp.read()
logger.debug.assert_called_with('Ignored premature client disconnection ')
async def test_raw_server_not_http_exception_debug(raw_test_server,
test_client):
exc = RuntimeError("custom runtime error")
async def handler(request):
raise exc
logger = mock.Mock()
server = await raw_test_server(handler, logger=logger, debug=True)
cli = await test_client(server)
resp = await cli.get('/path/to')
assert resp.status == 500
txt = await resp.text()
assert "<h2>Traceback:</h2>" in txt
logger.exception.assert_called_with(
"Error handling request",
exc_info=exc)
def test_create_web_server_with_implicit_loop(loop):
asyncio.set_event_loop(loop)
async def handler(request):
return web.Response() # pragma: no cover
srv = web.Server(handler)
assert srv._loop is loop