From b583d375f25347499f10c9eb1ea52f62e0f0f2ce Mon Sep 17 00:00:00 2001 From: 7000pctAUTO Date: Thu, 29 Jan 2026 13:53:59 +0000 Subject: [PATCH] Initial upload: API Mock CLI v0.1.0 --- tests/test_matcher.py | 113 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/test_matcher.py diff --git a/tests/test_matcher.py b/tests/test_matcher.py new file mode 100644 index 0000000..14fdde2 --- /dev/null +++ b/tests/test_matcher.py @@ -0,0 +1,113 @@ +import pytest +from src.core.matcher import Matcher +from src.models.endpoint import Endpoint + + +class TestMatcher: + def setup_method(self): + self.matcher = Matcher() + + def test_compile_pattern_simple(self): + pattern = self.matcher.compile_pattern("/api/users") + assert pattern.match("/api/users") + assert not pattern.match("/api/users/123") + + def test_compile_pattern_with_params(self): + pattern = self.matcher.compile_pattern("/api/users/{id}") + match = pattern.match("/api/users/123") + assert match is not None + assert match.group("id") == "123" + + def test_compile_pattern_multiple_params(self): + pattern = self.matcher.compile_pattern("/api/posts/{post_id}/comments/{comment_id}") + match = pattern.match("/api/posts/abc/comments/def") + assert match is not None + assert match.group("post_id") == "abc" + assert match.group("comment_id") == "def" + + def test_add_endpoint(self): + endpoint = Endpoint( + path="/api/test", + method="GET", + response={"status_code": 200} + ) + self.matcher.add_endpoint(endpoint) + endpoints = self.matcher.get_endpoints("GET") + assert len(endpoints) == 1 + + def test_add_endpoint_invalid_method(self): + endpoint = Endpoint( + path="/api/test", + method="INVALID", + response={"status_code": 200} + ) + with pytest.raises(ValueError, match="Invalid HTTP method"): + self.matcher.add_endpoint(endpoint) + + def test_match_simple(self): + endpoint = Endpoint( + path="/api/hello", + method="GET", + response={"status_code": 200} + ) + self.matcher.add_endpoint(endpoint) + result = self.matcher.match("/api/hello", "GET") + assert result is not None + ep, params = result + assert ep.path == "/api/hello" + + def test_match_with_params(self): + endpoint = Endpoint( + path="/api/items/{item_id}", + method="GET", + response={"status_code": 200} + ) + self.matcher.add_endpoint(endpoint) + result = self.matcher.match("/api/items/456", "GET") + assert result is not None + ep, params = result + assert params["item_id"] == "456" + + def test_match_method_case_insensitive(self): + endpoint = Endpoint( + path="/api/test", + method="POST", + response={"status_code": 201} + ) + self.matcher.add_endpoint(endpoint) + assert self.matcher.match("/api/test", "post") is not None + assert self.matcher.match("/api/test", "Post") is not None + + def test_get_endpoints_by_method(self): + endpoints = [ + Endpoint(path="/api/a", method="GET", response={}), + Endpoint(path="/api/b", method="POST", response={}), + Endpoint(path="/api/c", method="GET", response={}), + ] + for ep in endpoints: + self.matcher.add_endpoint(ep) + get_endpoints = self.matcher.get_endpoints("GET") + post_endpoints = self.matcher.get_endpoints("POST") + assert len(get_endpoints) == 2 + assert len(post_endpoints) == 1 + + def test_clear_by_method(self): + endpoints = [ + Endpoint(path="/api/a", method="GET", response={}), + Endpoint(path="/api/b", method="POST", response={}), + ] + for ep in endpoints: + self.matcher.add_endpoint(ep) + self.matcher.clear("GET") + assert len(self.matcher.get_endpoints("GET")) == 0 + assert len(self.matcher.get_endpoints("POST")) == 1 + + def test_clear_all(self): + endpoints = [ + Endpoint(path="/api/a", method="GET", response={}), + Endpoint(path="/api/b", method="POST", response={}), + ] + for ep in endpoints: + self.matcher.add_endpoint(ep) + self.matcher.clear() + assert len(self.matcher.get_endpoints()) == 0