Skip to content

Commit

Permalink
convert some files to async await syntax (aio-libs#2389)
Browse files Browse the repository at this point in the history
* convert test_web_urldispatcher.py to use async/await syntax

* convert test_web_websocket_functional.py to use async/await syntax

* convert test_web_websocket.py to use async/await syntax

* convert test_signals.py to use async/await syntax

* convert web_server.py to use async/await syntax
  • Loading branch information
jairhenrique authored and asvetlov committed Oct 24, 2017
1 parent d6b5ebc commit 2a47ca2
Show file tree
Hide file tree
Showing 5 changed files with 403 additions and 495 deletions.
5 changes: 2 additions & 3 deletions aiohttp/web_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ def _make_request(self, message, payload, protocol, writer, task):
return BaseRequest(
message, payload, protocol, writer, task, self._loop)

@asyncio.coroutine
def shutdown(self, timeout=None):
async def shutdown(self, timeout=None):
coros = [conn.shutdown(timeout) for conn in self._connections]
yield from asyncio.gather(*coros, loop=self._loop)
await asyncio.gather(*coros, loop=self._loop)
self._connections.clear()

def __call__(self):
Expand Down
10 changes: 3 additions & 7 deletions tests/test_signals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
from unittest import mock

import pytest
Expand Down Expand Up @@ -36,8 +35,7 @@ async def test_function_signal_dispatch(app):

callback_mock = mock.Mock()

@asyncio.coroutine
def callback(**kwargs):
async def callback(**kwargs):
callback_mock(**kwargs)

signal.append(callback)
Expand All @@ -53,8 +51,7 @@ async def test_function_signal_dispatch2(app):

callback_mock = mock.Mock()

@asyncio.coroutine
def callback(*args, **kwargs):
async def callback(*args, **kwargs):
callback_mock(*args, **kwargs)

signal.append(callback)
Expand All @@ -66,8 +63,7 @@ def callback(*args, **kwargs):
async def test_response_prepare(app):
callback = mock.Mock()

@asyncio.coroutine
def cb(*args, **kwargs):
async def cb(*args, **kwargs):
callback(*args, **kwargs)

app.on_response_prepare.append(cb)
Expand Down
116 changes: 50 additions & 66 deletions tests/test_web_urldispatcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import asyncio
import functools
import os
import shutil
Expand Down Expand Up @@ -43,9 +42,8 @@ def teardown():
b'<li><a href="/static/my_dir">my_dir/</a></li>\n'
b'<li><a href="/static/my_file">my_file</a></li>\n'
b'</ul>\n</body>\n</html>')])
@asyncio.coroutine
def test_access_root_of_static_handler(tmp_dir_path, loop, test_client,
show_index, status, prefix, data):
async def test_access_root_of_static_handler(tmp_dir_path, loop, test_client,
show_index, status, prefix, data):
"""
Tests the operation of static file server.
Try to access the root of static file server, and make
Expand All @@ -68,21 +66,20 @@ def test_access_root_of_static_handler(tmp_dir_path, loop, test_client,

# Register global static route:
app.router.add_static(prefix, tmp_dir_path, show_index=show_index)
client = yield from test_client(app)
client = await test_client(app)

# Request the root of the static directory.
r = yield from client.get(prefix)
r = await client.get(prefix)
assert r.status == status

if data:
assert r.headers['Content-Type'] == "text/html; charset=utf-8"
read_ = (yield from r.read())
read_ = (await r.read())
assert read_ == data


@pytest.mark.parametrize('data', ['hello world'])
@asyncio.coroutine
def test_follow_symlink(tmp_dir_path, loop, test_client, data):
async def test_follow_symlink(tmp_dir_path, loop, test_client, data):
"""
Tests the access to a symlink, in static folder
"""
Expand All @@ -100,21 +97,20 @@ def test_follow_symlink(tmp_dir_path, loop, test_client, data):

# Register global static route:
app.router.add_static('/', tmp_dir_path, follow_symlinks=True)
client = yield from test_client(app)
client = await test_client(app)

# Request the root of the static directory.
r = yield from client.get('/my_symlink/my_file_in_dir')
r = await client.get('/my_symlink/my_file_in_dir')
assert r.status == 200
assert (yield from r.text()) == data
assert (await r.text()) == data


@pytest.mark.parametrize('dir_name,filename,data', [
('', 'test file.txt', 'test text'),
('test dir name', 'test dir file .txt', 'test text file folder')
])
@asyncio.coroutine
def test_access_to_the_file_with_spaces(tmp_dir_path, loop, test_client,
dir_name, filename, data):
async def test_access_to_the_file_with_spaces(tmp_dir_path, loop, test_client,
dir_name, filename, data):
"""
Checks operation of static files with spaces
"""
Expand All @@ -134,15 +130,14 @@ def test_access_to_the_file_with_spaces(tmp_dir_path, loop, test_client,
url = os.path.join('/', dir_name, filename)

app.router.add_static('/', tmp_dir_path)
client = yield from test_client(app)
client = await test_client(app)

r = yield from client.get(url)
r = await client.get(url)
assert r.status == 200
assert (yield from r.text()) == data
assert (await r.text()) == data


@asyncio.coroutine
def test_access_non_existing_resource(tmp_dir_path, loop, test_client):
async def test_access_non_existing_resource(tmp_dir_path, loop, test_client):
"""
Tests accessing non-existing resource
Try to access a non-exiting resource and make sure that 404 HTTP status
Expand All @@ -152,10 +147,10 @@ def test_access_non_existing_resource(tmp_dir_path, loop, test_client):

