ApplyAppearance: cut BasePath only as a whole path segment
Behind http.StripPrefix the request path is already relative, and a blind TrimPrefix turned /release-notes into "elease-notes" for BasePath /r, sending the menu back to the start page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3af449e9f9
commit
148062ec28
2 files changed
+20
-1
No files matched your search
+11
-1
@@ -67,7 +67,17 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) {
|
|||||||
if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) {
|
if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) {
|
||||||
h.Palette = c.Value
|
h.Palette = c.Value
|
||||||
}
|
}
|
||||||
back := url.URL{Path: strings.TrimPrefix(r.URL.Path, h.BasePath), RawQuery: r.URL.RawQuery}
|
// Only a whole leading BasePath segment is cut: behind
|
||||||
|
// http.StripPrefix the path is already relative, and "/r" must not
|
||||||
|
// eat the start of "/release-notes".
|
||||||
|
path := r.URL.Path
|
||||||
|
if h.BasePath != "" && (path == h.BasePath || strings.HasPrefix(path, h.BasePath+"/")) {
|
||||||
|
path = strings.TrimPrefix(path, h.BasePath)
|
||||||
|
}
|
||||||
|
if path == "" {
|
||||||
|
path = "/"
|
||||||
|
}
|
||||||
|
back := url.URL{Path: path, RawQuery: r.URL.RawQuery}
|
||||||
h.AppearanceBack = back.String()
|
h.AppearanceBack = back.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -142,6 +142,15 @@ func TestApplyAppearance(t *testing.T) {
|
|||||||
if h.AppearanceBack != "/tasks?f=1" {
|
if h.AppearanceBack != "/tasks?f=1" {
|
||||||
t.Errorf("AppearanceBack = %q, want %q", h.AppearanceBack, "/tasks?f=1")
|
t.Errorf("AppearanceBack = %q, want %q", h.AppearanceBack, "/tasks?f=1")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Behind http.StripPrefix the path is already relative to BasePath.
|
||||||
|
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))
|
||||||
|
if h.AppearanceBack != want {
|
||||||
|
t.Errorf("BasePath /r, path %s: AppearanceBack = %q, want %q", path, h.AppearanceBack, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAppearanceHandler(t *testing.T) {
|
func TestAppearanceHandler(t *testing.T) {
|
||||||
|
|||||||
Reference in new issue
Block a user