Merge pull request #312 from r0fls/allow-vhost-lists
allow using a list of hosts on a route
This commit is contained in:
commit
99c8d779dc
|
@ -11,9 +11,16 @@ from sanic.blueprints import Blueprint
|
||||||
app = Sanic()
|
app = Sanic()
|
||||||
bp = Blueprint("bp", host="bp.example.com")
|
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")
|
@app.route('/', host="example.com")
|
||||||
async def hello(request):
|
async def hello(request):
|
||||||
return text("Answer")
|
return text("Answer")
|
||||||
|
|
||||||
@app.route('/', host="sub.example.com")
|
@app.route('/', host="sub.example.com")
|
||||||
async def hello(request):
|
async def hello(request):
|
||||||
return text("42")
|
return text("42")
|
||||||
|
|
|
@ -76,8 +76,15 @@ class Router:
|
||||||
if self.hosts is None:
|
if self.hosts is None:
|
||||||
self.hosts = set(host)
|
self.hosts = set(host)
|
||||||
else:
|
else:
|
||||||
|
if isinstance(host, list):
|
||||||
|
host = set(host)
|
||||||
self.hosts.add(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
|
# Dict for faster lookups of if method allowed
|
||||||
if methods:
|
if methods:
|
||||||
|
|
|
@ -4,7 +4,7 @@ from sanic.utils import sanic_endpoint_test
|
||||||
|
|
||||||
|
|
||||||
def test_vhosts():
|
def test_vhosts():
|
||||||
app = Sanic('test_text')
|
app = Sanic('test_vhosts')
|
||||||
|
|
||||||
@app.route('/', host="example.com")
|
@app.route('/', host="example.com")
|
||||||
async def handler(request):
|
async def handler(request):
|
||||||
|
@ -21,3 +21,19 @@ def test_vhosts():
|
||||||
headers = {"Host": "subdomain.example.com"}
|
headers = {"Host": "subdomain.example.com"}
|
||||||
request, response = sanic_endpoint_test(app, headers=headers)
|
request, response = sanic_endpoint_test(app, headers=headers)
|
||||||
assert response.text == "You're at subdomain.example.com!"
|
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