Sync fastapi docs from b5ca1324 on 2025-12-07
Issue Manager / issue-manager (push) Has been cancelled
Build Docs / changes (push) Has been cancelled
Build Docs / langs (push) Has been cancelled
Build Docs / build-docs (push) Has been cancelled
Build Docs / docs-all-green (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi) (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi-slim) (push) Has been cancelled
Test Redistribute / test-redistribute-alls-green (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.13) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.13) (push) Has been cancelled
Test / test (pydantic-v2, 3.14) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
FastAPI People Contributors / job (push) Has been cancelled
FastAPI People Sponsors / job (push) Has been cancelled
Update Topic Repos / topic-repos (push) Has been cancelled
FastAPI People / job (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
Issue Manager / issue-manager (push) Has been cancelled
Build Docs / changes (push) Has been cancelled
Build Docs / langs (push) Has been cancelled
Build Docs / build-docs (push) Has been cancelled
Build Docs / docs-all-green (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi) (push) Has been cancelled
Test Redistribute / test-redistribute (fastapi-slim) (push) Has been cancelled
Test Redistribute / test-redistribute-alls-green (push) Has been cancelled
Test / lint (push) Has been cancelled
Test / test (pydantic-v1, 3.10) (push) Has been cancelled
Test / test (pydantic-v1, 3.11) (push) Has been cancelled
Test / test (pydantic-v1, 3.13) (push) Has been cancelled
Test / test (pydantic-v1, 3.8) (push) Has been cancelled
Test / test (pydantic-v1, 3.9) (push) Has been cancelled
Test / test (pydantic-v2, 3.10) (push) Has been cancelled
Test / test (pydantic-v2, 3.11) (push) Has been cancelled
Test / test (pydantic-v2, 3.12) (push) Has been cancelled
Test / test (pydantic-v2, 3.13) (push) Has been cancelled
Test / test (pydantic-v2, 3.14) (push) Has been cancelled
Test / test (pydantic-v2, 3.8) (push) Has been cancelled
Test / test (pydantic-v2, 3.9) (push) Has been cancelled
Test / coverage-combine (push) Has been cancelled
Test / check (push) Has been cancelled
Label Approved / label-approved (push) Has been cancelled
FastAPI People Contributors / job (push) Has been cancelled
FastAPI People Sponsors / job (push) Has been cancelled
Update Topic Repos / topic-repos (push) Has been cancelled
FastAPI People / job (push) Has been cancelled
Test / test (pydantic-v1, 3.12) (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
from fastapi.websockets import WebSocketDisconnect
|
||||
|
||||
from docs_src.websockets.tutorial001 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_main():
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200, response.text
|
||||
assert b"<!DOCTYPE html>" in response.content
|
||||
|
||||
|
||||
def test_websocket():
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/ws") as websocket:
|
||||
message = "Message one"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
assert data == f"Message text was: {message}"
|
||||
message = "Message two"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
assert data == f"Message text was: {message}"
|
||||
@@ -0,0 +1,107 @@
|
||||
import importlib
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
from fastapi.websockets import WebSocketDisconnect
|
||||
|
||||
from ...utils import needs_py39, needs_py310
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
name="app",
|
||||
params=[
|
||||
"tutorial002",
|
||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||
"tutorial002_an",
|
||||
pytest.param("tutorial002_an_py39", marks=needs_py39),
|
||||
pytest.param("tutorial002_an_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_app(request: pytest.FixtureRequest):
|
||||
mod = importlib.import_module(f"docs_src.websockets.{request.param}")
|
||||
|
||||
return mod.app
|
||||
|
||||
|
||||
def test_main(app: FastAPI):
|
||||
client = TestClient(app)
|
||||
response = client.get("/")
|
||||
assert response.status_code == 200, response.text
|
||||
assert b"<!DOCTYPE html>" in response.content
|
||||
|
||||
|
||||
def test_websocket_with_cookie(app: FastAPI):
|
||||
client = TestClient(app, cookies={"session": "fakesession"})
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/foo/ws") as websocket:
|
||||
message = "Message one"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
assert data == "Session cookie or query token value is: fakesession"
|
||||
data = websocket.receive_text()
|
||||
assert data == f"Message text was: {message}, for item ID: foo"
|
||||
message = "Message two"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
assert data == "Session cookie or query token value is: fakesession"
|
||||
data = websocket.receive_text()
|
||||
assert data == f"Message text was: {message}, for item ID: foo"
|
||||
|
||||
|
||||
def test_websocket_with_header(app: FastAPI):
|
||||
client = TestClient(app)
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/bar/ws?token=some-token") as websocket:
|
||||
message = "Message one"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
assert data == "Session cookie or query token value is: some-token"
|
||||
data = websocket.receive_text()
|
||||
assert data == f"Message text was: {message}, for item ID: bar"
|
||||
message = "Message two"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
assert data == "Session cookie or query token value is: some-token"
|
||||
data = websocket.receive_text()
|
||||
assert data == f"Message text was: {message}, for item ID: bar"
|
||||
|
||||
|
||||
def test_websocket_with_header_and_query(app: FastAPI):
|
||||
client = TestClient(app)
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/2/ws?q=3&token=some-token") as websocket:
|
||||
message = "Message one"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
assert data == "Session cookie or query token value is: some-token"
|
||||
data = websocket.receive_text()
|
||||
assert data == "Query parameter q is: 3"
|
||||
data = websocket.receive_text()
|
||||
assert data == f"Message text was: {message}, for item ID: 2"
|
||||
message = "Message two"
|
||||
websocket.send_text(message)
|
||||
data = websocket.receive_text()
|
||||
assert data == "Session cookie or query token value is: some-token"
|
||||
data = websocket.receive_text()
|
||||
assert data == "Query parameter q is: 3"
|
||||
data = websocket.receive_text()
|
||||
assert data == f"Message text was: {message}, for item ID: 2"
|
||||
|
||||
|
||||
def test_websocket_no_credentials(app: FastAPI):
|
||||
client = TestClient(app)
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/foo/ws"):
|
||||
pytest.fail(
|
||||
"did not raise WebSocketDisconnect on __enter__"
|
||||
) # pragma: no cover
|
||||
|
||||
|
||||
def test_websocket_invalid_data(app: FastAPI):
|
||||
client = TestClient(app)
|
||||
with pytest.raises(WebSocketDisconnect):
|
||||
with client.websocket_connect("/items/foo/ws?q=bar&token=some-token"):
|
||||
pytest.fail(
|
||||
"did not raise WebSocketDisconnect on __enter__"
|
||||
) # pragma: no cover
|
||||
@@ -0,0 +1,27 @@
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.websockets.tutorial003 import app, html
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_get():
|
||||
response = client.get("/")
|
||||
assert response.text == html
|
||||
|
||||
|
||||
def test_websocket_handle_disconnection():
|
||||
with client.websocket_connect("/ws/1234") as connection, client.websocket_connect(
|
||||
"/ws/5678"
|
||||
) as connection_two:
|
||||
connection.send_text("Hello from 1234")
|
||||
data1 = connection.receive_text()
|
||||
assert data1 == "You wrote: Hello from 1234"
|
||||
data2 = connection_two.receive_text()
|
||||
client1_says = "Client #1234 says: Hello from 1234"
|
||||
assert data2 == client1_says
|
||||
data1 = connection.receive_text()
|
||||
assert data1 == client1_says
|
||||
connection_two.close()
|
||||
data1 = connection.receive_text()
|
||||
assert data1 == "Client #5678 left the chat"
|
||||
@@ -0,0 +1,50 @@
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from ...utils import needs_py39
|
||||
|
||||
|
||||
@pytest.fixture(name="app")
|
||||
def get_app():
|
||||
from docs_src.websockets.tutorial003_py39 import app
|
||||
|
||||
return app
|
||||
|
||||
|
||||
@pytest.fixture(name="html")
|
||||
def get_html():
|
||||
from docs_src.websockets.tutorial003_py39 import html
|
||||
|
||||
return html
|
||||
|
||||
|
||||
@pytest.fixture(name="client")
|
||||
def get_client(app: FastAPI):
|
||||
client = TestClient(app)
|
||||
|
||||
return client
|
||||
|
||||
|
||||
@needs_py39
|
||||
def test_get(client: TestClient, html: str):
|
||||
response = client.get("/")
|
||||
assert response.text == html
|
||||
|
||||
|
||||
@needs_py39
|
||||
def test_websocket_handle_disconnection(client: TestClient):
|
||||
with client.websocket_connect("/ws/1234") as connection, client.websocket_connect(
|
||||
"/ws/5678"
|
||||
) as connection_two:
|
||||
connection.send_text("Hello from 1234")
|
||||
data1 = connection.receive_text()
|
||||
assert data1 == "You wrote: Hello from 1234"
|
||||
data2 = connection_two.receive_text()
|
||||
client1_says = "Client #1234 says: Hello from 1234"
|
||||
assert data2 == client1_says
|
||||
data1 = connection.receive_text()
|
||||
assert data1 == client1_says
|
||||
connection_two.close()
|
||||
data1 = connection.receive_text()
|
||||
assert data1 == "Client #5678 left the chat"
|
||||
Reference in New Issue
Block a user