Coverage for tests/test_tutorial/test_websockets/test_tutorial003_py39.py: 100%
34 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1import pytest 1fabcde
2from fastapi import FastAPI 1fabcde
3from fastapi.testclient import TestClient 1fabcde
5from ...utils import needs_py39 1fabcde
8@pytest.fixture(name="app") 1fabcde
9def get_app(): 1fabcde
10 from docs_src.websockets.tutorial003_py39 import app 1abcde
12 return app 1abcde
15@pytest.fixture(name="html") 1fabcde
16def get_html(): 1fabcde
17 from docs_src.websockets.tutorial003_py39 import html 1abcde
19 return html 1abcde
22@pytest.fixture(name="client") 1fabcde
23def get_client(app: FastAPI): 1fabcde
24 client = TestClient(app) 1abcde
26 return client 1abcde
29@needs_py39 1fabcde
30def test_get(client: TestClient, html: str): 1fabcde
31 response = client.get("/") 1lmnop
32 assert response.text == html 1lmnop
35@needs_py39 1fabcde
36def test_websocket_handle_disconnection(client: TestClient): 1fabcde
37 with client.websocket_connect("/ws/1234") as connection, client.websocket_connect( 1ghijk
38 "/ws/5678"
39 ) as connection_two:
40 connection.send_text("Hello from 1234") 1ghijk
41 data1 = connection.receive_text() 1ghijk
42 assert data1 == "You wrote: Hello from 1234" 1ghijk
43 data2 = connection_two.receive_text() 1ghijk
44 client1_says = "Client #1234 says: Hello from 1234" 1ghijk
45 assert data2 == client1_says 1ghijk
46 data1 = connection.receive_text() 1ghijk
47 assert data1 == client1_says 1ghijk
48 connection_two.close() 1ghijk
49 data1 = connection.receive_text() 1ghijk
50 assert data1 == "Client #5678 left the chat" 1ghijk