Пример #1
0
def test_routes():
    assert app.routes == [
        Route("/func", endpoint=func_homepage, methods=["GET"]),
        Route("/async", endpoint=async_homepage, methods=["GET"]),
        Route("/class", endpoint=Homepage),
        Mount(
            "/users",
            app=Router(routes=[
                Route("/", endpoint=all_users_page),
                Route("/{username}", endpoint=user_page),
            ]),
        ),
        Host(
            "{subdomain}.example.org",
            app=Router(routes=[Route("/", endpoint=custom_subdomain)]),
        ),
        Route("/500", endpoint=runtime_error, methods=["GET"]),
        WebSocketRoute("/ws", endpoint=websocket_endpoint),
    ]
Пример #2
0
def test_mount_at_root():
    mounted = Router([Mount("/", ok, name="users")])
    client = TestClient(mounted)
    assert client.get("/").status_code == 200


def users_api(request):
    return JSONResponse({"users": [{"username": "******"}]})


mixed_hosts_app = Router(routes=[
    Host(
        "www.example.org",
        app=Router([
            Route("/", homepage, name="homepage"),
            Route("/users", users, name="users"),
        ]),
    ),
    Host(
        "api.example.org",
        name="api",
        app=Router([Route("/users", users_api, name="users")]),
    ),
])


def test_host_routing():
    client = TestClient(mixed_hosts_app, base_url="https://api.example.org/")

    response = client.get("/users")
Пример #3
0
from starlette.routing import Host, Mount, Route, WebSocketRoute

from . import legacy, middleware, resources, settings, views
from .i18n.routing import LocaleRoute
from .reload import hotreload
from .sitemap import sitemap

routes = [
    Host(
        "florimondmanca.com",
        app=legacy.DomainRedirect("florimond.dev"),
        name="legacy:dot_com",
    ),
    Host(
        "blog.florimondmanca.com",
        app=legacy.DomainRedirect("blog.florimond.dev"),
        name="legacy:blog_dot_com",
    ),
    Host(
        "blog.florimond.dev",
        app=legacy.DomainRedirect("florimond.dev", root_path="/blog"),
        name="legacy:blog_dot_dev",
    ),
    Route("/error/", views.error),
    Route("/blog/", views.legacy_blog_home, name="legacy:blog_home"),
    Mount(settings.STATIC_ROOT, resources.static, name="static"),
    # These files need to be exposed at the root, not '/static/'.
    Route("/favicon.ico", resources.static, name="favicon"),
    Route("/robots.txt", resources.static, name="robots"),
    Route("/sitemap.xml", sitemap, name="sitemap"),
    Route(
Пример #4
0
}

middleware = [
    Middleware(TrustedHostMiddleware,
               allowed_hosts=["testserver", "*.example.org"])
]

app = Starlette(
    routes=[
        Route("/func", endpoint=func_homepage),
        Route("/async", endpoint=async_homepage),
        Route("/class", endpoint=Homepage),
        Route("/500", endpoint=runtime_error),
        WebSocketRoute("/ws", endpoint=websocket_endpoint),
        Mount("/users", app=users),
        Host("{subdomain}.example.org", app=subdomain),
    ],
    exception_handlers=exception_handlers,
    middleware=middleware,
)


@pytest.fixture
def client(test_client_factory):
    with test_client_factory(app) as client:
        yield client


def test_url_path_for():
    assert app.url_path_for("func_homepage") == "/func"