Test_CSRF_WithSession() — fiber Function Reference
Architecture documentation for the Test_CSRF_WithSession() function in csrf_test.go from the fiber codebase.
Entity Profile
Dependency Diagram
graph TD eb742eae_2a85_b9df_93b2_10c2fec75a9c["Test_CSRF_WithSession()"] 306a0c68_f5a5_b368_f37a_1419425a8fea["csrf_test.go"] eb742eae_2a85_b9df_93b2_10c2fec75a9c -->|defined in| 306a0c68_f5a5_b368_f37a_1419425a8fea style eb742eae_2a85_b9df_93b2_10c2fec75a9c fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
middleware/csrf/csrf_test.go lines 253–341
func Test_CSRF_WithSession(t *testing.T) {
t.Parallel()
// session store
store := session.NewStore(session.Config{
Extractor: extractors.FromCookie("_session"),
})
// fiber instance
app := fiber.New()
// fiber context
ctx := &fasthttp.RequestCtx{}
defer app.ReleaseCtx(app.AcquireCtx(ctx))
// get session
sess, err := store.Get(app.AcquireCtx(ctx))
require.NoError(t, err)
require.True(t, sess.Fresh())
// the session string is no longer be 123
newSessionIDString := sess.ID()
require.NoError(t, sess.Save())
app.AcquireCtx(ctx).Request().Header.SetCookie("_session", newSessionIDString)
// middleware config
config := Config{
Session: store,
}
// middleware
app.Use(New(config))
app.Post("/", func(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
})
h := app.Handler()
methods := [4]string{fiber.MethodGet, fiber.MethodHead, fiber.MethodOptions, fiber.MethodTrace}
for _, method := range methods {
// Generate CSRF token
ctx.Request.Header.SetMethod(fiber.MethodGet)
ctx.Request.Header.SetCookie("_session", newSessionIDString)
h(ctx)
// Without CSRF cookie
ctx.Request.Reset()
ctx.Response.Reset()
ctx.Request.Header.SetMethod(fiber.MethodPost)
ctx.Request.Header.SetCookie("_session", newSessionIDString)
h(ctx)
require.Equal(t, 403, ctx.Response.StatusCode())
// Empty/invalid CSRF token
ctx.Request.Reset()
ctx.Response.Reset()
ctx.Request.Header.SetMethod(fiber.MethodPost)
ctx.Request.Header.Set(HeaderName, "johndoe")
ctx.Request.Header.SetCookie("_session", newSessionIDString)
h(ctx)
require.Equal(t, 403, ctx.Response.StatusCode())
// Valid CSRF token
ctx.Request.Reset()
ctx.Response.Reset()
ctx.Request.Header.SetMethod(method)
ctx.Request.Header.SetCookie("_session", newSessionIDString)
h(ctx)
token := string(ctx.Response.Header.Peek(fiber.HeaderSetCookie))
for header := range strings.SplitSeq(token, ";") {
if strings.Split(utils.TrimSpace(header), "=")[0] == ConfigDefault.CookieName {
token = strings.Split(header, "=")[1]
break
}
}
ctx.Request.Reset()
ctx.Response.Reset()
Domain
Subdomains
Defined In
Source
Frequently Asked Questions
What does Test_CSRF_WithSession() do?
Test_CSRF_WithSession() is a function in the fiber codebase, defined in middleware/csrf/csrf_test.go.
Where is Test_CSRF_WithSession() defined?
Test_CSRF_WithSession() is defined in middleware/csrf/csrf_test.go at line 253.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free