Merge pull request #371 from channelcat/blueprint-shorthand

Blueprint route shorthand
This commit is contained in:
Raphael Deem 2017-01-29 23:23:47 -08:00 committed by GitHub
commit cfb5734b85
2 changed files with 98 additions and 0 deletions

View File

@ -130,3 +130,25 @@ class Blueprint:
""" """
static = FutureStatic(uri, file_or_directory, args, kwargs) static = FutureStatic(uri, file_or_directory, args, kwargs)
self.statics.append(static) 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)

View File

@ -228,3 +228,79 @@ def test_bp_static():
request, response = sanic_endpoint_test(app, uri='/testing.file') request, response = sanic_endpoint_test(app, uri='/testing.file')
assert response.status == 200 assert response.status == 200
assert response.body == current_file_contents 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