From 507042581794a48b71b5a4b5e0f1ddcde6783c00 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 7 Oct 2020 15:51:11 -0700 Subject: [PATCH] Fix handling of nested custom page wildcard paths, closes #996 --- datasette/app.py | 2 +- tests/test_custom_pages.py | 11 ++++++++--- tests/test_templates/pages/topic_{topic}.html | 1 + tests/test_templates/pages/topic_{topic}/{slug}.html | 1 + 4 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 tests/test_templates/pages/topic_{topic}.html create mode 100644 tests/test_templates/pages/topic_{topic}/{slug}.html 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