Home / Function/ Test_Middleware_Encrypt_Cookie() — fiber Function Reference

Test_Middleware_Encrypt_Cookie() — fiber Function Reference

Architecture documentation for the Test_Middleware_Encrypt_Cookie() function in encryptcookie_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  e1aa6921_7bb1_1b8b_4ec1_fdf47c3ee2d5["Test_Middleware_Encrypt_Cookie()"]
  ac3bbef3_a1d3_d68b_64fb_619088dde10d["encryptcookie_test.go"]
  e1aa6921_7bb1_1b8b_4ec1_fdf47c3ee2d5 -->|defined in| ac3bbef3_a1d3_d68b_64fb_619088dde10d
  style e1aa6921_7bb1_1b8b_4ec1_fdf47c3ee2d5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/encryptcookie/encryptcookie_test.go lines 189–249

func Test_Middleware_Encrypt_Cookie(t *testing.T) {
	t.Parallel()
	testKey := GenerateKey(32)
	app := fiber.New()

	app.Use(New(Config{
		Key: testKey,
	}))

	app.Get("/", func(c fiber.Ctx) error {
		return c.SendString("value=" + c.Cookies("test"))
	})
	app.Post("/", func(c fiber.Ctx) error {
		c.Cookie(&fiber.Cookie{
			Name:  "test",
			Value: "SomeThing",
		})
		return nil
	})

	h := app.Handler()

	// Test empty cookie
	ctx := &fasthttp.RequestCtx{}
	ctx.Request.Header.SetMethod(fiber.MethodGet)
	h(ctx)
	require.Equal(t, 200, ctx.Response.StatusCode())
	require.Equal(t, "value=", string(ctx.Response.Body()))

	// Test invalid cookie
	ctx = &fasthttp.RequestCtx{}
	ctx.Request.Header.SetMethod(fiber.MethodGet)
	ctx.Request.Header.SetCookie("test", "Invalid")
	h(ctx)
	require.Equal(t, 200, ctx.Response.StatusCode())
	require.Equal(t, "value=", string(ctx.Response.Body()))
	ctx.Request.Header.SetCookie("test", "ixQURE2XOyZUs0WAOh2ehjWcP7oZb07JvnhWOsmeNUhPsj4+RyI=")
	h(ctx)
	require.Equal(t, 200, ctx.Response.StatusCode())
	require.Equal(t, "value=", string(ctx.Response.Body()))

	// Test valid cookie
	ctx = &fasthttp.RequestCtx{}
	ctx.Request.Header.SetMethod(fiber.MethodPost)
	h(ctx)
	require.Equal(t, 200, ctx.Response.StatusCode())

	encryptedCookie := fasthttp.Cookie{}
	encryptedCookie.SetKey("test")
	require.True(t, ctx.Response.Header.Cookie(&encryptedCookie), "Get cookie value")
	decryptedCookieValue, err := DecryptCookie("test", string(encryptedCookie.Value()), testKey)
	require.NoError(t, err)
	require.Equal(t, "SomeThing", decryptedCookieValue)

	ctx = &fasthttp.RequestCtx{}
	ctx.Request.Header.SetMethod(fiber.MethodGet)
	ctx.Request.Header.SetCookie("test", string(encryptedCookie.Value()))
	h(ctx)
	require.Equal(t, 200, ctx.Response.StatusCode())
	require.Equal(t, "value=SomeThing", string(ctx.Response.Body()))
}

Domain

Subdomains

Frequently Asked Questions

What does Test_Middleware_Encrypt_Cookie() do?
Test_Middleware_Encrypt_Cookie() is a function in the fiber codebase, defined in middleware/encryptcookie/encryptcookie_test.go.
Where is Test_Middleware_Encrypt_Cookie() defined?
Test_Middleware_Encrypt_Cookie() is defined in middleware/encryptcookie/encryptcookie_test.go at line 189.

Analyze Your Own Codebase

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

Try Supermodel Free