2016-04-19 20:47:49 +00:00
|
|
|
try:
|
|
|
|
import aiohttp
|
2017-11-11 20:29:50 +00:00
|
|
|
import aiohttp.web
|
2016-04-19 20:47:49 +00:00
|
|
|
except ImportError:
|
|
|
|
skip_tests = True
|
|
|
|
else:
|
|
|
|
skip_tests = False
|
|
|
|
|
2016-11-08 00:27:37 +00:00
|
|
|
import asyncio
|
2016-04-19 20:47:49 +00:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
from uvloop import _testbase as tb
|
|
|
|
|
|
|
|
|
|
|
|
class _TestAioHTTP:
|
|
|
|
|
|
|
|
def test_aiohttp_basic_1(self):
|
|
|
|
|
2017-11-11 20:29:50 +00:00
|
|
|
PAYLOAD = '<h1>It Works!</h1>' * 10000
|
2016-04-19 20:47:49 +00:00
|
|
|
|
2017-11-11 20:29:50 +00:00
|
|
|
async def on_request(request):
|
|
|
|
return aiohttp.web.Response(text=PAYLOAD)
|
2016-04-19 20:47:49 +00:00
|
|
|
|
2016-11-08 00:27:37 +00:00
|
|
|
asyncio.set_event_loop(self.loop)
|
2017-11-24 21:02:36 +00:00
|
|
|
app = aiohttp.web.Application()
|
2017-11-11 20:29:50 +00:00
|
|
|
app.router.add_get('/', on_request)
|
2016-11-08 00:27:37 +00:00
|
|
|
|
2016-04-19 20:47:49 +00:00
|
|
|
f = self.loop.create_server(
|
2017-11-11 20:29:50 +00:00
|
|
|
app.make_handler(),
|
2016-04-19 20:47:49 +00:00
|
|
|
'0.0.0.0', '0')
|
|
|
|
srv = self.loop.run_until_complete(f)
|
|
|
|
|
2016-05-23 19:46:51 +00:00
|
|
|
port = srv.sockets[0].getsockname()[1]
|
2016-04-19 20:47:49 +00:00
|
|
|
|
|
|
|
async def test():
|
2017-11-24 21:02:36 +00:00
|
|
|
# Make sure we're using the correct event loop.
|
|
|
|
self.assertIs(asyncio.get_event_loop(), self.loop)
|
|
|
|
|
2016-05-23 19:46:51 +00:00
|
|
|
for addr in (('localhost', port),
|
|
|
|
('127.0.0.1', port)):
|
2016-11-06 20:24:43 +00:00
|
|
|
async with aiohttp.ClientSession() as client:
|
2016-05-23 19:46:51 +00:00
|
|
|
async with client.get('http://{}:{}'.format(*addr)) as r:
|
|
|
|
self.assertEqual(r.status, 200)
|
2017-11-11 20:29:50 +00:00
|
|
|
result = await r.text()
|
|
|
|
self.assertEqual(result, PAYLOAD)
|
2016-04-19 20:47:49 +00:00
|
|
|
|
|
|
|
self.loop.run_until_complete(test())
|
2017-11-11 20:29:50 +00:00
|
|
|
self.loop.run_until_complete(app.shutdown())
|
|
|
|
self.loop.run_until_complete(app.cleanup())
|
2016-04-19 20:47:49 +00:00
|
|
|
|
2017-11-21 17:11:10 +00:00
|
|
|
srv.close()
|
|
|
|
self.loop.run_until_complete(srv.wait_closed())
|
|
|
|
|
2016-04-19 20:47:49 +00:00
|
|
|
|
|
|
|
@unittest.skipIf(skip_tests, "no aiohttp module")
|
|
|
|
class Test_UV_AioHTTP(_TestAioHTTP, tb.UVTestCase):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(skip_tests, "no aiohttp module")
|
|
|
|
class Test_AIO_AioHTTP(_TestAioHTTP, tb.AIOTestCase):
|
|
|
|
pass
|