mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-21 19:32:58 +00:00
added HEAD method to caching (#1730)
* 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>
This commit is contained in:
parent
82d1039416
commit
d411ec10d6
7
middleware/cache/cache.go
vendored
7
middleware/cache/cache.go
vendored
@ -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)
|
||||
|
61
middleware/cache/cache_test.go
vendored
61
middleware/cache/cache_test.go
vendored
@ -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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user