test: report failures as got/want and use subtests for tables
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
d20225982a
commit
b6843330a8
8 files changed
+184
-175
No files matched your search
@@ -9,21 +9,9 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func csrfRouter(sess *Session) *gin.Engine {
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) {
|
||||
if sess != nil {
|
||||
c.Set(ctxKey, sess)
|
||||
}
|
||||
}, CSRF())
|
||||
r.Any("/x", func(c *gin.Context) { c.Status(http.StatusNoContent) })
|
||||
return r
|
||||
}
|
||||
|
||||
func TestCSRF(t *testing.T) {
|
||||
sess := &Session{CSRF: "good"}
|
||||
cases := []struct {
|
||||
tests := []struct {
|
||||
name string
|
||||
sess *Session
|
||||
method string
|
||||
@@ -32,32 +20,43 @@ func TestCSRF(t *testing.T) {
|
||||
form string
|
||||
want int
|
||||
}{
|
||||
{"safe method", sess, http.MethodGet, "", "", "", 204},
|
||||
{"missing token", sess, http.MethodPost, "", "", "", 403},
|
||||
{"wrong token", sess, http.MethodPost, "bad", "", "", 403},
|
||||
{"header token", sess, http.MethodPost, "good", "", "", 204},
|
||||
{"form token", sess, http.MethodPost, "", "", "_csrf=good", 204},
|
||||
{"no session", nil, http.MethodPost, "", "", "", 204},
|
||||
{"same origin", nil, http.MethodPost, "", "http://example.com", "", 204},
|
||||
{"foreign origin without session", nil, http.MethodPost, "", "https://evil.test", "", 403},
|
||||
{"foreign origin with token", sess, http.MethodPost, "good", "https://evil.test", "", 403},
|
||||
{"opaque origin", nil, http.MethodPost, "", "null", "", 403},
|
||||
{name: "safe method", sess: sess, method: http.MethodGet, want: http.StatusNoContent},
|
||||
{name: "missing token", sess: sess, method: http.MethodPost, want: http.StatusForbidden},
|
||||
{name: "wrong token", sess: sess, method: http.MethodPost, header: "bad", want: http.StatusForbidden},
|
||||
{name: "header token", sess: sess, method: http.MethodPost, header: "good", want: http.StatusNoContent},
|
||||
{name: "form token", sess: sess, method: http.MethodPost, form: "_csrf=good", want: http.StatusNoContent},
|
||||
{name: "no session", method: http.MethodPost, want: http.StatusNoContent},
|
||||
{name: "same origin", method: http.MethodPost, origin: "http://example.com", want: http.StatusNoContent},
|
||||
{name: "foreign origin without session", method: http.MethodPost, origin: "https://evil.test", want: http.StatusForbidden},
|
||||
{name: "foreign origin with token", sess: sess, method: http.MethodPost, header: "good", origin: "https://evil.test", want: http.StatusForbidden},
|
||||
{name: "opaque origin", method: http.MethodPost, origin: "null", want: http.StatusForbidden},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
req := httptest.NewRequest(tc.method, "/x", strings.NewReader(tc.form))
|
||||
if tc.form != "" {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
if tc.origin != "" {
|
||||
req.Header.Set("Origin", tc.origin)
|
||||
}
|
||||
if tc.header != "" {
|
||||
req.Header.Set(CSRFHeader, tc.header)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
csrfRouter(tc.sess).ServeHTTP(w, req)
|
||||
if w.Code != tc.want {
|
||||
t.Errorf("%s: got %d want %d", tc.name, w.Code, tc.want)
|
||||
}
|
||||
gin.SetMode(gin.TestMode)
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) {
|
||||
if tt.sess != nil {
|
||||
c.Set(ctxKey, tt.sess)
|
||||
}
|
||||
}, CSRF())
|
||||
r.Any("/x", func(c *gin.Context) { c.Status(http.StatusNoContent) })
|
||||
|
||||
req := httptest.NewRequest(tt.method, "/x", strings.NewReader(tt.form))
|
||||
if tt.form != "" {
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
if tt.origin != "" {
|
||||
req.Header.Set("Origin", tt.origin)
|
||||
}
|
||||
if tt.header != "" {
|
||||
req.Header.Set(CSRFHeader, tt.header)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req)
|
||||
if w.Code != tt.want {
|
||||
t.Errorf("%s /x = %d, want %d", tt.method, w.Code, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user