-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathtest_973_url_decoding.py
More file actions
78 lines (55 loc) · 2.24 KB
/
test_973_url_decoding.py
File metadata and controls
78 lines (55 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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"