alias_test.go - hugo - [fork] hugo port for 9front
HTML git clone https://git.drkhsh.at/hugo.git
DIR Log
DIR Files
DIR Refs
DIR Submodules
DIR README
DIR LICENSE
---
alias_test.go (5938B)
---
1 // Copyright 2019 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 hugolib
15
16 import (
17 "os"
18 "path/filepath"
19 "runtime"
20 "testing"
21
22 qt "github.com/frankban/quicktest"
23 "github.com/gohugoio/hugo/common/loggers"
24 )
25
26 const pageWithAlias = `---
27 title: Has Alias
28 aliases: ["/foo/bar/", "rel"]
29 ---
30 For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
31 `
32
33 const pageWithAliasMultipleOutputs = `---
34 title: Has Alias for HTML and AMP
35 aliases: ["/foo/bar/"]
36 outputs: ["HTML", "AMP", "JSON"]
37 ---
38 For some moments the old man did not reply. He stood with bowed head, buried in deep thought. But at last he spoke.
39 `
40
41 const (
42 basicTemplate = "<html><body>{{.Content}}</body></html>"
43 aliasTemplate = "<html><body>ALIASTEMPLATE</body></html>"
44 )
45
46 func TestAlias(t *testing.T) {
47 t.Parallel()
48 c := qt.New(t)
49
50 tests := []struct {
51 fileSuffix string
52 urlPrefix string
53 urlSuffix string
54 settings map[string]any
55 }{
56 {"/index.html", "http://example.com", "/", map[string]any{"baseURL": "http://example.com"}},
57 {"/index.html", "http://example.com/some/path", "/", map[string]any{"baseURL": "http://example.com/some/path"}},
58 {"/index.html", "http://example.com", "/", map[string]any{"baseURL": "http://example.com", "canonifyURLs": true}},
59 {"/index.html", "../..", "/", map[string]any{"relativeURLs": true}},
60 {".html", "", ".html", map[string]any{"uglyURLs": true}},
61 }
62
63 for _, test := range tests {
64 b := newTestSitesBuilder(t)
65 b.WithSimpleConfigFileAndSettings(test.settings).WithContent("blog/page.md", pageWithAlias)
66 b.CreateSites().Build(BuildCfg{})
67
68 c.Assert(len(b.H.Sites), qt.Equals, 1)
69 c.Assert(len(b.H.Sites[0].RegularPages()), qt.Equals, 1)
70
71 // the real page
72 b.AssertFileContent("public/blog/page"+test.fileSuffix, "For some moments the old man")
73 // the alias redirectors
74 b.AssertFileContent("public/foo/bar"+test.fileSuffix, "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page"+test.urlSuffix+"\">")
75 b.AssertFileContent("public/blog/rel"+test.fileSuffix, "<meta http-equiv=\"refresh\" content=\"0; url="+test.urlPrefix+"/blog/page"+test.urlSuffix+"\">")
76 }
77 }
78
79 func TestAliasMultipleOutputFormats(t *testing.T) {
80 t.Parallel()
81
82 c := qt.New(t)
83
84 b := newTestSitesBuilder(t)
85 b.WithSimpleConfigFile().WithContent("blog/page.md", pageWithAliasMultipleOutputs)
86
87 b.WithTemplates(
88 "_default/single.html", basicTemplate,
89 "_default/single.amp.html", basicTemplate,
90 "_default/single.json", basicTemplate)
91
92 b.CreateSites().Build(BuildCfg{})
93
94 b.H.Sites[0].pageMap.debugPrint("", 999, os.Stdout)
95
96 // the real pages
97 b.AssertFileContent("public/blog/page/index.html", "For some moments the old man")
98 b.AssertFileContent("public/amp/blog/page/index.html", "For some moments the old man")
99 b.AssertFileContent("public/blog/page/index.json", "For some moments the old man")
100
101 // the alias redirectors
102 b.AssertFileContent("public/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
103 b.AssertFileContent("public/amp/foo/bar/index.html", "<meta http-equiv=\"refresh\" content=\"0; ")
104 c.Assert(b.CheckExists("public/foo/bar/index.json"), qt.Equals, false)
105 }
106
107 func TestAliasTemplate(t *testing.T) {
108 t.Parallel()
109
110 files := `
111 -- hugo.toml --
112 baseURL = "http://example.com"
113 -- layouts/single.html --
114 Single.
115 -- layouts/home.html --
116 Home.
117 -- layouts/alias.html --
118 ALIASTEMPLATE
119 -- content/page.md --
120 ---
121 title: "Page"
122 aliases: ["/foo/bar/"]
123 ---
124 `
125
126 b := Test(t, files)
127
128 // the real page
129 b.AssertFileContent("public/page/index.html", "Single.")
130 // the alias redirector
131 b.AssertFileContent("public/foo/bar/index.html", "ALIASTEMPLATE")
132 }
133
134 func TestTargetPathHTMLRedirectAlias(t *testing.T) {
135 h := newAliasHandler(nil, loggers.NewDefault(), false)
136
137 errIsNilForThisOS := runtime.GOOS != "windows"
138
139 tests := []struct {
140 value string
141 expected string
142 errIsNil bool
143 }{
144 {"", "", false},
145 {"s", filepath.FromSlash("s/index.html"), true},
146 {"/", "", false},
147 {"alias 1", filepath.FromSlash("alias 1/index.html"), true},
148 {"alias 2/", filepath.FromSlash("alias 2/index.html"), true},
149 {"alias 3.html", "alias 3.html", true},
150 {"alias4.html", "alias4.html", true},
151 {"/alias 5.html", "alias 5.html", true},
152 {"/трям.html", "трям.html", true},
153 {"../../../../tmp/passwd", "", false},
154 {"/foo/../../../../tmp/passwd", filepath.FromSlash("tmp/passwd/index.html"), true},
155 {"foo/../../../../tmp/passwd", "", false},
156 {"C:\\Windows", filepath.FromSlash("C:\\Windows/index.html"), errIsNilForThisOS},
157 {"/trailing-space /", filepath.FromSlash("trailing-space /index.html"), errIsNilForThisOS},
158 {"/trailing-period./", filepath.FromSlash("trailing-period./index.html"), errIsNilForThisOS},
159 {"/tab\tseparated/", filepath.FromSlash("tab\tseparated/index.html"), errIsNilForThisOS},
160 {"/chrome/?p=help&ctx=keyboard#topic=3227046", filepath.FromSlash("chrome/?p=help&ctx=keyboard#topic=3227046/index.html"), errIsNilForThisOS},
161 {"/LPT1/Printer/", filepath.FromSlash("LPT1/Printer/index.html"), errIsNilForThisOS},
162 }
163
164 for _, test := range tests {
165 path, err := h.targetPathAlias(test.value)
166 if (err == nil) != test.errIsNil {
167 t.Errorf("Expected err == nil => %t, got: %t. err: %s", test.errIsNil, err == nil, err)
168 continue
169 }
170 if err == nil && path != test.expected {
171 t.Errorf("Expected: %q, got: %q", test.expected, path)
172 }
173 }
174 }