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

80 statements  

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

1import pytest 1deabc

2from fastapi import FastAPI 1deabc

3from fastapi.testclient import TestClient 1deabc

4from fastapi.websockets import WebSocketDisconnect 1deabc

5 

6from ...utils import needs_py310 1deabc

7 

8 

9@pytest.fixture(name="app") 1deabc

10def get_app(): 1deabc

11 from docs_src.websockets.tutorial002_an_py310 import app 1abc

12 

13 return app 1abc

14 

15 

16@needs_py310 1deabc

17def test_main(app: FastAPI): 1deabc

18 client = TestClient(app) 1abc

19 response = client.get("/") 1abc

20 assert response.status_code == 200, response.text 1abc

21 assert b"<!DOCTYPE html>" in response.content 1abc

22 

23 

24@needs_py310 1deabc

25def test_websocket_with_cookie(app: FastAPI): 1deabc

26 client = TestClient(app, cookies={"session": "fakesession"}) 1abc

27 with pytest.raises(WebSocketDisconnect): 1abc

28 with client.websocket_connect("/items/foo/ws") as websocket: 1abc

29 message = "Message one" 1abc

30 websocket.send_text(message) 1abc

31 data = websocket.receive_text() 1abc

32 assert data == "Session cookie or query token value is: fakesession" 1abc

33 data = websocket.receive_text() 1abc

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

35 message = "Message two" 1abc

36 websocket.send_text(message) 1abc

37 data = websocket.receive_text() 1abc

38 assert data == "Session cookie or query token value is: fakesession" 1abc

39 data = websocket.receive_text() 1abc

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

41 

42 

43@needs_py310 1deabc

44def test_websocket_with_header(app: FastAPI): 1deabc

45 client = TestClient(app) 1abc

46 with pytest.raises(WebSocketDisconnect): 1abc

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

48 message = "Message one" 1abc

49 websocket.send_text(message) 1abc

50 data = websocket.receive_text() 1abc

51 assert data == "Session cookie or query token value is: some-token" 1abc

52 data = websocket.receive_text() 1abc

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

54 message = "Message two" 1abc

55 websocket.send_text(message) 1abc

56 data = websocket.receive_text() 1abc

57 assert data == "Session cookie or query token value is: some-token" 1abc

58 data = websocket.receive_text() 1abc

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

60 

61 

62@needs_py310 1deabc

63def test_websocket_with_header_and_query(app: FastAPI): 1deabc

64 client = TestClient(app) 1abc

65 with pytest.raises(WebSocketDisconnect): 1abc

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

67 message = "Message one" 1abc

68 websocket.send_text(message) 1abc

69 data = websocket.receive_text() 1abc

70 assert data == "Session cookie or query token value is: some-token" 1abc

71 data = websocket.receive_text() 1abc

72 assert data == "Query parameter q is: 3" 1abc

73 data = websocket.receive_text() 1abc

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

75 message = "Message two" 1abc

76 websocket.send_text(message) 1abc

77 data = websocket.receive_text() 1abc

78 assert data == "Session cookie or query token value is: some-token" 1abc

79 data = websocket.receive_text() 1abc

80 assert data == "Query parameter q is: 3" 1abc

81 data = websocket.receive_text() 1abc

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

83 

84 

85@needs_py310 1deabc

86def test_websocket_no_credentials(app: FastAPI): 1deabc

87 client = TestClient(app) 1abc

88 with pytest.raises(WebSocketDisconnect): 1abc

89 with client.websocket_connect("/items/foo/ws"): 1abc

90 pytest.fail( 

91 "did not raise WebSocketDisconnect on __enter__" 

92 ) # pragma: no cover 

93 

94 

95@needs_py310 1deabc

96def test_websocket_invalid_data(app: FastAPI): 1deabc

97 client = TestClient(app) 1abc

98 with pytest.raises(WebSocketDisconnect): 1abc

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

100 pytest.fail( 

101 "did not raise WebSocketDisconnect on __enter__" 

102 ) # pragma: no cover