86 lines
3.2 KiB
Python
86 lines
3.2 KiB
Python
import pytest
|
|
|
|
from api_mock_cli.core.route_matcher import RouteMatcher, RouteMatch
|
|
|
|
|
|
class TestRouteMatcher:
|
|
def test_add_route(self):
|
|
matcher = RouteMatcher()
|
|
pattern = matcher.add_route("https://api.example.com/users/123")
|
|
assert pattern == "/users/<id>"
|
|
|
|
def test_add_multiple_routes(self):
|
|
matcher = RouteMatcher()
|
|
matcher.add_route("https://api.example.com/users/123")
|
|
matcher.add_route("https://api.example.com/posts/456")
|
|
routes = matcher.get_routes()
|
|
assert len(routes) == 2
|
|
|
|
def test_match_exact_path(self):
|
|
matcher = RouteMatcher()
|
|
matcher.add_route("https://api.example.com/users")
|
|
|
|
match = matcher.match("GET", "https://api.example.com/users")
|
|
assert match is not None
|
|
assert match.matched is True
|
|
assert match.route_pattern == "/users"
|
|
|
|
def test_match_with_path_params(self):
|
|
matcher = RouteMatcher()
|
|
matcher.add_route("https://api.example.com/users/123")
|
|
|
|
match = matcher.match("GET", "https://api.example.com/users/456")
|
|
assert match is not None
|
|
assert match.matched is True
|
|
assert match.path_params.get("id") == "456"
|
|
|
|
def test_match_with_uuid(self):
|
|
matcher = RouteMatcher()
|
|
matcher.add_route("https://api.example.com/items/123e4567-e89b-12d3-a456-426614174000")
|
|
|
|
match = matcher.match("GET", "https://api.example.com/items/987fcdeb-51a2-3def-9abc-123456789012")
|
|
assert match is not None
|
|
assert match.route_pattern == "/items/<uuid>"
|
|
|
|
def test_no_match(self):
|
|
matcher = RouteMatcher()
|
|
matcher.add_route("https://api.example.com/users")
|
|
|
|
match = matcher.match("GET", "https://api.example.com/posts")
|
|
assert match is None
|
|
|
|
def test_convert_url_with_numeric_id(self):
|
|
matcher = RouteMatcher()
|
|
pattern, params = matcher._convert_url_to_flask_pattern("https://api.example.com/users/123")
|
|
assert pattern == "/users/<id>"
|
|
assert "id" in params
|
|
|
|
def test_convert_url_with_uuid(self):
|
|
matcher = RouteMatcher()
|
|
pattern, params = matcher._convert_url_to_flask_pattern("https://api.example.com/items/123e4567-e89b-12d3-a456-426614174000")
|
|
assert pattern == "/items/<uuid>"
|
|
assert "uuid" in params
|
|
|
|
def test_convert_url_with_multiple_params(self):
|
|
matcher = RouteMatcher()
|
|
pattern, params = matcher._convert_url_to_flask_pattern("https://api.example.com/orgs/123/repos/456")
|
|
assert "/orgs/<id>/repos/<id>" in pattern
|
|
|
|
def test_extract_path_params(self):
|
|
matcher = RouteMatcher()
|
|
params = matcher._extract_path_params("/users/456", "/users/<id>")
|
|
assert params is not None
|
|
assert params["id"] == "456"
|
|
|
|
def test_extract_path_params_mismatch(self):
|
|
matcher = RouteMatcher()
|
|
params = matcher._extract_path_params("/users/456/posts", "/users/<id>")
|
|
assert params is None
|
|
|
|
def test_duplicate_route_not_added(self):
|
|
matcher = RouteMatcher()
|
|
matcher.add_route("https://api.example.com/users")
|
|
matcher.add_route("https://api.example.com/users")
|
|
routes = matcher.get_routes()
|
|
assert len(routes) == 1
|