sanic/tests/test_vhosts.py

54 lines
1.5 KiB
Python
Raw Normal View History

2018-08-26 15:43:14 +01:00
from sanic.response import text
2017-01-08 23:48:12 +00:00
2018-08-26 15:43:14 +01:00
def test_vhosts(app):
2017-01-08 23:48:12 +00:00
@app.route('/', host="example.com")
async def handler(request):
return text("You're at example.com!")
@app.route('/', host="subdomain.example.com")
async def handler(request):
return text("You're at subdomain.example.com!")
headers = {"Host": "example.com"}
2017-02-14 19:51:20 +00:00
request, response = app.test_client.get('/', headers=headers)
2017-01-08 23:48:12 +00:00
assert response.text == "You're at example.com!"
headers = {"Host": "subdomain.example.com"}
2017-02-14 19:51:20 +00:00
request, response = app.test_client.get('/', headers=headers)
2017-01-08 23:48:12 +00:00
assert response.text == "You're at subdomain.example.com!"
2017-01-19 03:40:20 +00:00
2018-08-26 15:43:14 +01:00
def test_vhosts_with_list(app):
2017-01-19 03:40:20 +00:00
@app.route('/', host=["hello.com", "world.com"])
async def handler(request):
return text("Hello, world!")
headers = {"Host": "hello.com"}
2017-02-14 19:51:20 +00:00
request, response = app.test_client.get('/', headers=headers)
2017-01-19 03:40:20 +00:00
assert response.text == "Hello, world!"
headers = {"Host": "world.com"}
2017-02-14 19:51:20 +00:00
request, response = app.test_client.get('/', headers=headers)
2017-01-19 03:40:20 +00:00
assert response.text == "Hello, world!"
2017-02-21 00:36:48 +00:00
2018-08-26 15:43:14 +01:00
def test_vhosts_with_defaults(app):
2017-02-21 00:36:48 +00:00
@app.route('/', host="hello.com")
async def handler(request):
return text("Hello, world!")
@app.route('/')
async def handler(request):
return text("default")
headers = {"Host": "hello.com"}
request, response = app.test_client.get('/', headers=headers)
assert response.text == "Hello, world!"
request, response = app.test_client.get('/')
assert response.text == "default"