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

20 statements  

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

1import pytest 1abcde

2from fastapi.testclient import TestClient 1abcde

3from fastapi.websockets import WebSocketDisconnect 1abcde

4 

5from docs_src.websockets.tutorial001 import app 1abcde

6 

7client = TestClient(app) 1abcde

8 

9 

10def test_main(): 1abcde

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

12 assert response.status_code == 200, response.text 1abcde

13 assert b"<!DOCTYPE html>" in response.content 1abcde

14 

15 

16def test_websocket(): 1abcde

17 with pytest.raises(WebSocketDisconnect): 1abcde

18 with client.websocket_connect("/ws") as websocket: 1abcde

19 message = "Message one" 1abcde

20 websocket.send_text(message) 1abcde

21 data = websocket.receive_text() 1abcde

22 assert data == f"Message text was: {message}" 1abcde

23 message = "Message two" 1abcde

24 websocket.send_text(message) 1abcde

25 data = websocket.receive_text() 1abcde

26 assert data == f"Message text was: {message}" 1abcde