Coverage for test\test_response.py: 100%

85 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-21 20:31 +0100

1from nexios import get_application, NexiosApp 

2import pytest 

3from nexios.http import Request, Response 

4from nexios.testing import Client 

5import datetime as dt 

6import time 

7from pathlib import Path 

8 

9app: NexiosApp = get_application() 

10 

11 

12@app.get("/response/text") 

13async def send_text_response(req: Request, res: Response): 

14 return res.text("Hello from nexios") 

15 

16 

17@app.get("/response/bytes") 

18async def send_byes_response(req: Request, res: Response): 

19 return res.resp(b"XXXX", content_type="image/png") 

20 

21 

22@app.get("/response/json-none") 

23async def send_json_none_response(req: Request, res: Response): 

24 return res.json(None) # type:ignore 

25 

26 

27@app.get("/response/redirect") 

28async def send_redirect_response(req: Request, res: Response): 

29 return res.redirect("http://google.com") 

30 

31 

32import anyio 

33 

34 

35@app.get("/response/stream") 

36async def send_streaming_response(req: Request, res: Response): 

37 

38 async def numbers(minimum: int, maximum: int): 

39 # async with lock: 

40 for i in range(minimum, maximum + 1): 

41 yield str(f"{i}, ") 

42 

43 await anyio.sleep(0) 

44 

45 generator = numbers(1, 5) 

46 return res.stream(generator) # type: ignore 

47 

48 

49@app.get("/response/headers") 

50async def send_header_response(req: Request, res: Response): 

51 headers = {"x-header-1": "123", "x-header-2": "456"} 

52 return res.json(None, headers=headers) # type:ignore 

53 

54 

55@app.get("/response/files") 

56async def send_file_response(req: Request, res: Response): 

57 base_dir = Path(__file__).resolve().parent 

58 file_path = base_dir / "static" / "example.txt" 

59 res.file( 

60 file_path, 

61 content_disposition_type="attachment", 

62 ) 

63 

64 

65@app.get("/response/cookies") 

66async def send_cookie_response(req: Request, res: Response): 

67 res.set_cookie( 

68 "mycookie", 

69 "myvalue", 

70 max_age=10, 

71 expires=10, 

72 path="/", 

73 domain="localhost", 

74 secure=True, 

75 httponly=True, 

76 samesite="none", 

77 ) 

78 res.text("cookie set") 

79 

80 

81@pytest.fixture(autouse=True) 

82async def async_client(): 

83 async with Client(app, log_requests=True) as c: 

84 yield c 

85 

86 

87async def test_text_response(async_client: Client): 

88 response = await async_client.get("/response/text") 

89 assert response.text == "Hello from nexios" 

90 assert response.status_code == 200 

91 

92 

93async def test_byte_response(async_client: Client): 

94 response = await async_client.get("/response/bytes") 

95 assert response.content == b"XXXX" 

96 assert response.status_code == 200 

97 

98 

99async def test_json_none_response(async_client: Client): 

100 response = await async_client.get("/response/json-none") 

101 assert response.json() is None 

102 assert response.status_code == 200 

103 

104 

105async def test_redirect_response(async_client: Client): 

106 response = await async_client.get("/response/redirect") 

107 assert response.url == "http://google.com" 

108 

109 

110async def test_streaming_response(async_client: Client): 

111 response = await async_client.get("/response/stream") 

112 print(response) 

113 assert response.text == "1, 2, 3, 4, 5, " 

114 

115 

116async def test_response_with_header(async_client: Client): 

117 response = await async_client.get("/response/headers") 

118 assert response.headers["x-header-1"] == "123" 

119 assert response.headers["x-header-2"] == "456" 

120 

121 

122async def test_file_response(async_client: Client): 

123 response = await async_client.get("/response/files") 

124 assert response.status_code == 200 

125 expected_disposition = 'attachment; filename="example.txt"' 

126 assert response.headers["content-disposition"] == expected_disposition 

127 assert "content-length" in response.headers 

128 

129 

130async def test_file_response_range(async_client: Client): 

131 response = await async_client.get( 

132 "/response/files", headers={"Range": "bytes=12-19"} 

133 ) 

134 assert response.status_code == 206 

135 assert response.headers["content-length"] == "8" 

136 

137 

138async def test_set_cookies(async_client: Client, monkeypatch: pytest.MonkeyPatch): 

139 mocked_now = dt.datetime(2037, 1, 22, 12, 0, 0, tzinfo=dt.timezone.utc) 

140 monkeypatch.setattr(time, "time", lambda: mocked_now.timestamp()) 

141 response = await async_client.get("/response/cookies") 

142 assert response.text == "cookie set" 

143 assert ( 

144 response.headers["set-cookie"] 

145 == "mycookie=myvalue; Domain=localhost; expires=Thu, 22 Jan 2037 12:00:10 GMT; " 

146 "HttpOnly; Max-Age=10; Path=/; SameSite=none; Secure" 

147 )