diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 3755e8b1..c9a4b8ac 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -130,3 +130,25 @@ class Blueprint: """ static = FutureStatic(uri, file_or_directory, args, kwargs) self.statics.append(static) + + # 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 delete(self, uri, host=None): + return self.route(uri, methods=["DELETE"], host=host) diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 75109e2c..aebb7429 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -228,3 +228,79 @@ def test_bp_static(): request, response = sanic_endpoint_test(app, uri='/testing.file') assert response.status == 200 assert response.body == current_file_contents + +def test_bp_shorthand(): + app = Sanic('test_shorhand_routes') + blueprint = Blueprint('test_shorhand_routes') + + @blueprint.get('/get') + def handler(request): + return text('OK') + + @blueprint.put('/put') + def handler(request): + return text('OK') + + @blueprint.post('/post') + def handler(request): + return text('OK') + + @blueprint.head('/head') + def handler(request): + return text('OK') + + @blueprint.options('/options') + def handler(request): + return text('OK') + + @blueprint.patch('/patch') + def handler(request): + return text('OK') + + @blueprint.delete('/delete') + def handler(request): + return text('OK') + + app.blueprint(blueprint) + + 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 + + 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='/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='/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 + + 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='/delete', method='delete') + assert response.text == 'OK' + + request, response = sanic_endpoint_test(app, uri='/delete', method='get') + assert response.status == 405