Home / Function/ Test_Cache_ConfigurationAndResponseHandling() — fiber Function Reference

Test_Cache_ConfigurationAndResponseHandling() — fiber Function Reference

Architecture documentation for the Test_Cache_ConfigurationAndResponseHandling() function in cache_test.go from the fiber codebase.

Entity Profile

Dependency Diagram

graph TD
  c8059ad1_a919_887a_6b0a_aa05695a69b6["Test_Cache_ConfigurationAndResponseHandling()"]
  8453a087_9678_fe96_1b20_2d125b6f8656["cache_test.go"]
  c8059ad1_a919_887a_6b0a_aa05695a69b6 -->|defined in| 8453a087_9678_fe96_1b20_2d125b6f8656
  style c8059ad1_a919_887a_6b0a_aa05695a69b6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

middleware/cache/cache_test.go lines 4841–5093

func Test_Cache_ConfigurationAndResponseHandling(t *testing.T) {
	t.Parallel()

	t.Run("response with Vary star prevents caching", func(t *testing.T) {
		t.Parallel()
		app := fiber.New()
		app.Use(New(Config{Expiration: 1 * time.Hour}))
		app.Get("/test", func(c fiber.Ctx) error {
			c.Response().Header.Set("Vary", "*")
			c.Response().Header.Set("Cache-Control", "max-age=3600")
			return c.SendString("test")
		})

		rsp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody))
		require.NoError(t, err)
		require.Equal(t, cacheUnreachable, rsp.Header.Get("X-Cache"))
	})

	t.Run("next function prevents caching", func(t *testing.T) {
		t.Parallel()
		app := fiber.New()
		app.Use(New(Config{
			Expiration: 1 * time.Hour,
			Next: func(c fiber.Ctx) bool {
				return c.Path() == "/skip"
			},
		}))
		app.Get("/skip", func(c fiber.Ctx) error {
			return c.SendString("test")
		})

		rsp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/skip", http.NoBody))
		require.NoError(t, err)
		require.Equal(t, cacheUnreachable, rsp.Header.Get("X-Cache"))
	})

	t.Run("non-cacheable status code", func(t *testing.T) {
		t.Parallel()
		app := fiber.New()
		app.Use(New(Config{Expiration: 1 * time.Hour}))
		app.Get("/test", func(c fiber.Ctx) error {
			return c.Status(fiber.StatusCreated).SendString("created")
		})

		rsp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody))
		require.NoError(t, err)
		require.Equal(t, cacheUnreachable, rsp.Header.Get("X-Cache"))
	})

	t.Run("body larger than MaxBytes", func(t *testing.T) {
		t.Parallel()
		app := fiber.New()
		app.Use(New(Config{
			Expiration: 1 * time.Hour,
			MaxBytes:   10,
		}))
		app.Get("/test", func(c fiber.Ctx) error {
			return c.Send(make([]byte, 100))
		})

		rsp, err := app.Test(httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody))
		require.NoError(t, err)
		require.Equal(t, cacheUnreachable, rsp.Header.Get("X-Cache"))
	})

	t.Run("authorization without shared cache directives", func(t *testing.T) {
		t.Parallel()
		app := fiber.New()
		app.Use(New(Config{Expiration: 1 * time.Hour}))
		app.Get("/test", func(c fiber.Ctx) error {
			c.Response().Header.Set("Cache-Control", "max-age=3600")
			return c.SendString("test")
		})

		req := httptest.NewRequest(fiber.MethodGet, "/test", http.NoBody)
		req.Header.Set("Authorization", "******")
		rsp, err := app.Test(req)
		require.NoError(t, err)
		require.Equal(t, cacheUnreachable, rsp.Header.Get("X-Cache"))
	})

Subdomains

Frequently Asked Questions

What does Test_Cache_ConfigurationAndResponseHandling() do?
Test_Cache_ConfigurationAndResponseHandling() is a function in the fiber codebase, defined in middleware/cache/cache_test.go.
Where is Test_Cache_ConfigurationAndResponseHandling() defined?
Test_Cache_ConfigurationAndResponseHandling() is defined in middleware/cache/cache_test.go at line 4841.

Analyze Your Own Codebase

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

Try Supermodel Free