2018-08-26 16:43:14 +02:00
|
|
|
from sanic.response import text
|
2017-01-08 15:48:12 -08:00
|
|
|
|
|
|
|
|
2018-08-26 16:43:14 +02:00
|
|
|
def test_vhosts(app):
|
2018-12-30 13:18:06 +02:00
|
|
|
@app.route("/", host="example.com")
|
2018-10-22 22:25:38 +02:00
|
|
|
async def handler1(request):
|
2017-01-08 15:48:12 -08:00
|
|
|
return text("You're at example.com!")
|
|
|
|
|
2018-12-30 13:18:06 +02:00
|
|
|
@app.route("/", host="subdomain.example.com")
|
2018-10-22 22:25:38 +02:00
|
|
|
async def handler2(request):
|
2017-01-08 15:48:12 -08:00
|
|
|
return text("You're at subdomain.example.com!")
|
|
|
|
|
|
|
|
headers = {"Host": "example.com"}
|
2018-12-30 13:18:06 +02:00
|
|
|
request, response = app.test_client.get("/", headers=headers)
|
2017-01-08 15:48:12 -08:00
|
|
|
assert response.text == "You're at example.com!"
|
|
|
|
|
|
|
|
headers = {"Host": "subdomain.example.com"}
|
2018-12-30 13:18:06 +02:00
|
|
|
request, response = app.test_client.get("/", headers=headers)
|
2017-01-08 15:48:12 -08:00
|
|
|
assert response.text == "You're at subdomain.example.com!"
|
2017-01-18 19:40:20 -08:00
|
|
|
|
|
|
|
|
2018-08-26 16:43:14 +02:00
|
|
|
def test_vhosts_with_list(app):
|
2018-12-30 13:18:06 +02:00
|
|
|
@app.route("/", host=["hello.com", "world.com"])
|
2017-01-18 19:40:20 -08:00
|
|
|
async def handler(request):
|
|
|
|
return text("Hello, world!")
|
|
|
|
|
|
|
|
headers = {"Host": "hello.com"}
|
2018-12-30 13:18:06 +02:00
|
|
|
request, response = app.test_client.get("/", headers=headers)
|
2017-01-18 19:40:20 -08:00
|
|
|
assert response.text == "Hello, world!"
|
|
|
|
|
|
|
|
headers = {"Host": "world.com"}
|
2018-12-30 13:18:06 +02:00
|
|
|
request, response = app.test_client.get("/", headers=headers)
|
2017-01-18 19:40:20 -08:00
|
|
|
assert response.text == "Hello, world!"
|
2017-02-20 16:36:48 -08:00
|
|
|
|
2018-08-26 16:43:14 +02:00
|
|
|
|
|
|
|
def test_vhosts_with_defaults(app):
|
2018-12-30 13:18:06 +02:00
|
|
|
@app.route("/", host="hello.com")
|
2018-10-22 22:25:38 +02:00
|
|
|
async def handler1(request):
|
2017-02-20 16:36:48 -08:00
|
|
|
return text("Hello, world!")
|
|
|
|
|
2018-12-30 13:18:06 +02:00
|
|
|
@app.route("/")
|
2018-10-22 22:25:38 +02:00
|
|
|
async def handler2(request):
|
2017-02-20 16:36:48 -08:00
|
|
|
return text("default")
|
|
|
|
|
|
|
|
headers = {"Host": "hello.com"}
|
2018-12-30 13:18:06 +02:00
|
|
|
request, response = app.test_client.get("/", headers=headers)
|
2017-02-20 16:36:48 -08:00
|
|
|
assert response.text == "Hello, world!"
|
|
|
|
|
2018-12-30 13:18:06 +02:00
|
|
|
request, response = app.test_client.get("/")
|
2017-02-20 16:36:48 -08:00
|
|
|
assert response.text == "default"
|
2018-12-22 23:21:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_remove_vhost_route(app):
|
2018-12-30 13:18:06 +02:00
|
|
|
@app.route("/", host="example.com")
|
2018-12-22 23:21:45 +08:00
|
|
|
async def handler1(request):
|
|
|
|
return text("You're at example.com!")
|
|
|
|
|
|
|
|
headers = {"Host": "example.com"}
|
2018-12-30 13:18:06 +02:00
|
|
|
request, response = app.test_client.get("/", headers=headers)
|
2018-12-22 23:21:45 +08:00
|
|
|
assert response.status == 200
|
|
|
|
|
2018-12-30 13:18:06 +02:00
|
|
|
app.remove_route("/", host="example.com")
|
|
|
|
request, response = app.test_client.get("/", headers=headers)
|
2018-12-22 23:21:45 +08:00
|
|
|
assert response.status == 404
|