Coverage for tests/test_tutorial/test_websockets/test_tutorial002_an_py39.py: 100%
80 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1import pytest 1eabcd
2from fastapi import FastAPI 1eabcd
3from fastapi.testclient import TestClient 1eabcd
4from fastapi.websockets import WebSocketDisconnect 1eabcd
6from ...utils import needs_py39 1eabcd
9@pytest.fixture(name="app") 1eabcd
10def get_app(): 1eabcd
11 from docs_src.websockets.tutorial002_an_py39 import app 1abcd
13 return app 1abcd
16@needs_py39 1eabcd
17def test_main(app: FastAPI): 1eabcd
18 client = TestClient(app) 1abcd
19 response = client.get("/") 1abcd
20 assert response.status_code == 200, response.text 1abcd
21 assert b"<!DOCTYPE html>" in response.content 1abcd
24@needs_py39 1eabcd
25def test_websocket_with_cookie(app: FastAPI): 1eabcd
26 client = TestClient(app, cookies={"session": "fakesession"}) 1abcd
27 with pytest.raises(WebSocketDisconnect): 1abcd
28 with client.websocket_connect("/items/foo/ws") as websocket: 1abcd
29 message = "Message one" 1abcd
30 websocket.send_text(message) 1abcd
31 data = websocket.receive_text() 1abcd
32 assert data == "Session cookie or query token value is: fakesession" 1abcd
33 data = websocket.receive_text() 1abcd
34 assert data == f"Message text was: {message}, for item ID: foo" 1abcd
35 message = "Message two" 1abcd
36 websocket.send_text(message) 1abcd
37 data = websocket.receive_text() 1abcd
38 assert data == "Session cookie or query token value is: fakesession" 1abcd
39 data = websocket.receive_text() 1abcd
40 assert data == f"Message text was: {message}, for item ID: foo" 1abcd
43@needs_py39 1eabcd
44def test_websocket_with_header(app: FastAPI): 1eabcd
45 client = TestClient(app) 1abcd
46 with pytest.raises(WebSocketDisconnect): 1abcd
47 with client.websocket_connect("/items/bar/ws?token=some-token") as websocket: 1abcd
48 message = "Message one" 1abcd
49 websocket.send_text(message) 1abcd
50 data = websocket.receive_text() 1abcd
51 assert data == "Session cookie or query token value is: some-token" 1abcd
52 data = websocket.receive_text() 1abcd
53 assert data == f"Message text was: {message}, for item ID: bar" 1abcd
54 message = "Message two" 1abcd
55 websocket.send_text(message) 1abcd
56 data = websocket.receive_text() 1abcd
57 assert data == "Session cookie or query token value is: some-token" 1abcd
58 data = websocket.receive_text() 1abcd
59 assert data == f"Message text was: {message}, for item ID: bar" 1abcd
62@needs_py39 1eabcd
63def test_websocket_with_header_and_query(app: FastAPI): 1eabcd
64 client = TestClient(app) 1abcd
65 with pytest.raises(WebSocketDisconnect): 1abcd
66 with client.websocket_connect("/items/2/ws?q=3&token=some-token") as websocket: 1abcd
67 message = "Message one" 1abcd
68 websocket.send_text(message) 1abcd
69 data = websocket.receive_text() 1abcd
70 assert data == "Session cookie or query token value is: some-token" 1abcd
71 data = websocket.receive_text() 1abcd
72 assert data == "Query parameter q is: 3" 1abcd
73 data = websocket.receive_text() 1abcd
74 assert data == f"Message text was: {message}, for item ID: 2" 1abcd
75 message = "Message two" 1abcd
76 websocket.send_text(message) 1abcd
77 data = websocket.receive_text() 1abcd
78 assert data == "Session cookie or query token value is: some-token" 1abcd
79 data = websocket.receive_text() 1abcd
80 assert data == "Query parameter q is: 3" 1abcd
81 data = websocket.receive_text() 1abcd
82 assert data == f"Message text was: {message}, for item ID: 2" 1abcd
85@needs_py39 1eabcd
86def test_websocket_no_credentials(app: FastAPI): 1eabcd
87 client = TestClient(app) 1abcd
88 with pytest.raises(WebSocketDisconnect): 1abcd
89 with client.websocket_connect("/items/foo/ws"): 1abcd
90 pytest.fail( 1a
91 "did not raise WebSocketDisconnect on __enter__"
92 ) # pragma: no cover
95@needs_py39 1eabcd
96def test_websocket_invalid_data(app: FastAPI): 1eabcd
97 client = TestClient(app) 1abcd
98 with pytest.raises(WebSocketDisconnect): 1abcd
99 with client.websocket_connect("/items/foo/ws?q=bar&token=some-token"): 1abcd
100 pytest.fail( 1a
101 "did not raise WebSocketDisconnect on __enter__"
102 ) # pragma: no cover