sanic/tests/test_vhosts.py
L. Kärkkäinen 48800e657f
Deprecation and test cleanup (#1818)
* Remove remove_route, deprecated in 19.6.

* No need for py35 compat anymore.

* Rewrite asyncio.coroutines with async/await.

* Remove deprecated request.raw_args.

* response.text() takes str only: avoid deprecation warning in all but one test.

* Remove unused import.

* Revert unnecessary deprecation warning.

* Remove apparently unnecessary py38 compat.

* Avoid asyncio.Task.all_tasks deprecation warning.

* Avoid warning on a test that tests deprecated response.text(int).

* Add pytest-asyncio to tox deps.

* Run the coroutine returned by AsyncioServer.close.

Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
2020-03-28 11:43:14 -07:00

51 lines
1.5 KiB
Python

from sanic.response import text
def test_vhosts(app):
@app.route("/", host="example.com")
async def handler1(request):
return text("You're at example.com!")
@app.route("/", host="subdomain.example.com")
async def handler2(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):
@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!"
def test_vhosts_with_defaults(app):
@app.route("/", host="hello.com")
async def handler1(request):
return text("Hello, world!")
@app.route("/")
async def handler2(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"