Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added HEAD method to caching #1730

Merged
merged 4 commits into from Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions middleware/cache/cache.go
Expand Up @@ -57,14 +57,15 @@ func New(config ...Config) fiber.Handler {

// Return new handler
return func(c *fiber.Ctx) error {
// Only cache GET methods
if c.Method() != fiber.MethodGet {
// Only cache GET and HEAD methods
if c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead {
c.Set(cfg.CacheHeader, cacheUnreachable)
return c.Next()
}

// Get key from request
key := cfg.KeyGenerator(c)
// TODO(allocation optimization): try to minimize the allocation from 2 to 1
key := cfg.KeyGenerator(c) + "_" + c.Method()

// Get entry from pool
e := manager.get(key)
Expand Down
61 changes: 61 additions & 0 deletions middleware/cache/cache_test.go
Expand Up @@ -341,6 +341,67 @@ func Test_CacheHeader(t *testing.T) {
utils.AssertEqual(t, cacheUnreachable, errRespCached.Header.Get("X-Cache"))
}

func Test_Cache_WithHead(t *testing.T) {
app := fiber.New()
app.Use(New())

app.Get("/", func(c *fiber.Ctx) error {
now := fmt.Sprintf("%d", time.Now().UnixNano())
return c.SendString(now)
})

req := httptest.NewRequest("HEAD", "/", nil)
resp, err := app.Test(req)
utils.AssertEqual(t, cacheMiss, resp.Header.Get("X-Cache"))

cachedReq := httptest.NewRequest("HEAD", "/", nil)
cachedResp, err := app.Test(cachedReq)
utils.AssertEqual(t, cacheHit, cachedResp.Header.Get("X-Cache"))

body, err := ioutil.ReadAll(resp.Body)
utils.AssertEqual(t, nil, err)
cachedBody, err := ioutil.ReadAll(cachedResp.Body)
utils.AssertEqual(t, nil, err)

utils.AssertEqual(t, cachedBody, body)
}

func Test_Cache_WithHeadThenGet(t *testing.T) {
app := fiber.New()
app.Use(New())
app.Get("/get", func(c *fiber.Ctx) error {
return c.SendString(c.Query("cache"))
})

headResp, err := app.Test(httptest.NewRequest("HEAD", "/head?cache=123", nil))
utils.AssertEqual(t, nil, err)
headBody, err := ioutil.ReadAll(headResp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "", string(headBody))
utils.AssertEqual(t, cacheMiss, headResp.Header.Get("X-Cache"))

headResp, err = app.Test(httptest.NewRequest("HEAD", "/head?cache=123", nil))
utils.AssertEqual(t, nil, err)
headBody, err = ioutil.ReadAll(headResp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "", string(headBody))
utils.AssertEqual(t, cacheHit, headResp.Header.Get("X-Cache"))

getResp, err := app.Test(httptest.NewRequest("GET", "/get?cache=123", nil))
utils.AssertEqual(t, nil, err)
getBody, err := ioutil.ReadAll(getResp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "123", string(getBody))
utils.AssertEqual(t, cacheMiss, getResp.Header.Get("X-Cache"))

getResp, err = app.Test(httptest.NewRequest("GET", "/get?cache=123", nil))
utils.AssertEqual(t, nil, err)
getBody, err = ioutil.ReadAll(getResp.Body)
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, "123", string(getBody))
utils.AssertEqual(t, cacheHit, getResp.Header.Get("X-Cache"))
}

func Test_CustomCacheHeader(t *testing.T) {
app := fiber.New()

Expand Down