package layout import ( "net/http/httptest" "testing" ) func TestFlashRoundTrip(t *testing.T) { u := FlashURL("/services?x=1", FlashOK, "service a started") var h PageHeader h.ApplyFlash(httptest.NewRequest("GET", u, nil)) if h.Flash != "service a started" || h.FlashKind != FlashOK { t.Fatalf("ApplyFlash(%q) = %q/%q", u, h.Flash, h.FlashKind) } if got := WithoutFlash(u); got != "/services?x=1" { t.Errorf("WithoutFlash = %q", got) } if WithoutFlash("/a?b=c") != "/a?b=c" || WithoutFlash("/a") != "/a" { t.Error("WithoutFlash changed a URL without flash") } h.ApplyFlash(httptest.NewRequest("GET", "/?flash=x&flashKind=bogus", nil)) if h.FlashKind != FlashError { t.Errorf("unknown kind = %q", h.FlashKind) } h.ApplyFlash(httptest.NewRequest("GET", "/", nil)) if h.Flash != "" || h.FlashKind != "" { t.Errorf("no flash = %q/%q", h.Flash, h.FlashKind) } } func TestRedirectFlash(t *testing.T) { rec := httptest.NewRecorder() RedirectFlash(rec, httptest.NewRequest("POST", "/x", nil), "/ui/repos", FlashInfo, "nothing to do") if rec.Code != 303 || rec.Header().Get("Location") != "/ui/repos?flash=nothing+to+do&flashKind=info" { t.Fatalf("%d %q", rec.Code, rec.Header().Get("Location")) } } func TestApplyAppearanceDropsFlash(t *testing.T) { h := PageHeader{BasePath: "/ui"} h.ApplyAppearance(httptest.NewRequest("GET", "/ui/repos?flash=done&flashKind=ok&page=2", nil)) if h.AppearanceBack != "/repos?page=2" { t.Fatalf("AppearanceBack = %q", h.AppearanceBack) } }