absurlreplacer_test.go (11257B)
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 b.ResetTimer() 129 for i := 0; i < b.N; i++ { 130 apply(b.Errorf, tr, absURLlBenchTests) 131 } 132 } 133 134 func BenchmarkAbsURLSrcset(b *testing.B) { 135 tr := transform.New(NewAbsURLTransformer(testBaseURL)) 136 137 b.ResetTimer() 138 for i := 0; i < b.N; i++ { 139 apply(b.Errorf, tr, srcsetTests) 140 } 141 } 142 143 func BenchmarkXMLAbsURLSrcset(b *testing.B) { 144 tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) 145 146 b.ResetTimer() 147 for i := 0; i < b.N; i++ { 148 apply(b.Errorf, tr, srcsetXMLTests) 149 } 150 } 151 152 func TestAbsURL(t *testing.T) { 153 tr := transform.New(NewAbsURLTransformer(testBaseURL)) 154 155 apply(t.Errorf, tr, absURLTests) 156 } 157 158 func TestAbsURLUnquoted(t *testing.T) { 159 tr := transform.New(NewAbsURLTransformer(testBaseURL)) 160 161 apply(t.Errorf, tr, []test{ 162 { 163 content: `Link: <a href=/asdf>ASDF</a>`, 164 expected: `Link: <a href=http://base/asdf>ASDF</a>`, 165 }, 166 { 167 content: `Link: <a href=/asdf >ASDF</a>`, 168 expected: `Link: <a href=http://base/asdf >ASDF</a>`, 169 }, 170 }) 171 } 172 173 func TestRelativeURL(t *testing.T) { 174 tr := transform.New(NewAbsURLTransformer(helpers.GetDottedRelativePath(filepath.FromSlash("/post/sub/")))) 175 176 applyWithPath(t.Errorf, tr, relurlTests) 177 } 178 179 func TestAbsURLSrcSet(t *testing.T) { 180 tr := transform.New(NewAbsURLTransformer(testBaseURL)) 181 182 apply(t.Errorf, tr, srcsetTests) 183 } 184 185 func TestAbsXMLURLSrcSet(t *testing.T) { 186 tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) 187 188 apply(t.Errorf, tr, srcsetXMLTests) 189 } 190 191 func BenchmarkXMLAbsURL(b *testing.B) { 192 tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) 193 194 b.ResetTimer() 195 for i := 0; i < b.N; i++ { 196 apply(b.Errorf, tr, xmlAbsURLBenchTests) 197 } 198 } 199 200 func TestXMLAbsURL(t *testing.T) { 201 tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL)) 202 apply(t.Errorf, tr, xmlAbsURLTests) 203 } 204 205 func apply(ef errorf, tr transform.Chain, tests []test) { 206 applyWithPath(ef, tr, tests) 207 } 208 209 func applyWithPath(ef errorf, tr transform.Chain, tests []test) { 210 out := bp.GetBuffer() 211 defer bp.PutBuffer(out) 212 213 in := bp.GetBuffer() 214 defer bp.PutBuffer(in) 215 216 for _, test := range tests { 217 var err error 218 in.WriteString(test.content) 219 err = tr.Apply(out, in) 220 if err != nil { 221 ef("Unexpected error: %s", err) 222 } 223 if test.expected != out.String() { 224 ef("Expected:\n%s\nGot:\n%s", test.expected, out.String()) 225 } 226 out.Reset() 227 in.Reset() 228 } 229 } 230 231 type test struct { 232 content string 233 expected string 234 } 235 236 type errorf func(string, ...any)