Skip to content

Commit

Permalink
Add support for all HTTP verbs in HttpComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
Damien Nicolas committed May 13, 2016
1 parent 6c8067c commit 2da54b7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 6 deletions.
16 changes: 14 additions & 2 deletions src/tygs/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,29 @@ def render_to_response_dict(self, response):


class HttpComponent(Component):

GET = 'GET'
POST = 'POST'
PUT = 'PUT'
PATCH = 'PATCH'
HEAD = 'HEAD'
OPTIONS = 'OPTIONS'
DELETE = 'DELETE'
methods = (GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE)

def __init__(self, app):
super().__init__(app)
self.router = Router()
for meth in self.methods:
setattr(self, meth.lower(), partial(self.route, methods=[meth]))
# TODO: figure out namespace cascading from the app tree architecture

# TODO: use explicit arguments
def get(self, url, *args, **kwargs):
def route(self, url, methods=None, *args, **kwargs):
def decorator(func):
# TODO: allow passing explicit endpoint
endpoint = "{}.{}".format(self.app.ns, func.__name__)
self.router.add_route(url, endpoint, func)
self.router.add_route(url, endpoint, func, methods=methods)
return func
return decorator

Expand Down
4 changes: 2 additions & 2 deletions src/tygs/http/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def __init__(self):
self.handlers = {}

# TODO: remplace args and kwargs with explicit parameters
def add_route(self, url, endpoint, handler, *args, **kwargs):
rule = Rule(url, endpoint=endpoint, *args, **kwargs)
def add_route(self, url, endpoint, handler, methods=None, *args, **kwargs):
rule = Rule(url, endpoint=endpoint, methods=methods, *args, **kwargs)
self.handlers[endpoint] = handler
self.url_map.add(rule)

Expand Down
102 changes: 100 additions & 2 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_jinja2_renderer_render_to_response_dict(app):


@pytest.mark.asyncio
async def test_http_component(app):
async def test_http_component_get(app):

http = components.HttpComponent(app)
assert isinstance(http.router, Router)
Expand All @@ -130,8 +130,106 @@ async def test_http_component(app):
@http.get('/troloo')
def foo():
pass

args = ['/troloo', 'namespace.foo', foo]
kwargs = {'methods': ['GET']}
http.router.add_route.assert_called_once_with(*args, **kwargs)


@pytest.mark.asyncio
async def test_http_component_post(app):

http = components.HttpComponent(app)
assert isinstance(http.router, Router)
http.router.add_route = MagicMock()

@http.post('/troloo')
def foo():
pass

args = ['/troloo', 'namespace.foo', foo]
kwargs = {'methods': ['POST']}
http.router.add_route.assert_called_once_with(*args, **kwargs)


@pytest.mark.asyncio
async def test_http_component_put(app):

http = components.HttpComponent(app)
assert isinstance(http.router, Router)
http.router.add_route = MagicMock()

@http.put('/troloo')
def foo():
pass

args = ['/troloo', 'namespace.foo', foo]
kwargs = {'methods': ['PUT']}
http.router.add_route.assert_called_once_with(*args, **kwargs)


@pytest.mark.asyncio
async def test_http_component_patch(app):

http = components.HttpComponent(app)
assert isinstance(http.router, Router)
http.router.add_route = MagicMock()

@http.patch('/troloo')
def foo():
pass

args = ['/troloo', 'namespace.foo', foo]
kwargs = {'methods': ['PATCH']}
http.router.add_route.assert_called_once_with(*args, **kwargs)


@pytest.mark.asyncio
async def test_http_component_options(app):

http = components.HttpComponent(app)
assert isinstance(http.router, Router)
http.router.add_route = MagicMock()

@http.options('/troloo')
def foo():
pass

args = ['/troloo', 'namespace.foo', foo]
kwargs = {'methods': ['OPTIONS']}
http.router.add_route.assert_called_once_with(*args, **kwargs)


@pytest.mark.asyncio
async def test_http_component_head(app):

http = components.HttpComponent(app)
assert isinstance(http.router, Router)
http.router.add_route = MagicMock()

@http.head('/troloo')
def foo():
pass

args = ['/troloo', 'namespace.foo', foo]
kwargs = {'methods': ['HEAD']}
http.router.add_route.assert_called_once_with(*args, **kwargs)


@pytest.mark.asyncio
async def test_http_component_delete(app):

http = components.HttpComponent(app)
assert isinstance(http.router, Router)
http.router.add_route = MagicMock()

@http.delete('/troloo')
def foo():
pass

args = ['/troloo', 'namespace.foo', foo]
http.router.add_route.assert_called_once_with(*args)
kwargs = {'methods': ['DELETE']}
http.router.add_route.assert_called_once_with(*args, **kwargs)


@pytest.fixture
Expand Down

0 comments on commit 2da54b7

Please sign in to comment.