ApplyAppearance: keep a path StripPrefix already made relative

Behind http.StripPrefix the request path is relative to the base path,
so for base path /feeds the page /feeds was cut to / again. A path that
no longer matches RequestURI's is now taken as already stripped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
terminarandClaude Opus 5.5 committed 2026-10-31 23:32:24 +01:00
1 parent 0f9b40df9f
commit 28db4f707f
3 files changed
+51 -6

No files matched your search

+25 -1
View File
@@ -143,7 +143,7 @@ func TestApplyAppearance(t *testing.T) {
t.Errorf("AppearanceBack = %q, want %q", h.AppearanceBack, "/tasks?f=1")
}
// Behind http.StripPrefix the path is already relative to BasePath.
// Without StripPrefix only a whole leading BasePath segment is cut.
for path, want := range map[string]string{"/release-notes": "/release-notes", "/r": "/", "/r/x": "/x"} {
h := PageHeader{BasePath: "/r"}
h.ApplyAppearance(httptest.NewRequest(http.MethodGet, path, nil))
@@ -153,6 +153,30 @@ func TestApplyAppearance(t *testing.T) {
}
}
// Behind http.StripPrefix the path is already relative to BasePath and
// must stay as it is, even when it looks like the base path itself.
func TestApplyAppearanceBehindStripPrefix(t *testing.T) {
cases := []struct{ base, request, want string }{
{"/feeds", "/feeds/feeds", "/feeds"},
{"/feeds", "/feeds/feeds/x?a=1", "/feeds/x?a=1"},
{"/feeds", "/feeds/", "/"},
{"/feeds", "/feeds/items", "/items"},
{"/r", "/r/release-notes", "/release-notes"},
}
for _, c := range cases {
var got string
h := http.StripPrefix(c.base, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
hdr := PageHeader{BasePath: c.base}
hdr.ApplyAppearance(r)
got = hdr.AppearanceBack
}))
h.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, c.request, nil))
if got != c.want {
t.Errorf("base %s, request %s: AppearanceBack = %q, want %q", c.base, c.request, got, c.want)
}
}
}
func TestAppearanceHandler(t *testing.T) {
h := AppearanceHandler("/ui")
post := func(form url.Values) *httptest.ResponseRecorder {