sanic/tests/test_vhosts.py
2017-02-14 14:51:20 -05:00

39 lines
1.2 KiB
Python

from sanic import Sanic
from sanic.response import json, text
def test_vhosts():
app = Sanic('test_vhosts')
@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"}
request, response = app.test_client.get('/', headers=headers)
assert response.text == "You're at example.com!"
headers = {"Host": "subdomain.example.com"}
request, response = app.test_client.get('/', 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 = app.test_client.get('/', headers=headers)
assert response.text == "Hello, world!"
headers = {"Host": "world.com"}
request, response = app.test_client.get('/', headers=headers)
assert response.text == "Hello, world!"