# Register global static route:
app.router.add_static('/', tmp_dir_path, show_index=True)
client = yield from test_client(app)
client = await test_client(app)

# Request the root of the static directory.
r = yield from client.get('/non_existing_resource')
r = await client.get('/non_existing_resource')
assert r.status == 404


Expand All @@ -164,8 +159,7 @@ def test_access_non_existing_resource(tmp_dir_path, loop, test_client):
('/a@b', '/a@b'),
('/a:b', '/a%3Ab'),
])
@asyncio.coroutine
def test_url_escaping(loop, test_client, registered_path, request_url):
async def test_url_escaping(loop, test_client, registered_path, request_url):
"""
Tests accessing a resource with
"""
Expand All @@ -174,22 +168,20 @@ def test_url_escaping(loop, test_client, registered_path, request_url):
def handler(_):
return web.Response()
app.router.add_get(registered_path, handler)
client = yield from test_client(app)
client = await test_client(app)

r = yield from client.get(request_url)
r = await client.get(request_url)
assert r.status == 200


@asyncio.coroutine
def test_handler_metadata_persistence():
async def test_handler_metadata_persistence():
"""
Tests accessing metadata of a handler after registering it on the app
router.
"""
app = web.Application()

@asyncio.coroutine
def async_handler(_):
async def async_handler(_):
"""Doc"""
return web.Response()

Expand All @@ -205,8 +197,7 @@ def sync_handler(_):
assert route.handler.__doc__ == 'Doc'


@asyncio.coroutine
def test_unauthorized_folder_access(tmp_dir_path, loop, test_client):
async def test_unauthorized_folder_access(tmp_dir_path, loop, test_client):
"""
Tests the unauthorized access to a folder of static file server.
Try to list a folder content of static file server when server does not
Expand All @@ -226,15 +217,14 @@ def test_unauthorized_folder_access(tmp_dir_path, loop, test_client):

# Register global static route:
app.router.add_static('/', tmp_dir_path, show_index=True)
client = yield from test_client(app)
client = await test_client(app)

# Request the root of the static directory.
r = yield from client.get('/my_dir')
r = await client.get('/my_dir')
assert r.status == 403


@asyncio.coroutine
def test_access_symlink_loop(tmp_dir_path, loop, test_client):
async def test_access_symlink_loop(tmp_dir_path, loop, test_client):
"""
Tests the access to a looped symlink, which could not be resolved.
"""
Expand All @@ -245,15 +235,14 @@ def test_access_symlink_loop(tmp_dir_path, loop, test_client):

# Register global static route:
app.router.add_static('/', tmp_dir_path, show_index=True)
client = yield from test_client(app)
client = await test_client(app)

# Request the root of the static directory.
r = yield from client.get('/my_symlink')
r = await client.get('/my_symlink')
assert r.status == 404


@asyncio.coroutine
def test_access_special_resource(tmp_dir_path, loop, test_client):
async def test_access_special_resource(tmp_dir_path, loop, test_client):
"""
Tests the access to a resource that is neither a file nor a directory.
Checks that if a special resource is accessed (f.e. named pipe or UNIX
Expand All @@ -276,26 +265,24 @@ def test_access_special_resource(tmp_dir_path, loop, test_client):

# Register global static route:
app.router.add_static('/', tmp_dir_path, show_index=True)
client = yield from test_client(app)
client = await test_client(app)

# Request the root of the static directory.
r = yield from client.get('/special')
r = await client.get('/special')
assert r.status == 404


@asyncio.coroutine
def test_partialy_applied_handler(loop, test_client):
async def test_partialy_applied_handler(loop, test_client):
app = web.Application()

@asyncio.coroutine
def handler(data, request):
async def handler(data, request):
return web.Response(body=data)

app.router.add_route('GET', '/', functools.partial(handler, b'hello'))
client = yield from test_client(app)
client = await test_client(app)

r = yield from client.get('/')
data = (yield from r.read())
r = await client.get('/')
data = (await r.read())
assert data == b'hello'


Expand All @@ -312,26 +299,23 @@ def test_system_route():
assert 'test' == route.reason


@asyncio.coroutine
def test_412_is_returned(test_client):
async def test_412_is_returned(test_client):

class MyRouter(abc.AbstractRouter):

@asyncio.coroutine
def resolve(self, request):
async def resolve(self, request):
raise web.HTTPPreconditionFailed()

app = web.Application(router=MyRouter())

client = yield from test_client(app)
client = await test_client(app)

resp = yield from client.get('/')
resp = await client.get('/')

assert resp.status == 412


@asyncio.coroutine
def test_allow_head(loop, test_client):
async def test_allow_head(loop, test_client):
"""
Test allow_head on routes.
"""
Expand All @@ -341,20 +325,20 @@ def handler(_):
return web.Response()
app.router.add_get('/a', handler, name='a')
app.router.add_get('/b', handler, allow_head=False, name='b')
client = yield from test_client(app)
client = await test_client(app)

r = yield from client.get('/a')
r = await client.get('/a')
assert r.status == 200
yield from r.release()
await r.release()

r = yield from client.head('/a')
r = await client.head('/a')
assert r.status == 200
yield from r.release()
await r.release()

r = yield from client.get('/b')
r = await client.get('/b')
assert r.status == 200
yield from r.release()
await r.release()

r = yield from client.head('/b')
r = await client.head('/b')
assert r.status == 405
yield from r.release()
await r.release()
Loading

0 comments on commit 2a47ca2

Please sign in to comment.