httpsec package and flash helpers
httpsec: security headers (yamase's set as default), Host gate (empty Host rejected; active without login or with configured hosts), SafeNext, CookiePath, SecureCookies. layout: FlashURL/RedirectFlash/ApplyFlash/ WithoutFlash over query parameters, an info kind, AppearanceBack without the flash parameters. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7d5feb21a1
commit
d6bf6f94d8
8 files changed
+489
-17
No files matched your search
@@ -0,0 +1,46 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user