Skip to content

Commit de89ae0

Browse files
committed
tests: Add a macro test -- aiohttp client/server (optional)
1 parent 0bfe1e4 commit de89ae0

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ python:
88

99
install:
1010
- "pip install --no-binary=:all: cython"
11+
- "pip install --no-binary=:all: aiohttp"
1112
- make
1213

1314
script:

tests/test_aiohttp.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
try:
2+
import aiohttp
3+
import aiohttp.server
4+
except ImportError:
5+
skip_tests = True
6+
else:
7+
skip_tests = False
8+
9+
import asyncio
10+
import uvloop
11+
import unittest
12+
13+
from uvloop import _testbase as tb
14+
15+
16+
class _TestAioHTTP:
17+
18+
def test_aiohttp_basic_1(self):
19+
20+
PAYLOAD = b'<h1>It Works!</h1>' * 10000
21+
22+
class HttpRequestHandler(aiohttp.server.ServerHttpProtocol):
23+
24+
async def handle_request(self, message, payload):
25+
response = aiohttp.Response(
26+
self.writer, 200, http_version=message.version
27+
)
28+
response.add_header('Content-Type', 'text/html')
29+
response.add_header('Content-Length', str(len(PAYLOAD)))
30+
response.send_headers()
31+
response.write(PAYLOAD)
32+
await response.write_eof()
33+
34+
f = self.loop.create_server(
35+
lambda: HttpRequestHandler(keep_alive=False),
36+
'0.0.0.0', '0')
37+
srv = self.loop.run_until_complete(f)
38+
39+
addr = srv.sockets[0].getsockname()[:2]
40+
41+
async def test():
42+
with aiohttp.ClientSession() as client:
43+
async with client.get('http://{}:{}'.format(*addr)) as resp:
44+
self.assertEqual(resp.status, 200)
45+
self.assertEqual(len(await resp.text()), len(PAYLOAD))
46+
47+
self.loop.run_until_complete(test())
48+
srv.close()
49+
self.loop.run_until_complete(srv.wait_closed())
50+
51+
52+
@unittest.skipIf(skip_tests, "no aiohttp module")
53+
class Test_UV_AioHTTP(_TestAioHTTP, tb.UVTestCase):
54+
pass
55+
56+
57+
@unittest.skipIf(skip_tests, "no aiohttp module")
58+
class Test_AIO_AioHTTP(_TestAioHTTP, tb.AIOTestCase):
59+
pass

0 commit comments

Comments
 (0)