mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-25 21:21:22 +03:00
cache: Apply httpcache defaults for polling config
Previously, compiling the config with partial or missing poll configs would introduce a panic. This ensures that the default poll configs are applied in such scenarios to ensure config is valid. Fixes #13471
This commit is contained in:
parent
61c39ae63b
commit
d28c84a871
2 changed files with 43 additions and 1 deletions
13
cache/httpcache/httpcache.go
vendored
13
cache/httpcache/httpcache.go
vendored
|
@ -188,7 +188,7 @@ func (gm *GlobMatcher) CompilePredicate() (func(string) bool, error) {
|
||||||
return p, nil
|
return p, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DecodeConfig(bcfg config.BaseConfig, m map[string]any) (Config, error) {
|
func DecodeConfig(_ config.BaseConfig, m map[string]any) (Config, error) {
|
||||||
if len(m) == 0 {
|
if len(m) == 0 {
|
||||||
return DefaultConfig, nil
|
return DefaultConfig, nil
|
||||||
}
|
}
|
||||||
|
@ -214,5 +214,16 @@ func DecodeConfig(bcfg config.BaseConfig, m map[string]any) (Config, error) {
|
||||||
c.Cache.For = DefaultConfig.Cache.For
|
c.Cache.For = DefaultConfig.Cache.For
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for pci := range c.Polls {
|
||||||
|
if c.Polls[pci].For.IsZero() {
|
||||||
|
c.Polls[pci].For = DefaultConfig.Cache.For
|
||||||
|
c.Polls[pci].Disable = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(c.Polls) == 0 {
|
||||||
|
c.Polls = DefaultConfig.Polls
|
||||||
|
}
|
||||||
|
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
31
cache/httpcache/httpcache_test.go
vendored
31
cache/httpcache/httpcache_test.go
vendored
|
@ -17,6 +17,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
qt "github.com/frankban/quicktest"
|
qt "github.com/frankban/quicktest"
|
||||||
|
"github.com/gohugoio/hugo/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGlobMatcher(t *testing.T) {
|
func TestGlobMatcher(t *testing.T) {
|
||||||
|
@ -40,3 +41,33 @@ func TestGlobMatcher(t *testing.T) {
|
||||||
c.Assert(p("foo/bar/foo.css"), qt.IsFalse)
|
c.Assert(p("foo/bar/foo.css"), qt.IsFalse)
|
||||||
c.Assert(p("foo/bar/foo.xml"), qt.IsTrue)
|
c.Assert(p("foo/bar/foo.xml"), qt.IsTrue)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDefaultConfig(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
|
||||||
|
_, err := DefaultConfig.Compile()
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDecodeConfigInjectsDefaultAndCompiles(t *testing.T) {
|
||||||
|
c := qt.New(t)
|
||||||
|
|
||||||
|
cfg, err := DecodeConfig(config.BaseConfig{}, map[string]interface{}{})
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
c.Assert(cfg, qt.DeepEquals, DefaultConfig)
|
||||||
|
|
||||||
|
_, err = cfg.Compile()
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
|
||||||
|
cfg, err = DecodeConfig(config.BaseConfig{}, map[string]any{
|
||||||
|
"cache": map[string]any{
|
||||||
|
"polls": []map[string]any{
|
||||||
|
{"disable": true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
|
||||||
|
_, err = cfg.Compile()
|
||||||
|
c.Assert(err, qt.IsNil)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue