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-12-04 08:29 +0000

1import pytest 1gabcdef

2from fastapi import FastAPI 1gabcdef

3from fastapi.testclient import TestClient 1gabcdef

4 

5from ...utils import needs_py39 1gabcdef

6 

7 

8@pytest.fixture(name="app") 1gabcdef

9def get_app(): 1gabcdef

10 from docs_src.websockets.tutorial003_py39 import app 1abcdef

11 

12 return app 1abcdef

13 

14 

15@pytest.fixture(name="html") 1gabcdef

16def get_html(): 1gabcdef

17 from docs_src.websockets.tutorial003_py39 import html 1abcdef

18 

19 return html 1abcdef

20 

21 

22@pytest.fixture(name="client") 1gabcdef

23def get_client(app: FastAPI): 1gabcdef

24 client = TestClient(app) 1abcdef

25 

26 return client 1abcdef

27 

28 

29@needs_py39 1gabcdef

30def test_get(client: TestClient, html: str): 1gabcdef

31 response = client.get("/") 1nopqrs

32 assert response.text == html 1nopqrs

33 

34 

35@needs_py39 1gabcdef

36def test_websocket_handle_disconnection(client: TestClient): 1gabcdef

37 with client.websocket_connect("/ws/1234") as connection, client.websocket_connect( 1hijklm

38 "/ws/5678" 

39 ) as connection_two: 

40 connection.send_text("Hello from 1234") 1hijklm

41 data1 = connection.receive_text() 1hijklm

42 assert data1 == "You wrote: Hello from 1234" 1hijklm

43 data2 = connection_two.receive_text() 1hijklm

44 client1_says = "Client #1234 says: Hello from 1234" 1hijklm

45 assert data2 == client1_says 1hijklm

46 data1 = connection.receive_text() 1hijklm

47 assert data1 == client1_says 1hijklm

48 connection_two.close() 1hijklm

49 data1 = connection.receive_text() 1hijklm

50 assert data1 == "Client #5678 left the chat" 1hijklm