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

69 statements  

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

1import pytest 1bacde

2from fastapi.testclient import TestClient 1bacde

3from fastapi.websockets import WebSocketDisconnect 1bacde

4 

5from docs_src.websockets.tutorial002_an import app 1bacde

6 

7 

8def test_main(): 1bacde

9 client = TestClient(app) 1bacde

10 response = client.get("/") 1bacde

11 assert response.status_code == 200, response.text 1bacde

12 assert b"<!DOCTYPE html>" in response.content 1bacde

13 

14 

15def test_websocket_with_cookie(): 1bacde

16 client = TestClient(app, cookies={"session": "fakesession"}) 1bacde

17 with pytest.raises(WebSocketDisconnect): 1bacde

18 with client.websocket_connect("/items/foo/ws") as websocket: 1bacde

19 message = "Message one" 1bacde

20 websocket.send_text(message) 1bacde

21 data = websocket.receive_text() 1bacde

22 assert data == "Session cookie or query token value is: fakesession" 1bacde

23 data = websocket.receive_text() 1bacde

24 assert data == f"Message text was: {message}, for item ID: foo" 1bacde

25 message = "Message two" 1bacde

26 websocket.send_text(message) 1bacde

27 data = websocket.receive_text() 1bacde

28 assert data == "Session cookie or query token value is: fakesession" 1bacde

29 data = websocket.receive_text() 1bacde

30 assert data == f"Message text was: {message}, for item ID: foo" 1bacde

31 

32 

33def test_websocket_with_header(): 1bacde

34 client = TestClient(app) 1bacde

35 with pytest.raises(WebSocketDisconnect): 1bacde

36 with client.websocket_connect("/items/bar/ws?token=some-token") as websocket: 1bacde

37 message = "Message one" 1bacde

38 websocket.send_text(message) 1bacde

39 data = websocket.receive_text() 1bacde

40 assert data == "Session cookie or query token value is: some-token" 1bacde

41 data = websocket.receive_text() 1bacde

42 assert data == f"Message text was: {message}, for item ID: bar" 1bacde

43 message = "Message two" 1bacde

44 websocket.send_text(message) 1bacde

45 data = websocket.receive_text() 1bacde

46 assert data == "Session cookie or query token value is: some-token" 1bacde

47 data = websocket.receive_text() 1bacde

48 assert data == f"Message text was: {message}, for item ID: bar" 1bacde

49 

50 

51def test_websocket_with_header_and_query(): 1bacde

52 client = TestClient(app) 1bacde

53 with pytest.raises(WebSocketDisconnect): 1bacde

54 with client.websocket_connect("/items/2/ws?q=3&token=some-token") as websocket: 1bacde

55 message = "Message one" 1bacde

56 websocket.send_text(message) 1bacde

57 data = websocket.receive_text() 1bacde

58 assert data == "Session cookie or query token value is: some-token" 1bacde

59 data = websocket.receive_text() 1bacde

60 assert data == "Query parameter q is: 3" 1bacde

61 data = websocket.receive_text() 1bacde

62 assert data == f"Message text was: {message}, for item ID: 2" 1bacde

63 message = "Message two" 1bacde

64 websocket.send_text(message) 1bacde

65 data = websocket.receive_text() 1bacde

66 assert data == "Session cookie or query token value is: some-token" 1bacde

67 data = websocket.receive_text() 1bacde

68 assert data == "Query parameter q is: 3" 1bacde

69 data = websocket.receive_text() 1bacde

70 assert data == f"Message text was: {message}, for item ID: 2" 1bacde

71 

72 

73def test_websocket_no_credentials(): 1bacde

74 client = TestClient(app) 1bacde

75 with pytest.raises(WebSocketDisconnect): 1bacde

76 with client.websocket_connect("/items/foo/ws"): 1bacde

77 pytest.fail( 1a

78 "did not raise WebSocketDisconnect on __enter__" 

79 ) # pragma: no cover 

80 

81 

82def test_websocket_invalid_data(): 1bacde

83 client = TestClient(app) 1bacde

84 with pytest.raises(WebSocketDisconnect): 1bacde

85 with client.websocket_connect("/items/foo/ws?q=bar&token=some-token"): 1bacde

86 pytest.fail( 1a

87 "did not raise WebSocketDisconnect on __enter__" 

88 ) # pragma: no cover