Coverage for test\test_request_objects.py: 100%

1 import pytest

2 from nexios import get_application, NexiosApp

3 from nexios.http import Request, Response

4 from nexios.testing import Client

5 import json

6 from http import cookies

7 import anyio

8

9

10@pytest.fixture

11 async def test_client():

12 app = get_application()

13 async with Client(app) as client:

14 yield client, app

15

16

17 async def test_request_properties(test_client):

18 client, app = test_client

19

20 @app.get("/test-properties")

21 async def handler(req: Request, res: Response):

22 return res.json(

23 {

24 "method": req.method,

25 "path": req.path,

26 "client_host": req.client.host if req.client else None,

27 "client_port": req.client.port if req.client else None,

28 }

29 )

30

31 response = await client.get("/test-properties")

32 data = response.json()

33 assert data["method"] == "GET"

34 assert data["path"] == "/test-properties"

35 assert data["client_host"] is not None

36 assert data["client_port"] is not None

37

38

39 async def test_query_params(test_client):

40 client, app = test_client

41

42 @app.get("/test-query")

43 async def handler(req: Request, res: Response):

44 return res.json(

45 {"name": req.query_params.get("name"), "age": req.query_params.get("age")}

46 )

47

48 response = await client.get("/test-query?name=test&age=25")

49 data = response.json()

50 assert data["name"] == "test"

51 assert data["age"] == "25"

52

53

54 async def test_path_params(test_client):

55 client, app = test_client

56

57 @app.get("/test-path/{user_id}")

58 async def handler(req: Request, res: Response):

59 return res.json({"user_id": req.path_params["user_id"]})

60

61 response = await client.get("/test-path/123")

62 data = response.json()

63 assert data["user_id"] == "123"

64

65

66 async def test_headers(test_client):

67 client, app = test_client

68

69 @app.get("/test-headers")

70 async def handler(req: Request, res: Response):

71 return res.json(

72 {

73 "content_type": req.headers.get("content-type"),

74 "user_agent": req.headers.get("user-agent"),

75 }

76 )

77

78 response = await client.get(

79 "/test-headers",

80 headers={"content-type": "application/json", "user-agent": "test-agent"},

81 )

82 data = response.json()

83 assert data["content_type"] == "application/json"

84 assert data["user_agent"] == "test-agent"

85

86

87 async def test_cookies(test_client):

88 client, app = test_client

89

90 @app.get("/test-cookies")

91 async def handler(req: Request, res: Response):

92 return res.json(

93 {"session": req.cookies.get("session"), "user": req.cookies.get("user")}

94 )

95

96 response = await client.get(

97 "/test-cookies", cookies={"session": "abc123", "user": "test"}

98 )

99 data = response.json()

100 assert data["session"] == "abc123"

101 assert data["user"] == "test"

102

103

104 async def test_state(test_client):

105 client, app = test_client

106

107 @app.get("/test-state")

108 async def handler(req: Request, res: Response):

109 req.state.test_value = "state_value"

110 return res.text(req.state.test_value)

111

112 response = await client.get("/test-state")

113 assert response.text == "state_value"

114

115

116 async def test_json_body(test_client):

117 client, app = test_client

118

119 @app.post("/test-json-body")

120 async def handler(req: Request, res: Response):

121 data = await req.json

122 return res.json(data)

123

124 test_data = {"key": "value"}

125 response = await client.post("/test-json-body", json=test_data)

126 assert response.json() == test_data

127

128

129 async def test_text_body(test_client):

130 client, app = test_client

131

132 @app.post("/test-text-body")

133 async def handler(req: Request, res: Response):

134 text = await req.text

135 return res.text(text)

136

137 test_text = "plain text body"

138 response = await client.post("/test-text-body", content=test_text)

139 assert response.text == test_text

140

141

142 async def test_form_data(test_client):

143 client, app = test_client

144

145 @app.post("/test-form-data")

146 async def handler(req: Request, res: Response):

147 form_data = await req.form_data

148 return res.json(

149 {"field1": form_data.get("field1"), "field2": form_data.get("field2")}

150 )

151

152 form_data = {"field1": "value1", "field2": "value2"}

153 response = await client.post("/test-form-data", data=form_data)

154 data = response.json()

155 assert data["field1"] == "value1"

156 assert data["field2"] == "value2"

157

158

159 async def test_user_property(test_client):

160 client, app = test_client

161

162 @app.get("/test-user")

163 async def handler(req: Request, res: Response):

164 req.user = "testuser"

165 return res.text(req.user)

166

167 response = await client.get("/test-user")

168 assert response.text == "testuser"

169

170

171 async def test_valid_method(test_client):

172 client, app = test_client

173

174 @app.route("/test-valid", methods=["GET", "POST"])

175 async def handler(req: Request, res: Response):

176 return res.text(str(req.valid()))

177

178 # Test valid method

179 response = await client.get("/test-valid")

180 assert response.text == "True"

181

182 # Test invalid method

183 response = await client.delete("/test-valid")

184 assert response.status_code == 405