mirror of https://github.com/gofiber/fiber.git
🧹 update: add methods configuration for cache middleware (#2081)
* 🧹 update: add methods configuration for cache middleware * 🧹 update: add methods configuration for cache middlewarepull/2089/head
parent
f482b303b5
commit
8ec62a64cc
|
@ -125,6 +125,12 @@ type Config struct {
|
|||
//
|
||||
// Default: 0
|
||||
MaxBytes uint
|
||||
|
||||
// You can specify HTTP methods to cache.
|
||||
// The middleware just caches the routes of its methods in this slice.
|
||||
//
|
||||
// Default: []string{fiber.MethodGet, fiber.MethodHead}
|
||||
Methods []string
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -144,5 +150,6 @@ var ConfigDefault = Config{
|
|||
StoreResponseHeaders: false,
|
||||
Storage: nil,
|
||||
MaxBytes: 0,
|
||||
Methods: []string{fiber.MethodGet, fiber.MethodHead},
|
||||
}
|
||||
```
|
||||
|
|
|
@ -83,8 +83,15 @@ func New(config ...Config) fiber.Handler {
|
|||
|
||||
// Return new handler
|
||||
return func(c *fiber.Ctx) error {
|
||||
// Only cache GET and HEAD methods
|
||||
if c.Method() != fiber.MethodGet && c.Method() != fiber.MethodHead {
|
||||
// Only cache selected methods
|
||||
var isExists bool
|
||||
for _, method := range cfg.Methods {
|
||||
if c.Method() == method {
|
||||
isExists = true
|
||||
}
|
||||
}
|
||||
|
||||
if !isExists {
|
||||
c.Set(cfg.CacheHeader, cacheUnreachable)
|
||||
return c.Next()
|
||||
}
|
||||
|
|
|
@ -173,7 +173,7 @@ func Test_Cache_Invalid_Expiration(t *testing.T) {
|
|||
utils.AssertEqual(t, cachedBody, body)
|
||||
}
|
||||
|
||||
func Test_Cache_Invalid_Method(t *testing.T) {
|
||||
func Test_Cache_Get(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := fiber.New()
|
||||
|
@ -213,6 +213,48 @@ func Test_Cache_Invalid_Method(t *testing.T) {
|
|||
utils.AssertEqual(t, "123", string(body))
|
||||
}
|
||||
|
||||
func Test_Cache_Post(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Use(New(Config{
|
||||
Methods: []string{fiber.MethodPost},
|
||||
}))
|
||||
|
||||
app.Post("/", func(c *fiber.Ctx) error {
|
||||
return c.SendString(c.Query("cache"))
|
||||
})
|
||||
|
||||
app.Get("/get", func(c *fiber.Ctx) error {
|
||||
return c.SendString(c.Query("cache"))
|
||||
})
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest("POST", "/?cache=123", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "123", string(body))
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest("POST", "/?cache=12345", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "123", string(body))
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest("GET", "/get?cache=123", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "123", string(body))
|
||||
|
||||
resp, err = app.Test(httptest.NewRequest("GET", "/get?cache=12345", nil))
|
||||
utils.AssertEqual(t, nil, err)
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, "12345", string(body))
|
||||
}
|
||||
|
||||
func Test_Cache_NothingToCache(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -428,10 +470,12 @@ func Test_Cache_WithHead(t *testing.T) {
|
|||
|
||||
req := httptest.NewRequest("HEAD", "/", nil)
|
||||
resp, err := app.Test(req)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, cacheMiss, resp.Header.Get("X-Cache"))
|
||||
|
||||
cachedReq := httptest.NewRequest("HEAD", "/", nil)
|
||||
cachedResp, err := app.Test(cachedReq)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, cacheHit, cachedResp.Header.Get("X-Cache"))
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
|
|
|
@ -66,6 +66,12 @@ type Config struct {
|
|||
//
|
||||
// Default: 0
|
||||
MaxBytes uint
|
||||
|
||||
// You can specify HTTP methods to cache.
|
||||
// The middleware just caches the routes of its methods in this slice.
|
||||
//
|
||||
// Default: []string{fiber.MethodGet, fiber.MethodHead}
|
||||
Methods []string
|
||||
}
|
||||
|
||||
// ConfigDefault is the default config
|
||||
|
@ -81,6 +87,7 @@ var ConfigDefault = Config{
|
|||
StoreResponseHeaders: false,
|
||||
Storage: nil,
|
||||
MaxBytes: 0,
|
||||
Methods: []string{fiber.MethodGet, fiber.MethodHead},
|
||||
}
|
||||
|
||||
// Helper function to set default values
|
||||
|
@ -114,5 +121,8 @@ func configDefault(config ...Config) Config {
|
|||
if cfg.KeyGenerator == nil {
|
||||
cfg.KeyGenerator = ConfigDefault.KeyGenerator
|
||||
}
|
||||
if len(cfg.Methods) == 0 {
|
||||
cfg.Methods = ConfigDefault.Methods
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue