Coverage for test\test_routing.py: 99%

112 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.testing import Client 

4from nexios.http import Request, Response 

5from nexios.routing import Router, Routes 

6 

7 

8@pytest.fixture 

9async def async_client(): 

10 app = get_application() # Fresh app instance for each test 

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

12 yield c, app 

13 

14 

15async def test_route_decorator(async_client): 

16 client, app = async_client # Extract client and app 

17 

18 @app.get("/get") 

19 async def handle_get(req: Request, res: Response): 

20 return res.json({"method": req.method}) 

21 

22 @app.post("/post") 

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

24 return res.json({"method": req.method}) 

25 

26 # Test GET request 

27 get_response = await client.get("/get") 

28 assert get_response.status_code == 200 

29 assert get_response.json() == {"method": "GET"} 

30 

31 # Test POST request 

32 post_response = await client.post("/post") 

33 assert post_response.status_code == 200 

34 assert post_response.json() == {"method": "POST"} 

35 

36 

37async def test_add_route_method(async_client): 

38 client, app = async_client # Extract client and app 

39 

40 async def handle_get(req: Request, res: Response): 

41 return res.json({"method": req.method}) 

42 

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

44 return res.json({"method": req.method}) 

45 

46 # Add routes 

47 app.add_route(Routes("/get", handle_get, methods=["GET"])) 

48 app.add_route(Routes("/post", handle_post, methods=["POST"])) 

49 

50 # Test GET request 

51 get_response = await client.get("/get") 

52 assert get_response.status_code == 200 

53 assert get_response.json() == {"method": "GET"} 

54 

55 # Test POST request 

56 post_response = await client.post("/post") 

57 assert post_response.status_code == 200 

58 assert post_response.json() == {"method": "POST"} 

59 

60 

61async def test_routers_no_prefix(async_client): 

62 client, app = async_client 

63 

64 router = Router() 

65 

66 @router.get("/") 

67 async def handle_get(req: Request, res: Response): 

68 return res.text("i love you") 

69 

70 app.mount_router(router) 

71 

72 response = await client.get("/") 

73 assert response.status_code == 200 

74 

75 

76async def test_url_for_with_params(async_client): 

77 client, app = async_client 

78 

79 @app.get("/get/name/{age}", name="name") 

80 async def get(req: Request, res: Response): ... 

81 

82 url = app.url_for("name", age=0) 

83 assert url == "/get/name/0" 

84 

85 

86async def test_url_for_with_request(async_client): 

87 client, app = async_client 

88 

89 @app.get("/get/name/{age}", name="name") 

90 async def get(req: Request, res: Response): 

91 

92 return res.json({"name": req.url_for("name", age=0)}) 

93 

94 response = await client.get("/get/name/0") 

95 data = response.json() 

96 assert data["name"] == "/get/name/0" 

97 

98 

99async def test_url_parameters(async_client): 

100 client, app = async_client 

101 

102 @app.get("/user/{user_id}") 

103 async def handle_user(req: Request, res: Response): 

104 user_id = req.path_params["user_id"] 

105 return res.json({"user_id": user_id}) 

106 

107 # Test with a specific user ID 

108 response = await client.get("/user/123") 

109 assert response.status_code == 200 

110 assert response.json() == {"user_id": "123"} 

111 

112 

113async def test_route_prefixes(async_client): 

114 client, app = async_client 

115 

116 # Create a router with a prefix 

117 router = Router(prefix="/api") 

118 

119 @router.get("/users") 

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

121 return res.json({"message": "Users Route"}) 

122 

123 @router.post("/posts") 

124 async def handle_posts(req: Request, res: Response): 

125 return res.json({"message": "Posts Route"}) 

126 

127 # Mount the router under the app 

128 app.mount_router(router) 

129 

130 # Test the prefixed routes 

131 users_response = await client.get("/api/users") 

132 assert users_response.status_code == 200 

133 assert users_response.json() == {"message": "Users Route"} 

134 

135 posts_response = await client.post("/api/posts") 

136 assert posts_response.status_code == 200 

137 assert posts_response.json() == {"message": "Posts Route"} 

138 

139 

140async def test_invalid_routes(async_client): 

141 client, app = async_client 

142 

143 # Define a valid route 

144 @app.get("/valid") 

145 async def handle_valid(req: Request, res: Response): 

146 return res.text("Valid Route") 

147 

148 # Test an invalid route 

149 invalid_response = await client.get("/invalid") 

150 

151 

152async def test_nested_routers(async_client): 

153 client, app = async_client 

154 

155 # Create a parent router 

156 parent_router = Router(prefix="/parent") 

157 

158 # Create a child router 

159 child_router = Router(prefix="/child") 

160 

161 @child_router.get("/nested") 

162 async def handle_nested(req: Request, res: Response): 

163 return res.text("Nested Router Works") 

164 

165 # Mount the child router under the parent router 

166 parent_router.mount_router(child_router) 

167 

168 # Mount the parent router under the app 

169 app.mount_router(parent_router) 

170 

171 # Test the nested route 

172 response = await client.get("/parent/child/nested") 

173 assert response.status_code == 200 

174 assert response.text == "Nested Router Works" 

175 

176 

177async def test_handler_args(async_client): 

178 client, app = async_client 

179 

180 @app.get("/user/{user_id}") 

181 async def handle_user(req: Request, res: Response, user_id: str): 

182 return res.json({"user_id": user_id}) 

183 

184 # Test with a specific user ID 

185 response = await client.get("/user/123") 

186 assert response.status_code == 200 

187 assert response.json() == {"user_id": "123"}