Browse Source

Add more tests

pull/12145/head
Synrom 7 months ago
parent
commit
a2b2114e97
  1. 55
      tests/test_ignore_trailing_slash.py

55
tests/test_ignore_trailing_slash.py

@ -1,4 +1,5 @@
from fastapi import APIRouter, FastAPI, WebSocket
from fastapi import APIRouter, FastAPI, Request, WebSocket
from fastapi.responses import JSONResponse
from fastapi.testclient import TestClient
app = FastAPI(ignore_trailing_slash=True)
@ -53,6 +54,44 @@ def route_endpoint_with_slash():
return {"msg": "Routing Example 2"}
@router.websocket("/websocket")
async def router_websocket_endpoint(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Websocket")
await websocket.close()
@router.websocket("/websocket2/")
async def router_websocket_endpoint_with_slash(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Websocket 2")
await websocket.close()
@router.websocket_route("/websocket_route")
async def router_websocket_route_endpoint(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Websocket route")
await websocket.close()
@router.websocket_route("/websocket_route_2/")
async def router_websocket_route_endpoint_with_slash(websocket: WebSocket):
await websocket.accept()
await websocket.send_text("Websocket route 2")
await websocket.close()
@router.route("/starlette_route", ["get"])
async def starlette_route_endpoint(request: Request):
return JSONResponse({"msg": "Starlette Route"})
@router.route("/starlette_route_2/", ["get"])
async def starlette_route_endpoint_with_slash(request: Request):
return JSONResponse({"msg": "Starlette Route 2"})
app.include_router(router, prefix="/router")
client = TestClient(app)
@ -85,3 +124,17 @@ def test_ignoring_trailing_routing():
response = client.get("router/example2", follow_redirects=False)
assert response.status_code == 200
assert response.json()["msg"] == "Routing Example 2"
response = client.get("router/starlette_route/", follow_redirects=False)
assert response.status_code == 200
assert response.json()["msg"] == "Starlette Route"
response = client.get("router/starlette_route_2", follow_redirects=False)
assert response.status_code == 200
assert response.json()["msg"] == "Starlette Route 2"
with client.websocket_connect("router/websocket/") as websocket:
assert websocket.receive_text() == "Websocket"
with client.websocket_connect("router/websocket2") as websocket:
assert websocket.receive_text() == "Websocket 2"
with client.websocket_connect("router/websocket_route/") as websocket:
assert websocket.receive_text() == "Websocket route"
with client.websocket_connect("router/websocket_route_2/") as websocket:
assert websocket.receive_text() == "Websocket route 2"

Loading…
Cancel
Save