|
6 | 6 | from mcp.server.fastmcp.resources import ResourceTemplate |
7 | 7 |
|
8 | 8 |
|
9 | | -class TestUrlParameterDecoding: |
10 | | - """Test URL parameter decoding in resource templates.""" |
| 9 | +def test_template_matches_decodes_space(): |
| 10 | + """Test that %20 is decoded to space.""" |
11 | 11 |
|
12 | | - def test_template_matches_decodes_space(self): |
13 | | - """Test that %20 is decoded to space.""" |
| 12 | + def search(query: str) -> str: # pragma: no cover |
| 13 | + return f"Results for: {query}" |
14 | 14 |
|
15 | | - def search(query: str) -> str: # pragma: no cover |
16 | | - return f"Results for: {query}" |
| 15 | + template = ResourceTemplate.from_function( |
| 16 | + fn=search, |
| 17 | + uri_template="search://{query}", |
| 18 | + name="search", |
| 19 | + ) |
17 | 20 |
|
18 | | - template = ResourceTemplate.from_function( |
19 | | - fn=search, |
20 | | - uri_template="search://{query}", |
21 | | - name="search", |
22 | | - ) |
| 21 | + params = template.matches("search://hello%20world") |
| 22 | + assert params is not None |
| 23 | + assert params["query"] == "hello world" |
23 | 24 |
|
24 | | - params = template.matches("search://hello%20world") |
25 | | - assert params is not None |
26 | | - assert params["query"] == "hello world" |
27 | 25 |
|
28 | | - def test_template_matches_decodes_accented_characters(self): |
29 | | - """Test that %C3%A9 is decoded to e with accent.""" |
| 26 | +def test_template_matches_decodes_accented_characters(): |
| 27 | + """Test that %C3%A9 is decoded to e with accent.""" |
30 | 28 |
|
31 | | - def search(query: str) -> str: # pragma: no cover |
32 | | - return f"Results for: {query}" |
| 29 | + def search(query: str) -> str: # pragma: no cover |
| 30 | + return f"Results for: {query}" |
33 | 31 |
|
34 | | - template = ResourceTemplate.from_function( |
35 | | - fn=search, |
36 | | - uri_template="search://{query}", |
37 | | - name="search", |
38 | | - ) |
| 32 | + template = ResourceTemplate.from_function( |
| 33 | + fn=search, |
| 34 | + uri_template="search://{query}", |
| 35 | + name="search", |
| 36 | + ) |
39 | 37 |
|
40 | | - params = template.matches("search://caf%C3%A9") |
41 | | - assert params is not None |
42 | | - assert params["query"] == "café" |
| 38 | + params = template.matches("search://caf%C3%A9") |
| 39 | + assert params is not None |
| 40 | + assert params["query"] == "café" |
43 | 41 |
|
44 | | - def test_template_matches_decodes_complex_phrase(self): |
45 | | - """Test complex French phrase from the original issue.""" |
46 | 42 |
|
47 | | - def search(query: str) -> str: # pragma: no cover |
48 | | - return f"Results for: {query}" |
| 43 | +def test_template_matches_decodes_complex_phrase(): |
| 44 | + """Test complex French phrase from the original issue.""" |
49 | 45 |
|
50 | | - template = ResourceTemplate.from_function( |
51 | | - fn=search, |
52 | | - uri_template="search://{query}", |
53 | | - name="search", |
54 | | - ) |
| 46 | + def search(query: str) -> str: # pragma: no cover |
| 47 | + return f"Results for: {query}" |
55 | 48 |
|
56 | | - params = template.matches("search://stick%20correcteur%20teint%C3%A9%20anti-imperfections") |
57 | | - assert params is not None |
58 | | - assert params["query"] == "stick correcteur teinté anti-imperfections" |
| 49 | + template = ResourceTemplate.from_function( |
| 50 | + fn=search, |
| 51 | + uri_template="search://{query}", |
| 52 | + name="search", |
| 53 | + ) |
59 | 54 |
|
60 | | - def test_template_matches_preserves_plus_sign(self): |
61 | | - """Test that plus sign remains as plus (not converted to space). |
| 55 | + params = template.matches("search://stick%20correcteur%20teint%C3%A9%20anti-imperfections") |
| 56 | + assert params is not None |
| 57 | + assert params["query"] == "stick correcteur teinté anti-imperfections" |
62 | 58 |
|
63 | | - In URI encoding, %20 is space. Plus-as-space is only for |
64 | | - application/x-www-form-urlencoded (HTML forms). |
65 | | - """ |
66 | 59 |
|
67 | | - def search(query: str) -> str: # pragma: no cover |
68 | | - return f"Results for: {query}" |
| 60 | +def test_template_matches_preserves_plus_sign(): |
| 61 | + """Test that plus sign remains as plus (not converted to space). |
69 | 62 |
|
70 | | - template = ResourceTemplate.from_function( |
71 | | - fn=search, |
72 | | - uri_template="search://{query}", |
73 | | - name="search", |
74 | | - ) |
| 63 | + In URI encoding, %20 is space. Plus-as-space is only for |
| 64 | + application/x-www-form-urlencoded (HTML forms). |
| 65 | + """ |
75 | 66 |
|
76 | | - params = template.matches("search://hello+world") |
77 | | - assert params is not None |
78 | | - assert params["query"] == "hello+world" |
| 67 | + def search(query: str) -> str: # pragma: no cover |
| 68 | + return f"Results for: {query}" |
| 69 | + |
| 70 | + template = ResourceTemplate.from_function( |
| 71 | + fn=search, |
| 72 | + uri_template="search://{query}", |
| 73 | + name="search", |
| 74 | + ) |
| 75 | + |
| 76 | + params = template.matches("search://hello+world") |
| 77 | + assert params is not None |
| 78 | + assert params["query"] == "hello+world" |
0 commit comments