Initial upload: API Mock CLI v0.1.0
Some checks failed
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
CI / test (3.10) (push) Has been cancelled
Some checks failed
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
CI / test (3.10) (push) Has been cancelled
This commit is contained in:
113
tests/test_matcher.py
Normal file
113
tests/test_matcher.py
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user