Skip to content

Commit

Permalink
added HEAD method to caching (#1730)
Browse files Browse the repository at this point in the history
* added HEAD method to caching

* changed key due to head and get sharing same key

* Update cache.go

- add a improvement task for later

* Update cache.go

correct comment

Co-authored-by: Jesse Quinn <jesse.quinn@zpesystems.com>
Co-authored-by: RW <rene@gofiber.io>
  • Loading branch information
3 people committed Jan 24, 2022
1 parent 82d1039 commit d411ec1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
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

0 comments on commit d411ec1

Please sign in to comment.