Added route shorthands to blueprints

This commit is contained in:
Channel Cat 2017-01-29 23:20:38 -08:00
parent 38d1ed76d2
commit 0ef39f35ae
2 changed files with 90 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,71 @@ 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')
def handler(request):
return text('OK')
def handler(request):
return text('OK')
def handler(request):
return text('OK')
def handler(request):
return text('OK')
def handler(request):
return text('OK')
def handler(request):
return text('OK')
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')
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