mirror of
https://github.com/fastapi/fastapi.git
synced 2026-09-14 13:36:21 +08:00
0ac9b3ee5c
* ✨ Re-export main features used from Starlette to simplify developer's code * ♻️ Refactor Starlette exports * ♻️ Refactor tutorial examples to use re-exported utils from Starlette * 📝 Add examples for all middlewares * 📝 Add new docs for middlewares * 📝 Add examples for custom responses * 📝 Extend docs for custom responses * 📝 Update docs and add notes explaining re-exports from Starlette everywhere * 🍱 Update screenshot for HTTP status * 🔧 Update MkDocs config with new content * ♻️ Refactor tests to use re-exported utils from Starlette * ✨ Re-export WebSocketDisconnect from Starlette for tests * ✅ Add extra tests for extra re-exported middleware * ✅ Add tests for re-exported responses from Starlette * ✨ Add docs about mounting WSGI apps * ➕ Add Flask as a dependency to test WSGIMiddleware * ✅ Test WSGIMiddleware example
32 lines
763 B
Python
32 lines
763 B
Python
from fastapi import FastAPI
|
|
from fastapi.testclient import TestClient
|
|
from fastapi.websockets import WebSocket
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
@app.get("/")
|
|
async def read_main():
|
|
return {"msg": "Hello World"}
|
|
|
|
|
|
@app.websocket_route("/ws")
|
|
async def websocket(websocket: WebSocket):
|
|
await websocket.accept()
|
|
await websocket.send_json({"msg": "Hello WebSocket"})
|
|
await websocket.close()
|
|
|
|
|
|
def test_read_main():
|
|
client = TestClient(app)
|
|
response = client.get("/")
|
|
assert response.status_code == 200
|
|
assert response.json() == {"msg": "Hello World"}
|
|
|
|
|
|
def test_websocket():
|
|
client = TestClient(app)
|
|
with client.websocket_connect("/ws") as websocket:
|
|
data = websocket.receive_json()
|
|
assert data == {"msg": "Hello WebSocket"}
|