| 1 |
// Copyright 2020 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 config |
| 15 |
|
| 16 |
import ( |
| 17 |
"errors" |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/common/herrors" |
| 21 |
"github.com/gohugoio/hugo/common/loggers" |
| 22 |
"github.com/gohugoio/hugo/common/types" |
| 23 |
|
| 24 |
qt "github.com/frankban/quicktest" |
| 25 |
) |
| 26 |
|
| 27 |
func TestBuild(t *testing.T) { |
| 28 |
c := qt.New(t) |
| 29 |
|
| 30 |
v := New() |
| 31 |
v.Set("build", map[string]any{ |
| 32 |
"useResourceCacheWhen": "always", |
| 33 |
}) |
| 34 |
|
| 35 |
b := DecodeBuildConfig(v) |
| 36 |
|
| 37 |
c.Assert(b.UseResourceCacheWhen, qt.Equals, "always") |
| 38 |
|
| 39 |
v.Set("build", map[string]any{ |
| 40 |
"useResourceCacheWhen": "foo", |
| 41 |
}) |
| 42 |
|
| 43 |
b = DecodeBuildConfig(v) |
| 44 |
|
| 45 |
c.Assert(b.UseResourceCacheWhen, qt.Equals, "fallback") |
| 46 |
|
| 47 |
c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, true) |
| 48 |
c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, false) |
| 49 |
|
| 50 |
b.UseResourceCacheWhen = "always" |
| 51 |
c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, true) |
| 52 |
c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, true) |
| 53 |
c.Assert(b.UseResourceCache(nil), qt.Equals, true) |
| 54 |
|
| 55 |
b.UseResourceCacheWhen = "never" |
| 56 |
c.Assert(b.UseResourceCache(herrors.ErrFeatureNotAvailable), qt.Equals, false) |
| 57 |
c.Assert(b.UseResourceCache(errors.New("err")), qt.Equals, false) |
| 58 |
c.Assert(b.UseResourceCache(nil), qt.Equals, false) |
| 59 |
} |
| 60 |
|
| 61 |
func TestServer(t *testing.T) { |
| 62 |
c := qt.New(t) |
| 63 |
|
| 64 |
cfg, err := FromConfigString(`[[server.headers]] |
| 65 |
for = "/*.jpg" |
| 66 |
|
| 67 |
[server.headers.values] |
| 68 |
X-Frame-Options = "DENY" |
| 69 |
X-XSS-Protection = "1; mode=block" |
| 70 |
X-Content-Type-Options = "nosniff" |
| 71 |
|
| 72 |
[[server.redirects]] |
| 73 |
from = "/foo/**" |
| 74 |
to = "/baz/index.html" |
| 75 |
status = 200 |
| 76 |
|
| 77 |
[[server.redirects]] |
| 78 |
from = "/loop/**" |
| 79 |
to = "/loop/foo/" |
| 80 |
status = 200 |
| 81 |
|
| 82 |
[[server.redirects]] |
| 83 |
from = "/b/**" |
| 84 |
fromRe = "/b/(.*)/" |
| 85 |
to = "/baz/$1/" |
| 86 |
status = 200 |
| 87 |
|
| 88 |
[[server.redirects]] |
| 89 |
fromRe = "/c/(.*)/" |
| 90 |
to = "/boo/$1/" |
| 91 |
status = 200 |
| 92 |
|
| 93 |
[[server.redirects]] |
| 94 |
fromRe = "/d/(.*)/" |
| 95 |
to = "/boo/$1/" |
| 96 |
status = 200 |
| 97 |
|
| 98 |
[[server.redirects]] |
| 99 |
from = "/google/**" |
| 100 |
to = "https://google.com/" |
| 101 |
status = 301 |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
`, "toml") |
| 106 |
|
| 107 |
c.Assert(err, qt.IsNil) |
| 108 |
|
| 109 |
s, err := DecodeServer(cfg) |
| 110 |
c.Assert(err, qt.IsNil) |
| 111 |
c.Assert(s.CompileConfig(loggers.NewDefault()), qt.IsNil) |
| 112 |
|
| 113 |
c.Assert(s.MatchHeaders("/foo.jpg"), qt.DeepEquals, []types.KeyValueStr{ |
| 114 |
{Key: "X-Content-Type-Options", Value: "nosniff"}, |
| 115 |
{Key: "X-Frame-Options", Value: "DENY"}, |
| 116 |
{Key: "X-XSS-Protection", Value: "1; mode=block"}, |
| 117 |
}) |
| 118 |
|
| 119 |
c.Assert(s.MatchRedirect("/foo/bar/baz", nil), qt.DeepEquals, Redirect{ |
| 120 |
From: "/foo/**", |
| 121 |
To: "/baz/", |
| 122 |
Status: 200, |
| 123 |
}) |
| 124 |
|
| 125 |
c.Assert(s.MatchRedirect("/foo/bar/", nil), qt.DeepEquals, Redirect{ |
| 126 |
From: "/foo/**", |
| 127 |
To: "/baz/", |
| 128 |
Status: 200, |
| 129 |
}) |
| 130 |
|
| 131 |
c.Assert(s.MatchRedirect("/b/c/", nil), qt.DeepEquals, Redirect{ |
| 132 |
From: "/b/**", |
| 133 |
FromRe: "/b/(.*)/", |
| 134 |
To: "/baz/c/", |
| 135 |
Status: 200, |
| 136 |
}) |
| 137 |
|
| 138 |
c.Assert(s.MatchRedirect("/c/d/", nil).To, qt.Equals, "/boo/d/") |
| 139 |
c.Assert(s.MatchRedirect("/c/d/e/", nil).To, qt.Equals, "/boo/d/e/") |
| 140 |
|
| 141 |
c.Assert(s.MatchRedirect("/someother", nil), qt.DeepEquals, Redirect{}) |
| 142 |
|
| 143 |
c.Assert(s.MatchRedirect("/google/foo", nil), qt.DeepEquals, Redirect{ |
| 144 |
From: "/google/**", |
| 145 |
To: "https://google.com/", |
| 146 |
Status: 301, |
| 147 |
}) |
| 148 |
} |
| 149 |
|
| 150 |
func TestBuildConfigCacheBusters(t *testing.T) { |
| 151 |
c := qt.New(t) |
| 152 |
cfg := New() |
| 153 |
conf := DecodeBuildConfig(cfg) |
| 154 |
l := loggers.NewDefault() |
| 155 |
c.Assert(conf.CompileConfig(l), qt.IsNil) |
| 156 |
|
| 157 |
m, _ := conf.MatchCacheBuster(l, "tailwind.config.js") |
| 158 |
c.Assert(m, qt.IsNotNil) |
| 159 |
c.Assert(m("css"), qt.IsTrue) |
| 160 |
c.Assert(m("js"), qt.IsFalse) |
| 161 |
|
| 162 |
m, _ = conf.MatchCacheBuster(l, "foo.bar") |
| 163 |
c.Assert(m, qt.IsNil) |
| 164 |
} |
| 165 |
|
| 166 |
func TestBuildConfigCacheBusterstTailwindSetup(t *testing.T) { |
| 167 |
c := qt.New(t) |
| 168 |
cfg := New() |
| 169 |
cfg.Set("build", map[string]any{ |
| 170 |
"cacheBusters": []map[string]string{ |
| 171 |
{ |
| 172 |
"source": "assets/watching/hugo_stats\\.json", |
| 173 |
"target": "css", |
| 174 |
}, |
| 175 |
{ |
| 176 |
"source": "(postcss|tailwind)\\.config\\.js", |
| 177 |
"target": "css", |
| 178 |
}, |
| 179 |
{ |
| 180 |
"source": "assets/.*\\.(js|ts|jsx|tsx)", |
| 181 |
"target": "js", |
| 182 |
}, |
| 183 |
{ |
| 184 |
"source": "assets/.*\\.(.*)$", |
| 185 |
"target": "$1", |
| 186 |
}, |
| 187 |
}, |
| 188 |
}) |
| 189 |
|
| 190 |
conf := DecodeBuildConfig(cfg) |
| 191 |
l := loggers.NewDefault() |
| 192 |
c.Assert(conf.CompileConfig(l), qt.IsNil) |
| 193 |
|
| 194 |
m, err := conf.MatchCacheBuster(l, "assets/watching/hugo_stats.json") |
| 195 |
c.Assert(err, qt.IsNil) |
| 196 |
c.Assert(m("css"), qt.IsTrue) |
| 197 |
} |