Skip to content

Commit 7891fd9

Browse files
committed
test: close coverage gaps in lenient-query branch and test handlers
Adds two no-match cases for the lenient-query code path in UriTemplate.match(): path regex failing when query vars are present, and ; explode name mismatch in the path portion before a {?...} expression. Adds a passing case to test_resource_security_default_rejects_traversal so the handler body executes (the test previously only sent rejected URIs, leaving the handler uncovered). Replaces the _make helper's unreachable return with raise NotImplementedError since those tests only exercise matches().
1 parent aed579c commit 7891fd9

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

tests/server/mcpserver/resources/test_resource_template.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
def _make(uri_template: str, security: ResourceSecurity = DEFAULT_RESOURCE_SECURITY) -> ResourceTemplate:
1717
def handler(**kwargs: Any) -> str:
18-
return "ok"
18+
raise NotImplementedError # these tests only exercise matches()
1919

2020
return ResourceTemplate.from_function(fn=handler, uri_template=uri_template, security=security)
2121

tests/server/mcpserver/test_server.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ def get_item(name: str) -> str:
197197
return f"item:{name}"
198198

199199
async with Client(mcp) as client:
200+
# Safe value passes through to the handler
201+
r = await client.read_resource("data://items/widget")
202+
assert isinstance(r.contents[0], TextResourceContents)
203+
assert r.contents[0].text == "item:widget"
204+
200205
# ".." as a path component is rejected by default policy
201206
with pytest.raises(MCPError, match="Unknown resource"):
202207
await client.read_resource("data://items/..")

tests/shared/test_uri_template.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ def test_match(template: str, uri: str, expected: dict[str, str | list[str]]):
475475
# ; explode: wrong parameter name in any segment rejects the match
476476
("item{;keys*}", "item;admin=true"),
477477
("item{;keys*}", "item;keys=a;admin=true"),
478+
# Lenient-query branch: path portion fails to match
479+
("api/{name}{?q}", "wrong/path?q=x"),
480+
# Lenient-query branch: ; explode name mismatch in path portion
481+
("item{;keys*}{?q}", "item;wrong=x?q=1"),
478482
],
479483
)
480484
def test_match_no_match(template: str, uri: str):

0 commit comments

Comments
 (0)