Some checks failed
CI / test (3.10) (push) Has been cancelled
CI / test (3.11) (push) Has been cancelled
CI / test (3.12) (push) Has been cancelled
CI / test (3.9) (push) Has been cancelled
CI / lint (push) Has been cancelled
CI / type-check (push) Has been cancelled
CI / build (push) Has been cancelled
84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
import pytest
|
|
from src.core.router import Router
|
|
from src.models.endpoint import Endpoint
|
|
|
|
|
|
class TestRouter:
|
|
def setup_method(self):
|
|
self.router = Router()
|
|
|
|
def test_add_route_simple(self):
|
|
endpoint = Endpoint(
|
|
path="/api/users",
|
|
method="GET",
|
|
response={"status_code": 200, "body": {"users": []}}
|
|
)
|
|
self.router.add_route(endpoint)
|
|
routes = self.router.get_all_routes()
|
|
assert len(routes) == 1
|
|
assert routes[0].path == "/api/users"
|
|
|
|
def test_match_simple_path(self):
|
|
endpoint = Endpoint(
|
|
path="/api/hello",
|
|
method="GET",
|
|
response={"status_code": 200, "body": {"message": "Hello"}}
|
|
)
|
|
self.router.add_route(endpoint)
|
|
result = self.router.match("/api/hello", "GET")
|
|
assert result is not None
|
|
endpoint_result, params = result
|
|
assert endpoint_result.path == "/api/hello"
|
|
assert params == {}
|
|
|
|
def test_match_with_path_params(self):
|
|
endpoint = Endpoint(
|
|
path="/api/users/{id}",
|
|
method="GET",
|
|
response={"status_code": 200, "body": {}}
|
|
)
|
|
self.router.add_route(endpoint)
|
|
result = self.router.match("/api/users/123", "GET")
|
|
assert result is not None
|
|
endpoint_result, params = result
|
|
assert params["id"] == "123"
|
|
|
|
def test_match_method_mismatch(self):
|
|
endpoint = Endpoint(
|
|
path="/api/users",
|
|
method="GET",
|
|
response={"status_code": 200, "body": {}}
|
|
)
|
|
self.router.add_route(endpoint)
|
|
result = self.router.match("/api/users", "POST")
|
|
assert result is None
|
|
|
|
def test_match_nonexistent_path(self):
|
|
result = self.router.match("/api/nonexistent", "GET")
|
|
assert result is None
|
|
|
|
def test_clear_routes(self):
|
|
endpoint = Endpoint(
|
|
path="/api/test",
|
|
method="GET",
|
|
response={"status_code": 200, "body": {}}
|
|
)
|
|
self.router.add_route(endpoint)
|
|
self.router.clear()
|
|
routes = self.router.get_all_routes()
|
|
assert len(routes) == 0
|
|
|
|
def test_multiple_routes(self):
|
|
endpoints = [
|
|
Endpoint(path="/api/users", method="GET", response={}),
|
|
Endpoint(path="/api/users/{id}", method="GET", response={}),
|
|
Endpoint(path="/api/posts", method="POST", response={}),
|
|
]
|
|
for ep in endpoints:
|
|
self.router.add_route(ep)
|
|
assert len(self.router.get_all_routes()) == 3
|
|
assert self.router.match("/api/users", "GET") is not None
|
|
assert self.router.match("/api/users/123", "GET") is not None
|
|
assert self.router.match("/api/posts", "POST") is not None
|
|
assert self.router.match("/api/posts", "GET") is None
|