| 1 |
// Copyright 2018 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 urlreplacers |
| 15 |
|
| 16 |
import ( |
| 17 |
"path/filepath" |
| 18 |
"testing" |
| 19 |
|
| 20 |
bp "github.com/gohugoio/hugo/bufferpool" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/helpers" |
| 23 |
"github.com/gohugoio/hugo/transform" |
| 24 |
) |
| 25 |
|
| 26 |
const ( |
| 27 |
h5JsContentDoubleQuote = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"/foobar\">Follow up</a></article></body></html>" |
| 28 |
h5JsContentSingleQuote = "<!DOCTYPE html><html><head><script src='foobar.js'></script><script src='/barfoo.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='foobar'>foobar</a>. <a href='/foobar'>Follow up</a></article></body></html>" |
| 29 |
h5JsContentAbsURL = "<!DOCTYPE html><html><head><script src=\"http://user@host:10234/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"https://host/foobar\">foobar</a>. Follow up</article></body></html>" |
| 30 |
h5JsContentAbsURLSchemaless = "<!DOCTYPE html><html><head><script src=\"//host/foobar.js\"></script><script src='//host2/barfoo.js'></head><body><nav><h1>title</h1></nav><article>content <a href=\"//host/foobar\">foobar</a>. <a href='//host2/foobar'>Follow up</a></article></body></html>" |
| 31 |
correctOutputSrcHrefDq = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script><script src=\"http://base/barfoo.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"foobar\">foobar</a>. <a href=\"http://base/foobar\">Follow up</a></article></body></html>" |
| 32 |
correctOutputSrcHrefSq = "<!DOCTYPE html><html><head><script src='foobar.js'></script><script src='http://base/barfoo.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='foobar'>foobar</a>. <a href='http://base/foobar'>Follow up</a></article></body></html>" |
| 33 |
|
| 34 |
h5XMLContentAbsURL = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="/foobar">foobar</a></p> <p>A video: <iframe src='/foo'></iframe></p></content></entry></feed>" |
| 35 |
correctOutputSrcHrefInXML = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="http://base/foobar">foobar</a></p> <p>A video: <iframe src='http://base/foo'></iframe></p></content></entry></feed>" |
| 36 |
h5XMLContentGuarded = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\"><p><a href="//foobar">foobar</a></p> <p>A video: <iframe src='//foo'></iframe></p></content></entry></feed>" |
| 37 |
) |
| 38 |
|
| 39 |
const ( |
| 40 |
// additional sanity tests for replacements testing |
| 41 |
replace1 = "No replacements." |
| 42 |
replace2 = "ᚠᛇᚻ ᛒᛦᚦ ᚠᚱᚩᚠᚢᚱ\nᚠᛁᚱᚪ ᚷᛖᚻᚹᛦᛚᚳᚢᛗ" |
| 43 |
replace3 = `End of file: src="/` |
| 44 |
replace5 = `Srcsett with no closing quote: srcset="/img/small.jpg do be do be do.` |
| 45 |
|
| 46 |
// Issue: 816, schemaless links combined with others |
| 47 |
replaceSchemalessHTML = `Pre. src='//schemaless' src='/normal' <a href="//schemaless">Schemaless</a>. <a href="/normal">normal</a>. Post.` |
| 48 |
replaceSchemalessHTMLCorrect = `Pre. src='//schemaless' src='http://base/normal' <a href="//schemaless">Schemaless</a>. <a href="http://base/normal">normal</a>. Post.` |
| 49 |
replaceSchemalessXML = `Pre. src='//schemaless' src='/normal' <a href='//schemaless'>Schemaless</a>. <a href='/normal'>normal</a>. Post.` |
| 50 |
replaceSchemalessXMLCorrect = `Pre. src='//schemaless' src='http://base/normal' <a href='//schemaless'>Schemaless</a>. <a href='http://base/normal'>normal</a>. Post.` |
| 51 |
) |
| 52 |
|
| 53 |
const ( |
| 54 |
// srcset= |
| 55 |
srcsetBasic = `Pre. <img srcset="/img/small.jpg 200w, /img/medium.jpg 300w, /img/big.jpg 700w" alt="text" src="/img/foo.jpg">` |
| 56 |
srcsetBasicCorrect = `Pre. <img srcset="http://base/img/small.jpg 200w, http://base/img/medium.jpg 300w, http://base/img/big.jpg 700w" alt="text" src="http://base/img/foo.jpg">` |
| 57 |
srcsetSingleQuote = `Pre. <img srcset='/img/small.jpg 200w, /img/big.jpg 700w' alt="text" src="/img/foo.jpg"> POST.` |
| 58 |
srcsetSingleQuoteCorrect = `Pre. <img srcset='http://base/img/small.jpg 200w, http://base/img/big.jpg 700w' alt="text" src="http://base/img/foo.jpg"> POST.` |
| 59 |
srcsetXMLBasic = `Pre. <img srcset="/img/small.jpg 200w, /img/big.jpg 700w" alt="text" src="/img/foo.jpg">` |
| 60 |
srcsetXMLBasicCorrect = `Pre. <img srcset="http://base/img/small.jpg 200w, http://base/img/big.jpg 700w" alt="text" src="http://base/img/foo.jpg">` |
| 61 |
srcsetXMLSingleQuote = `Pre. <img srcset="/img/small.jpg 200w, /img/big.jpg 700w" alt="text" src="/img/foo.jpg">` |
| 62 |
srcsetXMLSingleQuoteCorrect = `Pre. <img srcset="http://base/img/small.jpg 200w, http://base/img/big.jpg 700w" alt="text" src="http://base/img/foo.jpg">` |
| 63 |
srcsetVariations = `Pre. |
| 64 |
Missing start quote: <img srcset=/img/small.jpg 200w, /img/big.jpg 700w" alt="text"> src='/img/foo.jpg'> FOO. |
| 65 |
<img srcset='/img.jpg'> |
| 66 |
schemaless: <img srcset='//img.jpg' src='//basic.jpg'> |
| 67 |
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST |
| 68 |
` |
| 69 |
) |
| 70 |
|
| 71 |
const ( |
| 72 |
srcsetVariationsCorrect = `Pre. |
| 73 |
Missing start quote: <img srcset=/img/small.jpg 200w, /img/big.jpg 700w" alt="text"> src='http://base/img/foo.jpg'> FOO. |
| 74 |
<img srcset='http://base/img.jpg'> |
| 75 |
schemaless: <img srcset='//img.jpg' src='//basic.jpg'> |
| 76 |
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST |
| 77 |
` |
| 78 |
srcsetXMLVariations = `Pre. |
| 79 |
Missing start quote: <img srcset=/img/small.jpg 200w /img/big.jpg 700w" alt="text"> src='/img/foo.jpg'> FOO. |
| 80 |
<img srcset='/img.jpg'> |
| 81 |
schemaless: <img srcset='//img.jpg' src='//basic.jpg'> |
| 82 |
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST |
| 83 |
` |
| 84 |
srcsetXMLVariationsCorrect = `Pre. |
| 85 |
Missing start quote: <img srcset=/img/small.jpg 200w /img/big.jpg 700w" alt="text"> src='http://base/img/foo.jpg'> FOO. |
| 86 |
<img srcset='http://base/img.jpg'> |
| 87 |
schemaless: <img srcset='//img.jpg' src='//basic.jpg'> |
| 88 |
schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST |
| 89 |
` |
| 90 |
|
| 91 |
relPathVariations = `PRE. a href="/img/small.jpg" input action="/foo.html" meta url=/redirect/to/page/ POST.` |
| 92 |
relPathVariationsCorrect = `PRE. a href="../../img/small.jpg" input action="../../foo.html" meta url=../../redirect/to/page/ POST.` |
| 93 |
|
| 94 |
testBaseURL = "http://base/" |
| 95 |
) |
| 96 |
|
| 97 |
var ( |
| 98 |
absURLlBenchTests = []test{ |
| 99 |
{h5JsContentDoubleQuote, correctOutputSrcHrefDq}, |
| 100 |
{h5JsContentSingleQuote, correctOutputSrcHrefSq}, |
| 101 |
{h5JsContentAbsURL, h5JsContentAbsURL}, |
| 102 |
{h5JsContentAbsURLSchemaless, h5JsContentAbsURLSchemaless}, |
| 103 |
} |
| 104 |
|
| 105 |
xmlAbsURLBenchTests = []test{ |
| 106 |
{h5XMLContentAbsURL, correctOutputSrcHrefInXML}, |
| 107 |
{h5XMLContentGuarded, h5XMLContentGuarded}, |
| 108 |
} |
| 109 |
|
| 110 |
sanityTests = []test{{replace1, replace1}, {replace2, replace2}, {replace3, replace3}, {replace3, replace3}, {replace5, replace5}} |
| 111 |
extraTestsHTML = []test{{replaceSchemalessHTML, replaceSchemalessHTMLCorrect}} |
| 112 |
absURLTests = append(absURLlBenchTests, append(sanityTests, extraTestsHTML...)...) |
| 113 |
extraTestsXML = []test{{replaceSchemalessXML, replaceSchemalessXMLCorrect}} |
| 114 |
xmlAbsURLTests = append(xmlAbsURLBenchTests, append(sanityTests, extraTestsXML...)...) |
| 115 |
srcsetTests = []test{{srcsetBasic, srcsetBasicCorrect}, {srcsetSingleQuote, srcsetSingleQuoteCorrect}, {srcsetVariations, srcsetVariationsCorrect}} |
| 116 |
srcsetXMLTests = []test{ |
| 117 |
{srcsetXMLBasic, srcsetXMLBasicCorrect}, |
| 118 |
{srcsetXMLSingleQuote, srcsetXMLSingleQuoteCorrect}, |
| 119 |
{srcsetXMLVariations, srcsetXMLVariationsCorrect}, |
| 120 |
} |
| 121 |
|
| 122 |
relurlTests = []test{{relPathVariations, relPathVariationsCorrect}} |
| 123 |
) |
| 124 |
|
| 125 |
func BenchmarkAbsURL(b *testing.B) { |
| 126 |
tr := transform.New(NewAbsURLTransformer(testBaseURL)) |
| 127 |
|
| 128 |
for b.Loop() { |
| 129 |
apply(b.Errorf, tr, absURLlBenchTests) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func BenchmarkAbsURLSrcset(b *testing.B) { |
| 134 |
tr := transform.New(NewAbsURLTransformer(testBaseURL)) |
| 135 |
|
| 136 |
for b.Loop() { |
| 137 |
apply(b.Errorf, tr, srcsetTests) |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
func BenchmarkXMLAbsURLSrcset(b *testing.B) { |
| 142 |
tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) |
| 143 |
|
| 144 |
for b.Loop() { |
| 145 |
apply(b.Errorf, tr, srcsetXMLTests) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestAbsURL(t *testing.T) { |
| 150 |
tr := transform.New(NewAbsURLTransformer(testBaseURL)) |
| 151 |
|
| 152 |
apply(t.Errorf, tr, absURLTests) |
| 153 |
} |
| 154 |
|
| 155 |
func TestAbsURLUnquoted(t *testing.T) { |
| 156 |
tr := transform.New(NewAbsURLTransformer(testBaseURL)) |
| 157 |
|
| 158 |
apply(t.Errorf, tr, []test{ |
| 159 |
{ |
| 160 |
content: `Link: <a href=/asdf>ASDF</a>`, |
| 161 |
expected: `Link: <a href=http://base/asdf>ASDF</a>`, |
| 162 |
}, |
| 163 |
{ |
| 164 |
content: `Link: <a href=/asdf >ASDF</a>`, |
| 165 |
expected: `Link: <a href=http://base/asdf >ASDF</a>`, |
| 166 |
}, |
| 167 |
}) |
| 168 |
} |
| 169 |
|
| 170 |
func TestRelativeURL(t *testing.T) { |
| 171 |
tr := transform.New(NewAbsURLTransformer(helpers.GetDottedRelativePath(filepath.FromSlash("/post/sub/")))) |
| 172 |
|
| 173 |
applyWithPath(t.Errorf, tr, relurlTests) |
| 174 |
} |
| 175 |
|
| 176 |
func TestAbsURLSrcSet(t *testing.T) { |
| 177 |
tr := transform.New(NewAbsURLTransformer(testBaseURL)) |
| 178 |
|
| 179 |
apply(t.Errorf, tr, srcsetTests) |
| 180 |
} |
| 181 |
|
| 182 |
func TestAbsXMLURLSrcSet(t *testing.T) { |
| 183 |
tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) |
| 184 |
|
| 185 |
apply(t.Errorf, tr, srcsetXMLTests) |
| 186 |
} |
| 187 |
|
| 188 |
func BenchmarkXMLAbsURL(b *testing.B) { |
| 189 |
tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) |
| 190 |
|
| 191 |
for b.Loop() { |
| 192 |
apply(b.Errorf, tr, xmlAbsURLBenchTests) |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
func TestXMLAbsURL(t *testing.T) { |
| 197 |
tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) |
| 198 |
apply(t.Errorf, tr, xmlAbsURLTests) |
| 199 |
} |
| 200 |
|
| 201 |
func apply(ef errorf, tr transform.Chain, tests []test) { |
| 202 |
applyWithPath(ef, tr, tests) |
| 203 |
} |
| 204 |
|
| 205 |
func applyWithPath(ef errorf, tr transform.Chain, tests []test) { |
| 206 |
out := bp.GetBuffer() |
| 207 |
defer bp.PutBuffer(out) |
| 208 |
|
| 209 |
in := bp.GetBuffer() |
| 210 |
defer bp.PutBuffer(in) |
| 211 |
|
| 212 |
for _, test := range tests { |
| 213 |
var err error |
| 214 |
in.WriteString(test.content) |
| 215 |
err = tr.Apply(out, in) |
| 216 |
if err != nil { |
| 217 |
ef("Unexpected error: %s", err) |
| 218 |
} |
| 219 |
if test.expected != out.String() { |
| 220 |
ef("Expected:\n%s\nGot:\n%s", test.expected, out.String()) |
| 221 |
} |
| 222 |
out.Reset() |
| 223 |
in.Reset() |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
type test struct { |
| 228 |
content string |
| 229 |
expected string |
| 230 |
} |
| 231 |
|
| 232 |
type errorf func(string, ...any) |