2020-06-28 12:45:52 +01:00
|
|
|
import asyncio
|
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
import pytest
|
|
|
|
|
2021-01-25 00:14:48 +00:00
|
|
|
from sanic_testing.testing import SanicTestClient
|
|
|
|
|
2020-06-28 12:45:52 +01:00
|
|
|
from sanic.blueprints import Blueprint
|
|
|
|
|
|
|
|
|
2020-02-21 17:10:22 +00:00
|
|
|
def test_routes_with_host(app):
|
|
|
|
@app.route("/", name="hostindex", host="example.com")
|
|
|
|
@app.route("/path", name="hostpath", host="path.example.com")
|
|
|
|
def index(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert app.url_for("hostindex") == "/"
|
|
|
|
assert app.url_for("hostpath") == "/path"
|
|
|
|
assert app.url_for("hostindex", _external=True) == "http://example.com/"
|
2020-06-05 15:14:18 +01:00
|
|
|
assert (
|
|
|
|
app.url_for("hostpath", _external=True)
|
|
|
|
== "http://path.example.com/path"
|
|
|
|
)
|
2020-06-28 12:45:52 +01:00
|
|
|
|
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
def test_routes_with_multiple_hosts(app):
|
|
|
|
@app.route("/", name="hostindex", host=["example.com", "path.example.com"])
|
|
|
|
def index(request):
|
|
|
|
pass
|
|
|
|
|
|
|
|
assert app.url_for("hostindex") == "/"
|
|
|
|
assert (
|
|
|
|
app.url_for("hostindex", _host="example.com") == "http://example.com/"
|
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(ValueError) as e:
|
|
|
|
assert app.url_for("hostindex", _external=True)
|
|
|
|
assert str(e.value).startswith("Host is ambiguous")
|
|
|
|
|
|
|
|
with pytest.raises(ValueError) as e:
|
|
|
|
assert app.url_for("hostindex", _host="unknown.com")
|
|
|
|
assert str(e.value).startswith(
|
|
|
|
"Requested host (unknown.com) is not available for this route"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-05 20:55:42 +01:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"name,expected",
|
|
|
|
(
|
|
|
|
("test_route", "/bp/route"),
|
|
|
|
("test_route2", "/bp/route2"),
|
|
|
|
("foobar_3", "/bp/route3"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_websocket_bp_route_name(app, name, expected):
|
2020-06-28 12:45:52 +01:00
|
|
|
"""Tests that blueprint websocket route is named."""
|
|
|
|
event = asyncio.Event()
|
|
|
|
bp = Blueprint("test_bp", url_prefix="/bp")
|
|
|
|
|
|
|
|
@bp.get("/main")
|
|
|
|
async def main(request):
|
|
|
|
...
|
|
|
|
|
|
|
|
@bp.websocket("/route")
|
|
|
|
async def test_route(request, ws):
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
@bp.websocket("/route2")
|
|
|
|
async def test_route2(request, ws):
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
@bp.websocket("/route3", name="foobar_3")
|
|
|
|
async def test_route3(request, ws):
|
|
|
|
event.set()
|
|
|
|
|
|
|
|
app.blueprint(bp)
|
|
|
|
|
|
|
|
uri = app.url_for("test_bp.main")
|
|
|
|
assert uri == "/bp/main"
|
|
|
|
|
2021-08-05 20:55:42 +01:00
|
|
|
uri = app.url_for(f"test_bp.{name}")
|
|
|
|
assert uri == expected
|
2021-01-25 00:14:48 +00:00
|
|
|
request, response = SanicTestClient(app).websocket(uri)
|
2020-06-28 12:45:52 +01:00
|
|
|
assert response.opened is True
|
|
|
|
assert event.is_set()
|
|
|
|
|
2021-02-07 09:38:37 +00:00
|
|
|
|
|
|
|
# TODO: add test with a route with multiple hosts
|
|
|
|
# TODO: add test with a route with _host in url_for
|
2021-03-01 13:30:52 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"path,strict,expected",
|
|
|
|
(
|
|
|
|
("/foo", False, "/foo"),
|
|
|
|
("/foo/", False, "/foo"),
|
|
|
|
("/foo", True, "/foo"),
|
|
|
|
("/foo/", True, "/foo/"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
def test_trailing_slash_url_for(app, path, strict, expected):
|
|
|
|
@app.route(path, strict_slashes=strict)
|
|
|
|
def handler(*_):
|
|
|
|
...
|
|
|
|
|
|
|
|
url = app.url_for("handler")
|
|
|
|
assert url == expected
|