From 15e4ec7ffb25e33a5dafa5950c18a25062e16425 Mon Sep 17 00:00:00 2001 From: Raphael Deem Date: Tue, 10 Jan 2017 22:08:15 -0800 Subject: [PATCH] add ability to override default host in blueprint --- sanic/blueprints.py | 8 ++++---- tests/test_blueprints.py | 11 ++++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/sanic/blueprints.py b/sanic/blueprints.py index 4cf460f3..583aa244 100644 --- a/sanic/blueprints.py +++ b/sanic/blueprints.py @@ -87,18 +87,18 @@ class Blueprint: for deferred in self.deferred_functions: deferred(state) - def route(self, uri, methods=None): + def route(self, uri, methods=None, host=None): """ """ def decorator(handler): - self.record(lambda s: s.add_route(handler, uri, methods)) + self.record(lambda s: s.add_route(handler, uri, methods, host)) return handler return decorator - def add_route(self, handler, uri, methods=None): + def add_route(self, handler, uri, methods=None, host=None): """ """ - self.record(lambda s: s.add_route(handler, uri, methods)) + self.record(lambda s: s.add_route(handler, uri, methods, host)) return handler def listener(self, event): diff --git a/tests/test_blueprints.py b/tests/test_blueprints.py index 04c5c59d..75109e2c 100644 --- a/tests/test_blueprints.py +++ b/tests/test_blueprints.py @@ -67,13 +67,22 @@ def test_bp_with_host(): def handler(request): return text('Hello') + @bp.route('/', host="sub.example.com") + def handler(request): + return text('Hello subdomain!') + app.blueprint(bp) headers = {"Host": "example.com"} request, response = sanic_endpoint_test(app, uri='/test1/', headers=headers) - assert response.text == 'Hello' + headers = {"Host": "sub.example.com"} + request, response = sanic_endpoint_test(app, uri='/test1/', + headers=headers) + + assert response.text == 'Hello subdomain!' + def test_several_bp_with_host(): app = Sanic('test_text')