|
| 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