diff --git a/datasette/app.py b/datasette/app.py index 20aae7d0..353e4a10 100644 --- a/datasette/app.py +++ b/datasette/app.py @@ -1204,7 +1204,7 @@ def route_pattern_from_filepath(filepath): re_bits.append("(?P<{}>[^/]*)".format(bit[1:-1])) else: re_bits.append(re.escape(bit)) - return re.compile("".join(re_bits)) + return re.compile("^" + "".join(re_bits) + "$") class NotFoundExplicit(NotFound): diff --git a/tests/test_custom_pages.py b/tests/test_custom_pages.py index f6180c16..071b055b 100644 --- a/tests/test_custom_pages.py +++ b/tests/test_custom_pages.py @@ -64,10 +64,15 @@ def test_redirect2(custom_pages_client): assert "/example" == response.headers["Location"] -def test_custom_route_pattern(custom_pages_client): - response = custom_pages_client.get("/route_Sally") +@pytest.mark.parametrize("path,expected", [ + ("/route_Sally", "

Hello from Sally

"), + ("/topic_python", "Topic page for python"), + ("/topic_python/info", "Slug: info, Topic: python"), +]) +def test_custom_route_pattern(custom_pages_client, path, expected): + response = custom_pages_client.get(path) assert response.status == 200 - assert response.text.strip() == "

Hello from Sally

" + assert response.text.strip() == expected def test_custom_route_pattern_404(custom_pages_client): diff --git a/tests/test_templates/pages/topic_{topic}.html b/tests/test_templates/pages/topic_{topic}.html new file mode 100644 index 00000000..f07b6b07 --- /dev/null +++ b/tests/test_templates/pages/topic_{topic}.html @@ -0,0 +1 @@ +Topic page for {{ topic }} \ No newline at end of file diff --git a/tests/test_templates/pages/topic_{topic}/{slug}.html b/tests/test_templates/pages/topic_{topic}/{slug}.html new file mode 100644 index 00000000..cbe5344f --- /dev/null +++ b/tests/test_templates/pages/topic_{topic}/{slug}.html @@ -0,0 +1 @@ +Slug: {{ slug }}, Topic: {{ topic }} \ No newline at end of file