From 96424b6b0a6e293ab08a48e7dd69110ede57ee2d Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Thu, 19 Jan 2017 23:47:07 -0800 Subject: [PATCH 1/2] add method shorthands --- sanic/sanic.py | 19 +++++++++++++ tests/test_routes.py | 67 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/sanic/sanic.py b/sanic/sanic.py index 94fcd983..15217de9 100644 --- a/sanic/sanic.py +++ b/sanic/sanic.py @@ -66,6 +66,25 @@ class Sanic: return response + # Shorthand method decorators + def get(self, uri, host=None): + return self.route(uri, methods=["GET"], host=host) + + def post(self, uri, host=None): + return self.route(uri, methods=["POST"], host=host) + + def put(self, uri, host=None): + return self.route(uri, methods=["PUT"], host=host) + + def head(self, uri, host=None): + return self.route(uri, methods=["HEAD"], host=host) + + def options(self, uri, host=None): + return self.route(uri, methods=["OPTIONS"], host=host) + + def patch(self, uri, host=None): + return self.route(uri, methods=["PATCH"], host=host) + def add_route(self, handler, uri, methods=None, host=None): """ A helper method to register class instance or diff --git a/tests/test_routes.py b/tests/test_routes.py index 9c671829..43023ed0 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -10,6 +10,73 @@ from sanic.utils import sanic_endpoint_test # UTF-8 # ------------------------------------------------------------ # +def test_shorthand_routes(): + app = Sanic('test_shorhand_routes') + + @app.get('') + def handler(request): + return text('OK') + + @app.post('/post') + def handler(request): + return text('OK') + + @app.put('/put') + def handler(request): + return text('OK') + + @app.patch('/patch') + def handler(request): + return text('OK') + + @app.head('/head') + def handler(request): + return text('OK') + + @app.options('/options') + def handler(request): + return text('OK') + + request, response = sanic_endpoint_test(app, uri='/') + assert response.text == 'OK' + + request, response = sanic_endpoint_test(app, uri='/', method='post') + assert response.status == 405 + + request, response = sanic_endpoint_test(app, uri='/post', method='post') + assert response.text == 'OK' + + request, response = sanic_endpoint_test(app, uri='/post', method='get') + assert response.status == 405 + + request, response = sanic_endpoint_test(app, uri='/put', method='put') + assert response.text == 'OK' + + request, response = sanic_endpoint_test(app, uri='/put', method='get') + assert response.status == 405 + + request, response = sanic_endpoint_test(app, uri='/patch', + method='patch') + assert response.text == 'OK' + + request, response = sanic_endpoint_test(app, uri='/patch', method='get') + assert response.status == 405 + + request, response = sanic_endpoint_test(app, uri='/head', method='head') + assert response.status== 200 + + request, response = sanic_endpoint_test(app, uri='/head', method='get') + assert response.status == 405 + + request, response = sanic_endpoint_test(app, uri='/options', + method='options') + assert response.text == 'OK' + + request, response = sanic_endpoint_test(app, uri='/options', + method='get') + assert response.status == 405 + + def test_static_routes(): app = Sanic('test_dynamic_route') From 6fd69b628425c1fc0aac33c48e69f36a72a2c4f5 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Fri, 20 Jan 2017 10:00:51 -0800 Subject: [PATCH 2/2] separate tests --- tests/test_routes.py | 79 +++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/tests/test_routes.py b/tests/test_routes.py index 43023ed0..16a1c767 100644 --- a/tests/test_routes.py +++ b/tests/test_routes.py @@ -10,73 +10,84 @@ from sanic.utils import sanic_endpoint_test # UTF-8 # ------------------------------------------------------------ # -def test_shorthand_routes(): - app = Sanic('test_shorhand_routes') +def test_shorthand_routes_get(): + app = Sanic('test_shorhand_routes_get') - @app.get('') + @app.get('/get') def handler(request): return text('OK') + request, response = sanic_endpoint_test(app, uri='/get', method='get') + assert response.text == 'OK' + + request, response = sanic_endpoint_test(app, uri='/get', method='post') + assert response.status == 405 + +def test_shorthand_routes_post(): + app = Sanic('test_shorhand_routes_post') + @app.post('/post') def handler(request): return text('OK') - @app.put('/put') - def handler(request): - return text('OK') - - @app.patch('/patch') - def handler(request): - return text('OK') - - @app.head('/head') - def handler(request): - return text('OK') - - @app.options('/options') - def handler(request): - return text('OK') - - request, response = sanic_endpoint_test(app, uri='/') - assert response.text == 'OK' - - request, response = sanic_endpoint_test(app, uri='/', method='post') - assert response.status == 405 - request, response = sanic_endpoint_test(app, uri='/post', method='post') assert response.text == 'OK' request, response = sanic_endpoint_test(app, uri='/post', method='get') assert response.status == 405 +def test_shorthand_routes_put(): + app = Sanic('test_shorhand_routes_put') + + @app.put('/put') + def handler(request): + return text('OK') + request, response = sanic_endpoint_test(app, uri='/put', method='put') assert response.text == 'OK' request, response = sanic_endpoint_test(app, uri='/put', method='get') assert response.status == 405 - request, response = sanic_endpoint_test(app, uri='/patch', - method='patch') +def test_shorthand_routes_patch(): + app = Sanic('test_shorhand_routes_patch') + + @app.patch('/patch') + def handler(request): + return text('OK') + + request, response = sanic_endpoint_test(app, uri='/patch', method='patch') assert response.text == 'OK' request, response = sanic_endpoint_test(app, uri='/patch', method='get') assert response.status == 405 +def test_shorthand_routes_head(): + app = Sanic('test_shorhand_routes_head') + + @app.head('/head') + def handler(request): + return text('OK') + request, response = sanic_endpoint_test(app, uri='/head', method='head') - assert response.status== 200 + assert response.status == 200 request, response = sanic_endpoint_test(app, uri='/head', method='get') assert response.status == 405 - request, response = sanic_endpoint_test(app, uri='/options', - method='options') - assert response.text == 'OK' +def test_shorthand_routes_options(): + app = Sanic('test_shorhand_routes_options') - request, response = sanic_endpoint_test(app, uri='/options', - method='get') + @app.options('/options') + def handler(request): + return text('OK') + + request, response = sanic_endpoint_test(app, uri='/options', method='options') + assert response.status == 200 + + request, response = sanic_endpoint_test(app, uri='/options', method='get') assert response.status == 405 - def test_static_routes(): app = Sanic('test_dynamic_route')