Coverage for test\test_application.py: 100%

51 statements  

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

1from nexios import get_application, NexiosApp 

2from nexios.routing import Routes, Router 

3from nexios.views import APIView 

4import pytest 

5from nexios.http import Request, Response 

6from nexios.testing import Client 

7 

8app: NexiosApp = get_application() 

9router = Router("/mounted") 

10 

11 

12@app.get("/func/home") 

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

14 

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

16 

17 

18class ClassBasedHandler(APIView): 

19 

20 async def get(self, req: Request, res: Response): 

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

22 

23 

24@router.get("/check") 

25async def mounted_route_handler(req: Request, res: Response): 

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

27 

28 

29@router.get("/hello/{name}") 

30@app.get("/hello/{name}") 

31async def route_prams(req: Request, res: Response): 

32 name = req.path_params.name # type: ignore 

33 return res.text(f"hello, {name}") 

34 

35 

36app.add_route(ClassBasedHandler.as_route("/class/home")) 

37app.mount_router(router=router) 

38 

39 

40@pytest.fixture 

41async def async_client(): 

42 async with Client(app) as c: 

43 yield c 

44 

45 

46async def test_func_route(async_client: Client): 

47 response = await async_client.get("/func/home") 

48 assert response.status_code == 200 

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

50 

51 

52async def test_class_route(async_client: Client): 

53 response = await async_client.get("/class/home") 

54 assert response.status_code == 200 

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

56 

57 

58async def test_mounted_router(async_client: Client): 

59 response = await async_client.get("/mounted/check") 

60 assert response.status_code == 200 

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

62 

63 

64async def test_route_path_params(async_client: Client): 

65 response = await async_client.get("/hello/dunamis") 

66 assert response.status_code == 200 

67 assert response.text == "hello, dunamis" 

68 

69 

70async def test_mounted_route_path_params(async_client: Client): 

71 response = await async_client.get("/mounted/hello/dunamis") 

72 assert response.status_code == 200 

73 assert response.text == "hello, dunamis" 

74 

75 

76async def test_405(async_client: Client): 

77 response = await async_client.post("/func/home") 

78 assert response.status_code == 405