add ability to override default host in blueprint

This commit is contained in:
Raphael Deem 2017-01-10 22:08:15 -08:00
parent 62df50e22b
commit 15e4ec7ffb
2 changed files with 14 additions and 5 deletions

View File

@ -87,18 +87,18 @@ class Blueprint:
for deferred in self.deferred_functions: for deferred in self.deferred_functions:
deferred(state) deferred(state)
def route(self, uri, methods=None): def route(self, uri, methods=None, host=None):
""" """
""" """
def decorator(handler): 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 handler
return decorator 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 return handler
def listener(self, event): def listener(self, event):

View File

@ -67,13 +67,22 @@ def test_bp_with_host():
def handler(request): def handler(request):
return text('Hello') return text('Hello')
@bp.route('/', host="sub.example.com")
def handler(request):
return text('Hello subdomain!')
app.blueprint(bp) app.blueprint(bp)
headers = {"Host": "example.com"} headers = {"Host": "example.com"}
request, response = sanic_endpoint_test(app, uri='/test1/', request, response = sanic_endpoint_test(app, uri='/test1/',
headers=headers) headers=headers)
assert response.text == 'Hello' 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(): def test_several_bp_with_host():
app = Sanic('test_text') app = Sanic('test_text')