Skip to content

Commit

Permalink
add web.options, fix #3062 (#3063)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin authored and asvetlov committed Jun 9, 2018
1 parent 4b54daf commit f26a49d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/3062.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fix `UrlDispatcher` has no attribute `add_options`, add `web.options`
6 changes: 5 additions & 1 deletion aiohttp/web_routedef.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from . import hdrs


__all__ = ('RouteDef', 'StaticDef', 'RouteTableDef', 'head', 'get',
__all__ = ('RouteDef', 'StaticDef', 'RouteTableDef', 'head', 'options', 'get',
'post', 'patch', 'put', 'delete', 'route', 'view',
'static')

Expand Down Expand Up @@ -67,6 +67,10 @@ def head(path, handler, **kwargs):
return route(hdrs.METH_HEAD, path, handler, **kwargs)


def options(path, handler, **kwargs):
return route(hdrs.METH_OPTIONS, path, handler, **kwargs)


def get(path, handler, *, name=None, allow_head=True, **kwargs):
return route(hdrs.METH_GET, path, handler, name=name,
allow_head=allow_head, **kwargs)
Expand Down
6 changes: 6 additions & 0 deletions aiohttp/web_urldispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,12 @@ def add_head(self, path, handler, **kwargs):
"""
return self.add_route(hdrs.METH_HEAD, path, handler, **kwargs)

def add_options(self, path, handler, **kwargs):
"""
Shortcut for add_route with method OPTIONS
"""
return self.add_route(hdrs.METH_OPTIONS, path, handler, **kwargs)

def add_get(self, path, handler, *, name=None, allow_head=True, **kwargs):
"""
Shortcut for add_route with method GET, if allow_head is true another
Expand Down
13 changes: 13 additions & 0 deletions tests/test_route_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ async def handler(request):
assert str(route.url_for()) == '/'


def test_options(router):
async def handler(request):
pass

router.add_routes([web.options('/', handler)])
assert len(router.routes()) == 1

route = list(router.routes())[0]
assert route.handler is handler
assert route.method == 'OPTIONS'
assert str(route.url_for()) == '/'


def test_post(router):
async def handler(request):
pass
Expand Down

0 comments on commit f26a49d

Please sign in to comment.