Skip to content

Commit

Permalink
Added blueprint exception and middleware support, and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
channelcat committed Oct 16, 2016
1 parent 9b716e9 commit 40b1ec9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 16 deletions.
57 changes: 44 additions & 13 deletions sanic/blueprints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@


class BlueprintSetup():
class BlueprintSetup:
"""
"""

Expand All @@ -17,26 +15,41 @@ def __init__(self, blueprint, app, options):
#: blueprint.
self.url_prefix = url_prefix

def add_url_rule(self, uri, methods=None, handler=None, **options):
"""A helper method to register a handler to the application url routes.
def add_route(self, handler, uri, methods):
"""
A helper method to register a handler to the application url routes.
"""
if self.url_prefix:
uri = self.url_prefix + uri

self.app.router.add(uri, methods, handler)

def add_exception(self, handler, *args, **kwargs):
"""
Registers exceptions to sanic
"""
self.app.exception(*args, **kwargs)(handler)

class Blueprint():
def add_middleware(self, middleware, *args, **kwargs):
"""
Registers middleware to sanic
"""
if args or kwargs:
self.app.middleware(*args, **kwargs)(middleware)
else:
self.app.middleware(middleware)


class Blueprint:
def __init__(self, name, url_prefix=None):
self.name = name
self.url_prefix = url_prefix
self.deferred_functions = []

def record(self, func):
"""Registers a callback function that is invoked when the blueprint is
"""
Registers a callback function that is invoked when the blueprint is
registered on the application.
"""
self.deferred_functions.append(func)

Expand All @@ -57,12 +70,30 @@ def route(self, uri, methods=None):
"""
"""
def decorator(handler):
self.add_url_rule(uri=uri, methods=methods, handler=handler)
self.record(lambda s: s.add_route(handler, uri, methods))
return handler
return decorator

def add_url_rule(self, uri, methods=None, handler=None):
def middleware(self, *args, **kwargs):
"""
"""

def register_middleware(middleware):
self.record(lambda s: s.add_middleware(middleware, *args, **kwargs))
return middleware

# Detect which way this was called, @middleware or @middleware('AT')
if len(args) == 1 and len(kwargs) == 0 and callable(args[0]):
args = []
return register_middleware(args[0])
else:
return register_middleware

def exception(self, *args, **kwargs):
"""
"""
self.record(lambda s:
s.add_url_rule(uri, methods, handler))
def decorator(handler):
self.record(lambda s: s.add_exception(handler, *args, **kwargs))
return handler
return decorator

57 changes: 54 additions & 3 deletions tests/test_blueprints.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from json import loads as json_loads, dumps as json_dumps
from sanic import Sanic
from sanic import Blueprint
from sanic.blueprints import Blueprint
from sanic.response import json, text
from sanic.utils import sanic_endpoint_test
from sanic.exceptions import SanicException
from sanic.exceptions import NotFound, ServerError, InvalidUsage


# ------------------------------------------------------------ #
Expand Down Expand Up @@ -58,3 +57,55 @@ def handler2(request):
request, response = sanic_endpoint_test(app, uri='/test2/')
assert response.text == 'Hello2'


def test_bp_middleware():
app = Sanic('test_middleware')
blueprint = Blueprint('test_middleware')

@blueprint.middleware('response')
async def process_response(request, response):
return text('OK')

@app.route('/')
async def handler(request):
return text('FAIL')

app.register_blueprint(blueprint)

request, response = sanic_endpoint_test(app)

assert response.status == 200
assert response.text == 'OK'

def test_bp_exception_handler():
app = Sanic('test_middleware')
blueprint = Blueprint('test_middleware')

@blueprint.route('/1')
def handler_1(request):
raise InvalidUsage("OK")

@blueprint.route('/2')
def handler_2(request):
raise ServerError("OK")

@blueprint.route('/3')
def handler_3(request):
raise NotFound("OK")

@blueprint.exception(NotFound, ServerError)
def handler_exception(request, exception):
return text("OK")

app.register_blueprint(blueprint)

request, response = sanic_endpoint_test(app, uri='/1')
assert response.status == 400


request, response = sanic_endpoint_test(app, uri='/2')
assert response.status == 200
assert response.text == 'OK'

request, response = sanic_endpoint_test(app, uri='/3')
assert response.status == 200

0 comments on commit 40b1ec9

Please sign in to comment.