fix: URL-decode parameters extracted from resource templates (#1864)
This commit is contained in:
parent
edf0950a6a
commit
0622babbd9
1 changed files with 85 additions and 2 deletions
78
tests/issues/test_973_url_decoding.py
Normal file
78
tests/issues/test_973_url_decoding.py
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
"""Test that URL-encoded parameters are decoded in resource templates.
|
||||
|
||||
Regression test for https://github.com/modelcontextprotocol/python-sdk/issues/973
|
||||
"""
|
||||
|
||||
from mcp.server.fastmcp.resources import ResourceTemplate
|
||||
|
||||
|
||||
def test_template_matches_decodes_space():
|
||||
"""Test that %20 is decoded to space."""
|
||||
|
||||
def search(query: str) -> str: # pragma: no cover
|
||||
return f"Results for: {query}"
|
||||
|
||||
template = ResourceTemplate.from_function(
|
||||
fn=search,
|
||||
uri_template="search://{query}",
|
||||
name="search",
|
||||
)
|
||||
|
||||
params = template.matches("search://hello%20world")
|
||||
assert params is not None
|
||||
assert params["query"] == "hello world"
|
||||
|
||||
|
||||
def test_template_matches_decodes_accented_characters():
|
||||
"""Test that %C3%A9 is decoded to e with accent."""
|
||||
|
||||
def search(query: str) -> str: # pragma: no cover
|
||||
return f"Results for: {query}"
|
||||
|
||||
template = ResourceTemplate.from_function(
|
||||
fn=search,
|
||||
uri_template="search://{query}",
|
||||
name="search",
|
||||
)
|
||||
|
||||
params = template.matches("search://caf%C3%A9")
|
||||
assert params is not None
|
||||
assert params["query"] == "café"
|
||||
|
||||
|
||||
def test_template_matches_decodes_complex_phrase():
|
||||
"""Test complex French phrase from the original issue."""
|
||||
|
||||
def search(query: str) -> str: # pragma: no cover
|
||||
return f"Results for: {query}"
|
||||
|
||||
template = ResourceTemplate.from_function(
|
||||
fn=search,
|
||||
uri_template="search://{query}",
|
||||
name="search",
|
||||
)
|
||||
|
||||
params = template.matches("search://stick%20correcteur%20teint%C3%A9%20anti-imperfections")
|
||||
assert params is not None
|
||||
assert params["query"] == "stick correcteur teinté anti-imperfections"
|
||||
|
||||
|
||||
def test_template_matches_preserves_plus_sign():
|
||||
"""Test that plus sign remains as plus (not converted to space).
|
||||
|
||||
In URI encoding, %20 is space. Plus-as-space is only for
|
||||
application/x-www-form-urlencoded (HTML forms).
|
||||
"""
|
||||
|
||||
def search(query: str) -> str: # pragma: no cover
|
||||
return f"Results for: {query}"
|
||||
|
||||
template = ResourceTemplate.from_function(
|
||||
fn=search,
|
||||
uri_template="search://{query}",
|
||||
name="search",
|
||||
)
|
||||
|
||||
params = template.matches("search://hello+world")
|
||||
assert params is not None
|
||||
assert params["query"] == "hello+world"
|
||||
Loading…
Reference in a new issue