Coverage for test\test_application.py: 100%

1 from nexios import get_application, NexiosApp

2 from nexios.routing import Routes, Router

3 from nexios.views import APIView

4 import pytest

5 from nexios.http import Request, Response

6 from nexios.testing import Client

7

8 app: NexiosApp = get_application()

9 router = Router("/mounted")

10

11

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

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

14

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

16

17

18 class 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")

25 async 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}")

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

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

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

34

35

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

37 app.mount_router(router=router)

38

39

40@pytest.fixture

41 async def async_client():

42 async with Client(app) as c:

43 yield c

44

45

46 async 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

52 async 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

58 async 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

64 async 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

70 async 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

76 async def test_405(async_client: Client):

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

78 assert response.status_code == 405