From 192151a71394834b3f8c0550804a4fbceff73b3c Mon Sep 17 00:00:00 2001 From: Synrom Date: Sun, 8 Sep 2024 13:24:44 +0200 Subject: [PATCH] Add more tests --- tests/test_ignore_trailing_slash.py | 41 +++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/test_ignore_trailing_slash.py b/tests/test_ignore_trailing_slash.py index 10830077c..5038c6f7b 100644 --- a/tests/test_ignore_trailing_slash.py +++ b/tests/test_ignore_trailing_slash.py @@ -92,7 +92,35 @@ async def starlette_route_endpoint_with_slash(request: Request): return JSONResponse({"msg": "Starlette Route 2"}) +router_ignore = APIRouter(ignore_trailing_slash=True) + + +@router_ignore.route("/example", ["get"]) +async def router_ignore_example(request: Request): + return JSONResponse({"msg": "Router Ignore"}) + + +@router_ignore.route("/example2/", ["get"]) +async def router_ignore_example_with_slash(request: Request): + return JSONResponse({"msg": "Router Ignore 2"}) + + +@router_ignore.websocket_route("/websocket") +async def router_ignore_websocket(websocket: WebSocket): + await websocket.accept() + await websocket.send_text("Router Ignore Websocket") + await websocket.close() + + +@router_ignore.websocket_route("/websocket2/") +async def router_ignore_websocket_with_slash(websocket: WebSocket): + await websocket.accept() + await websocket.send_text("Router Ignore Websocket 2") + await websocket.close() + + app.include_router(router, prefix="/router") +app.include_router(router_ignore, prefix="/router_ignore") client = TestClient(app) @@ -138,3 +166,16 @@ def test_ignoring_trailing_routing(): assert websocket.receive_text() == "Websocket route" with client.websocket_connect("router/websocket_route_2/") as websocket: assert websocket.receive_text() == "Websocket route 2" + + +def test_add_router_with_ignore_flag(): + response = client.get("/router_ignore/example/", follow_redirects=False) + assert response.status_code == 200 + assert response.json()["msg"] == "Router Ignore" + response = client.get("/router_ignore/example2", follow_redirects=False) + assert response.status_code == 200 + assert response.json()["msg"] == "Router Ignore 2" + with client.websocket_connect("/router_ignore/websocket/") as websocket: + assert websocket.receive_text() == "Router Ignore Websocket" + with client.websocket_connect("/router_ignore/websocket2") as websocket: + assert websocket.receive_text() == "Router Ignore Websocket 2"