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
« 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
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
15async def test_route_decorator(async_client):
16 client, app = async_client # Extract client and app
18 @app.get("/get")
19 async def handle_get(req: Request, res: Response):
20 return res.json({"method": req.method})
22 @app.post("/post")
23 async def handle_post(req: Request, res: Response):
24 return res.json({"method": req.method})
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"}
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"}
37async def test_add_route_method(async_client):
38 client, app = async_client # Extract client and app
40 async def handle_get(req: Request, res: Response):
41 return res.json({"method": req.method})
43 async def handle_post(req: Request, res: Response):
44 return res.json({"method": req.method})
46 # Add routes
47 app.add_route(Routes("/get", handle_get, methods=["GET"]))
48 app.add_route(Routes("/post", handle_post, methods=["POST"]))
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"}
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"}
61async def test_routers_no_prefix(async_client):
62 client, app = async_client
64 router = Router()
66 @router.get("/")
67 async def handle_get(req: Request, res: Response):
68 return res.text("i love you")
70 app.mount_router(router)
72 response = await client.get("/")
73 assert response.status_code == 200
76async def test_url_for_with_params(async_client):
77 client, app = async_client
79 @app.get("/get/name/{age}", name="name")
80 async def get(req: Request, res: Response): ...
82 url = app.url_for("name", age=0)
83 assert url == "/get/name/0"
86async def test_url_for_with_request(async_client):
87 client, app = async_client
89 @app.get("/get/name/{age}", name="name")
90 async def get(req: Request, res: Response):
92 return res.json({"name": req.url_for("name", age=0)})
94 response = await client.get("/get/name/0")
95 data = response.json()
96 assert data["name"] == "/get/name/0"
99async def test_url_parameters(async_client):
100 client, app = async_client
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})
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"}
113async def test_route_prefixes(async_client):
114 client, app = async_client
116 # Create a router with a prefix
117 router = Router(prefix="/api")
119 @router.get("/users")
120 async def handle_users(req: Request, res: Response):
121 return res.json({"message": "Users Route"})
123 @router.post("/posts")
124 async def handle_posts(req: Request, res: Response):
125 return res.json({"message": "Posts Route"})
127 # Mount the router under the app
128 app.mount_router(router)
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"}
135 posts_response = await client.post("/api/posts")
136 assert posts_response.status_code == 200
137 assert posts_response.json() == {"message": "Posts Route"}
140async def test_invalid_routes(async_client):
141 client, app = async_client
143 # Define a valid route
144 @app.get("/valid")
145 async def handle_valid(req: Request, res: Response):
146 return res.text("Valid Route")
148 # Test an invalid route
149 invalid_response = await client.get("/invalid")
152async def test_nested_routers(async_client):
153 client, app = async_client
155 # Create a parent router
156 parent_router = Router(prefix="/parent")
158 # Create a child router
159 child_router = Router(prefix="/child")
161 @child_router.get("/nested")
162 async def handle_nested(req: Request, res: Response):
163 return res.text("Nested Router Works")
165 # Mount the child router under the parent router
166 parent_router.mount_router(child_router)
168 # Mount the parent router under the app
169 app.mount_router(parent_router)
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"
177async def test_handler_args(async_client):
178 client, app = async_client
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})
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"}