allow using a list of hosts on a route
This commit is contained in:
parent
e218a59911
commit
2c1ff5bf5d
|
@ -11,9 +11,16 @@ from sanic.blueprints import Blueprint
|
|||
app = Sanic()
|
||||
bp = Blueprint("bp", host="bp.example.com")
|
||||
|
||||
@app.route('/', host=["example.com",
|
||||
"somethingelse.com",
|
||||
"therestofyourdomains.com"])
|
||||
async def hello(request):
|
||||
return text("Some defaults")
|
||||
|
||||
@app.route('/', host="example.com")
|
||||
async def hello(request):
|
||||
return text("Answer")
|
||||
|
||||
@app.route('/', host="sub.example.com")
|
||||
async def hello(request):
|
||||
return text("42")
|
||||
|
|
|
@ -76,8 +76,15 @@ class Router:
|
|||
if self.hosts is None:
|
||||
self.hosts = set(host)
|
||||
else:
|
||||
if isinstance(host, list):
|
||||
host = set(host)
|
||||
self.hosts.add(host)
|
||||
uri = host + uri
|
||||
if isinstance(host, str):
|
||||
uri = host + uri
|
||||
else:
|
||||
for h in host:
|
||||
self.add(uri, methods, handler, h)
|
||||
return
|
||||
|
||||
# Dict for faster lookups of if method allowed
|
||||
if methods:
|
||||
|
|
|
@ -4,7 +4,7 @@ from sanic.utils import sanic_endpoint_test
|
|||
|
||||
|
||||
def test_vhosts():
|
||||
app = Sanic('test_text')
|
||||
app = Sanic('test_vhosts')
|
||||
|
||||
@app.route('/', host="example.com")
|
||||
async def handler(request):
|
||||
|
@ -21,3 +21,19 @@ def test_vhosts():
|
|||
headers = {"Host": "subdomain.example.com"}
|
||||
request, response = sanic_endpoint_test(app, headers=headers)
|
||||
assert response.text == "You're at subdomain.example.com!"
|
||||
|
||||
|
||||
def test_vhosts_with_list():
|
||||
app = Sanic('test_vhosts')
|
||||
|
||||
@app.route('/', host=["hello.com", "world.com"])
|
||||
async def handler(request):
|
||||
return text("Hello, world!")
|
||||
|
||||
headers = {"Host": "hello.com"}
|
||||
request, response = sanic_endpoint_test(app, headers=headers)
|
||||
assert response.text == "Hello, world!"
|
||||
|
||||
headers = {"Host": "world.com"}
|
||||
request, response = sanic_endpoint_test(app, headers=headers)
|
||||
assert response.text == "Hello, world!"
|
||||
|
|
Loading…
Reference in New Issue
Block a user