MCPcopy
hub / github.com/gofiber/fiber / Test_Store_getSessionID

Function Test_Store_getSessionID

middleware/session/store_test.go:15–67  ·  view source on GitHub ↗

go test -run Test_Store_getSessionID

(t *testing.T)

Source from the content-addressed store, hash-verified

13
14// go test -run Test_Store_getSessionID
15func Test_Store_getSessionID(t *testing.T) {
16 t.Parallel()
17 expectedID := "test-session-id"
18
19 // fiber instance
20 app := fiber.New()
21
22 t.Run("from cookie", func(t *testing.T) {
23 t.Parallel()
24 // session store
25 store := NewStore()
26 // fiber context
27 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
28 defer app.ReleaseCtx(ctx)
29
30 // set cookie
31 ctx.Request().Header.SetCookie(store.Extractor.Key, expectedID)
32
33 require.Equal(t, expectedID, store.getSessionID(ctx))
34 })
35
36 t.Run("from header", func(t *testing.T) {
37 t.Parallel()
38 // session store
39 store := NewStore(Config{
40 Extractor: extractors.FromHeader("session_id"),
41 })
42 // fiber context
43 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
44 defer app.ReleaseCtx(ctx)
45
46 // set header
47 ctx.Request().Header.Set(store.Extractor.Key, expectedID)
48
49 require.Equal(t, expectedID, store.getSessionID(ctx))
50 })
51
52 t.Run("from url query", func(t *testing.T) {
53 t.Parallel()
54 // session store
55 store := NewStore(Config{
56 Extractor: extractors.FromQuery("session_id"),
57 })
58 // fiber context
59 ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
60 defer app.ReleaseCtx(ctx)
61
62 // set url parameter
63 ctx.Request().SetRequestURI(fmt.Sprintf("/path?%s=%s", store.Extractor.Key, expectedID))
64
65 require.Equal(t, expectedID, store.getSessionID(ctx))
66 })
67}
68
69// go test -run Test_Store_Get
70// Regression: https://github.com/gofiber/fiber/issues/1408

Callers

nothing calls this directly

Calls 10

getSessionIDMethod · 0.95
FromHeaderFunction · 0.92
FromQueryFunction · 0.92
NewStoreFunction · 0.85
AcquireCtxMethod · 0.80
ReleaseCtxMethod · 0.80
NewMethod · 0.65
RequestMethod · 0.65
SetMethod · 0.65
SetCookieMethod · 0.45

Tested by

no test coverage detected