Coverage for tests/test_tutorial/test_websockets/test_tutorial003.py: 100%

19 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-08-08 03:53 +0000

1from fastapi.testclient import TestClient 1abcde

2 

3from docs_src.websockets.tutorial003 import app, html 1abcde

4 

5client = TestClient(app) 1abcde

6 

7 

8def test_get(): 1abcde

9 response = client.get("/") 1abcde

10 assert response.text == html 1abcde

11 

12 

13def test_websocket_handle_disconnection(): 1abcde

14 with client.websocket_connect("/ws/1234") as connection, client.websocket_connect( 1abcde

15 "/ws/5678" 

16 ) as connection_two: 

17 connection.send_text("Hello from 1234") 1abcde

18 data1 = connection.receive_text() 1abcde

19 assert data1 == "You wrote: Hello from 1234" 1abcde

20 data2 = connection_two.receive_text() 1abcde

21 client1_says = "Client #1234 says: Hello from 1234" 1abcde

22 assert data2 == client1_says 1abcde

23 data1 = connection.receive_text() 1abcde

24 assert data1 == client1_says 1abcde

25 connection_two.close() 1abcde

26 data1 = connection.receive_text() 1abcde

27 assert data1 == "Client #5678 left the chat" 1abcde