| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package httpcache |
| 15 |
|
| 16 |
import ( |
| 17 |
"testing" |
| 18 |
|
| 19 |
qt "github.com/frankban/quicktest" |
| 20 |
"github.com/gohugoio/hugo/config" |
| 21 |
) |
| 22 |
|
| 23 |
func TestGlobMatcher(t *testing.T) { |
| 24 |
c := qt.New(t) |
| 25 |
|
| 26 |
g := GlobMatcher{ |
| 27 |
Includes: []string{"**/*.jpg", "**.png", "**/bar/**"}, |
| 28 |
Excludes: []string{"**/foo.jpg", "**.css"}, |
| 29 |
} |
| 30 |
|
| 31 |
p, err := g.CompilePredicate() |
| 32 |
c.Assert(err, qt.IsNil) |
| 33 |
|
| 34 |
c.Assert(p("foo.jpg"), qt.IsFalse) |
| 35 |
c.Assert(p("foo.png"), qt.IsTrue) |
| 36 |
c.Assert(p("foo/bar.jpg"), qt.IsTrue) |
| 37 |
c.Assert(p("foo/bar.png"), qt.IsTrue) |
| 38 |
c.Assert(p("foo/bar/foo.jpg"), qt.IsFalse) |
| 39 |
c.Assert(p("foo/bar/foo.css"), qt.IsFalse) |
| 40 |
c.Assert(p("foo.css"), qt.IsFalse) |
| 41 |
c.Assert(p("foo/bar/foo.css"), qt.IsFalse) |
| 42 |
c.Assert(p("foo/bar/foo.xml"), qt.IsTrue) |
| 43 |
} |
| 44 |
|
| 45 |
func TestDefaultConfig(t *testing.T) { |
| 46 |
c := qt.New(t) |
| 47 |
|
| 48 |
_, err := DefaultConfig.Compile() |
| 49 |
c.Assert(err, qt.IsNil) |
| 50 |
} |
| 51 |
|
| 52 |
func TestDecodeConfigInjectsDefaultAndCompiles(t *testing.T) { |
| 53 |
c := qt.New(t) |
| 54 |
|
| 55 |
cfg, err := DecodeConfig(config.BaseConfig{}, map[string]any{}) |
| 56 |
c.Assert(err, qt.IsNil) |
| 57 |
c.Assert(cfg, qt.DeepEquals, DefaultConfig) |
| 58 |
|
| 59 |
_, err = cfg.Compile() |
| 60 |
c.Assert(err, qt.IsNil) |
| 61 |
|
| 62 |
cfg, err = DecodeConfig(config.BaseConfig{}, map[string]any{ |
| 63 |
"cache": map[string]any{ |
| 64 |
"polls": []map[string]any{ |
| 65 |
{"disable": true}, |
| 66 |
}, |
| 67 |
}, |
| 68 |
}) |
| 69 |
c.Assert(err, qt.IsNil) |
| 70 |
|
| 71 |
_, err = cfg.Compile() |
| 72 |
c.Assert(err, qt.IsNil) |
| 73 |
} |