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-01-13 13:38 +0000

1import pytest 1eabcd

2from fastapi import FastAPI 1eabcd

3from fastapi.testclient import TestClient 1eabcd

4 

5from ...utils import needs_py39 1eabcd

6 

7 

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

9def get_app(): 1eabcd

10 from docs_src.websockets.tutorial003_py39 import app 1abcd

11 

12 return app 1abcd

13 

14 

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

16def get_html(): 1eabcd

17 from docs_src.websockets.tutorial003_py39 import html 1abcd

18 

19 return html 1abcd

20 

21 

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

23def get_client(app: FastAPI): 1eabcd

24 client = TestClient(app) 1abcd

25 

26 return client 1abcd

27 

28 

29@needs_py39 1eabcd

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

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

32 assert response.text == html 1jklm

33 

34 

35@needs_py39 1eabcd

36def test_websocket_handle_disconnection(client: TestClient): 1eabcd

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

38 "/ws/5678" 

39 ) as connection_two: 

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

41 data1 = connection.receive_text() 1fghi

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

43 data2 = connection_two.receive_text() 1fghi

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

45 assert data2 == client1_says 1fghi

46 data1 = connection.receive_text() 1fghi

47 assert data1 == client1_says 1fghi

48 connection_two.close() 1fghi

49 data1 = connection.receive_text() 1fghi

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