Home / Function/ Test_Session_Reset() — fiber Function Reference

Test_Session_Reset() — fiber Function Reference

Architecture documentation for the Test_Session_Reset() function in session_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  d2395cc0_63a5_04b4_14bd_bdbcee924bd9["Test_Session_Reset()"]
  397e6e82_749b_4ef2_9365_02be671c59f7["session_test.go"]
  d2395cc0_63a5_04b4_14bd_bdbcee924bd9 -->|defined in| 397e6e82_749b_4ef2_9365_02be671c59f7
  style d2395cc0_63a5_04b4_14bd_bdbcee924bd9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/session/session_test.go lines 1130–1199

func Test_Session_Reset(t *testing.T) {
	t.Parallel()
	// fiber instance
	app := fiber.New()

	// session store
	store := NewStore()

	t.Run("reset session data and id, and set fresh to be true", func(t *testing.T) {
		t.Parallel()
		// fiber context
		ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
		// a random session uuid
		originalSessionUUIDString := ""

		// now the session is in the storage
		freshSession, err := store.Get(ctx)
		require.NoError(t, err)

		originalSessionUUIDString = freshSession.ID()

		// set a value
		freshSession.Set("name", "fenny")
		freshSession.Set("email", "fenny@example.com")

		err = freshSession.Save()
		require.NoError(t, err)

		freshSession.Release()
		app.ReleaseCtx(ctx)
		ctx = app.AcquireCtx(&fasthttp.RequestCtx{})

		// set cookie
		ctx.Request().Header.SetCookie("session_id", originalSessionUUIDString)

		// as the session is in the storage, session.fresh should be false
		acquiredSession, err := store.Get(ctx)
		require.NoError(t, err)
		require.False(t, acquiredSession.Fresh())

		err = acquiredSession.Reset()
		require.NoError(t, err)

		require.NotEqual(t, originalSessionUUIDString, acquiredSession.ID())

		// acquiredSession.fresh should be true after resetting
		require.True(t, acquiredSession.Fresh())

		// Check that the session data has been reset
		keys := acquiredSession.Keys()
		require.Equal(t, []any{}, keys)

		// Set a new value for 'name' and check that it's updated
		acquiredSession.Set("name", "john")
		require.Equal(t, "john", acquiredSession.Get("name"))
		require.Nil(t, acquiredSession.Get("email"))

		// Save after resetting
		err = acquiredSession.Save()
		require.NoError(t, err)

		acquiredSession.Release()

		// Check that the session id is not in the header or cookie anymore
		require.Empty(t, string(ctx.Response().Header.Peek("session_id")))
		require.Empty(t, string(ctx.Request().Header.Peek("session_id")))

		app.ReleaseCtx(ctx)
	})
}

Subdomains

Frequently Asked Questions

What does Test_Session_Reset() do?
Test_Session_Reset() is a function in the fiber codebase, defined in middleware/session/session_test.go.
Where is Test_Session_Reset() defined?
Test_Session_Reset() is defined in middleware/session/session_test.go at line 1130.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free