Debian Codegraph @

cache/httpcache/httpcache_integration_test.go

raw
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_test
15
16 import (
17 "testing"
18 "time"
19
20 qt "github.com/frankban/quicktest"
21 "github.com/gohugoio/hugo/hugolib"
22 )
23
24 func TestConfigCustom(t *testing.T) {
25 t.Parallel()
26
27 files := `
28 -- hugo.toml --
29 [httpcache]
30 [httpcache.cache.for]
31 includes = ["**gohugo.io**"]
32 [[httpcache.polls]]
33 low = "5s"
34 high = "32s"
35 [httpcache.polls.for]
36 includes = ["**gohugo.io**"]
37
38
39 `
40
41 b := hugolib.Test(t, files)
42
43 httpcacheConf := b.H.Configs.Base.HTTPCache
44 compiled := b.H.Configs.Base.C.HTTPCache
45
46 b.Assert(httpcacheConf.Cache.For.Includes, qt.DeepEquals, []string{"**gohugo.io**"})
47 b.Assert(httpcacheConf.Cache.For.Excludes, qt.IsNil)
48
49 pc := compiled.PollConfigFor("https://gohugo.io/foo.jpg")
50 b.Assert(pc.Config.Low, qt.Equals, 5*time.Second)
51 b.Assert(pc.Config.High, qt.Equals, 32*time.Second)
52 b.Assert(compiled.PollConfigFor("https://example.com/foo.jpg").IsZero(), qt.IsTrue)
53 }
54
55 func TestConfigDefault(t *testing.T) {
56 t.Parallel()
57
58 files := `
59 -- hugo.toml --
60 `
61 b := hugolib.Test(t, files)
62
63 compiled := b.H.Configs.Base.C.HTTPCache
64
65 b.Assert(compiled.For("https://gohugo.io/posts.json"), qt.IsFalse)
66 b.Assert(compiled.For("https://gohugo.io/foo.jpg"), qt.IsFalse)
67 b.Assert(compiled.PollConfigFor("https://gohugo.io/foo.jpg").Config.Disable, qt.IsTrue)
68 }
69
70 func TestConfigPollsOnly(t *testing.T) {
71 t.Parallel()
72 files := `
73 -- hugo.toml --
74 [httpcache]
75 [[httpcache.polls]]
76 low = "5s"
77 high = "32s"
78 [httpcache.polls.for]
79 includes = ["**gohugo.io**"]
80
81
82 `
83
84 b := hugolib.Test(t, files)
85
86 compiled := b.H.Configs.Base.C.HTTPCache
87
88 b.Assert(compiled.For("https://gohugo.io/posts.json"), qt.IsFalse)
89 b.Assert(compiled.For("https://gohugo.io/foo.jpg"), qt.IsFalse)
90
91 pc := compiled.PollConfigFor("https://gohugo.io/foo.jpg")
92 b.Assert(pc.Config.Low, qt.Equals, 5*time.Second)
93 b.Assert(pc.Config.High, qt.Equals, 32*time.Second)
94 b.Assert(compiled.PollConfigFor("https://example.com/foo.jpg").IsZero(), qt.IsTrue)
95 }