gitee.com/mirrors/Hugo-Go@v0.47.1/transform/urlreplacers/absurlreplacer_test.go (about)

     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  	corectOutputSrcHrefDq       = "<!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  	corectOutputSrcHrefSq       = "<!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  	h5XMLXontentAbsURL        = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?><feed xmlns=\"http://www.w3.org/2005/Atom\"><entry><content type=\"html\">&lt;p&gt;&lt;a href=&#34;/foobar&#34;&gt;foobar&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A video: &lt;iframe src=&#39;/foo&#39;&gt;&lt;/iframe&gt;&lt;/p&gt;</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\">&lt;p&gt;&lt;a href=&#34;http://base/foobar&#34;&gt;foobar&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A video: &lt;iframe src=&#39;http://base/foo&#39;&gt;&lt;/iframe&gt;&lt;/p&gt;</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\">&lt;p&gt;&lt;a href=&#34;//foobar&#34;&gt;foobar&lt;/a&gt;&lt;/p&gt; &lt;p&gt;A video: &lt;iframe src=&#39;//foo&#39;&gt;&lt;/iframe&gt;&lt;/p&gt;</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  	replace4 = `End of file: srcset="/`
    45  	replace5 = `Srcsett with no closing quote: srcset="/img/small.jpg do be do be do.`
    46  
    47  	// Issue: 816, schemaless links combined with others
    48  	replaceSchemalessHTML        = `Pre. src='//schemaless' src='/normal'  <a href="//schemaless">Schemaless</a>. <a href="/normal">normal</a>. Post.`
    49  	replaceSchemalessHTMLCorrect = `Pre. src='//schemaless' src='http://base/normal'  <a href="//schemaless">Schemaless</a>. <a href="http://base/normal">normal</a>. Post.`
    50  	replaceSchemalessXML         = `Pre. src=&#39;//schemaless&#39; src=&#39;/normal&#39;  <a href=&#39;//schemaless&#39;>Schemaless</a>. <a href=&#39;/normal&#39;>normal</a>. Post.`
    51  	replaceSchemalessXMLCorrect  = `Pre. src=&#39;//schemaless&#39; src=&#39;http://base/normal&#39;  <a href=&#39;//schemaless&#39;>Schemaless</a>. <a href=&#39;http://base/normal&#39;>normal</a>. Post.`
    52  )
    53  
    54  const (
    55  	// srcset=
    56  	srcsetBasic                 = `Pre. <img srcset="/img/small.jpg 200w, /img/medium.jpg 300w, /img/big.jpg 700w" alt="text" src="/img/foo.jpg">`
    57  	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">`
    58  	srcsetSingleQuote           = `Pre. <img srcset='/img/small.jpg 200w, /img/big.jpg 700w' alt="text" src="/img/foo.jpg"> POST.`
    59  	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.`
    60  	srcsetXMLBasic              = `Pre. <img srcset=&#34;/img/small.jpg 200w, /img/big.jpg 700w&#34; alt=&#34;text&#34; src=&#34;/img/foo.jpg&#34;>`
    61  	srcsetXMLBasicCorrect       = `Pre. <img srcset=&#34;http://base/img/small.jpg 200w, http://base/img/big.jpg 700w&#34; alt=&#34;text&#34; src=&#34;http://base/img/foo.jpg&#34;>`
    62  	srcsetXMLSingleQuote        = `Pre. <img srcset=&#34;/img/small.jpg 200w, /img/big.jpg 700w&#34; alt=&#34;text&#34; src=&#34;/img/foo.jpg&#34;>`
    63  	srcsetXMLSingleQuoteCorrect = `Pre. <img srcset=&#34;http://base/img/small.jpg 200w, http://base/img/big.jpg 700w&#34; alt=&#34;text&#34; src=&#34;http://base/img/foo.jpg&#34;>`
    64  	srcsetVariations            = `Pre. 
    65  Missing start quote: <img srcset=/img/small.jpg 200w, /img/big.jpg 700w" alt="text"> src='/img/foo.jpg'> FOO. 
    66  <img srcset='/img.jpg'> 
    67  schemaless: <img srcset='//img.jpg' src='//basic.jpg'>
    68  schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST
    69  `
    70  )
    71  
    72  const (
    73  	srcsetVariationsCorrect = `Pre. 
    74  Missing start quote: <img srcset=/img/small.jpg 200w, /img/big.jpg 700w" alt="text"> src='http://base/img/foo.jpg'> FOO. 
    75  <img srcset='http://base/img.jpg'> 
    76  schemaless: <img srcset='//img.jpg' src='//basic.jpg'>
    77  schemaless2: <img srcset="//img.jpg" src="//basic.jpg2> POST
    78  `
    79  	srcsetXMLVariations = `Pre. 
    80  Missing start quote: &lt;img srcset=/img/small.jpg 200w /img/big.jpg 700w&quot; alt=&quot;text&quot;&gt; src=&#39;/img/foo.jpg&#39;&gt; FOO. 
    81  &lt;img srcset=&#39;/img.jpg&#39;&gt; 
    82  schemaless: &lt;img srcset=&#39;//img.jpg&#39; src=&#39;//basic.jpg&#39;&gt;
    83  schemaless2: &lt;img srcset=&quot;//img.jpg&quot; src=&quot;//basic.jpg2&gt; POST
    84  `
    85  	srcsetXMLVariationsCorrect = `Pre. 
    86  Missing start quote: &lt;img srcset=/img/small.jpg 200w /img/big.jpg 700w&quot; alt=&quot;text&quot;&gt; src=&#39;http://base/img/foo.jpg&#39;&gt; FOO. 
    87  &lt;img srcset=&#39;http://base/img.jpg&#39;&gt; 
    88  schemaless: &lt;img srcset=&#39;//img.jpg&#39; src=&#39;//basic.jpg&#39;&gt;
    89  schemaless2: &lt;img srcset=&quot;//img.jpg&quot; src=&quot;//basic.jpg2&gt; POST
    90  `
    91  
    92  	relPathVariations        = `PRE. a href="/img/small.jpg" POST.`
    93  	relPathVariationsCorrect = `PRE. a href="../../img/small.jpg" POST.`
    94  
    95  	testBaseURL = "http://base/"
    96  )
    97  
    98  var (
    99  	absURLlBenchTests = []test{
   100  		{h5JsContentDoubleQuote, corectOutputSrcHrefDq},
   101  		{h5JsContentSingleQuote, corectOutputSrcHrefSq},
   102  		{h5JsContentAbsURL, h5JsContentAbsURL},
   103  		{h5JsContentAbsURLSchemaless, h5JsContentAbsURLSchemaless},
   104  	}
   105  
   106  	xmlAbsURLBenchTests = []test{
   107  		{h5XMLXontentAbsURL, correctOutputSrcHrefInXML},
   108  		{h5XMLContentGuarded, h5XMLContentGuarded},
   109  	}
   110  
   111  	sanityTests    = []test{{replace1, replace1}, {replace2, replace2}, {replace3, replace3}, {replace3, replace3}, {replace5, replace5}}
   112  	extraTestsHTML = []test{{replaceSchemalessHTML, replaceSchemalessHTMLCorrect}}
   113  	absURLTests    = append(absURLlBenchTests, append(sanityTests, extraTestsHTML...)...)
   114  	extraTestsXML  = []test{{replaceSchemalessXML, replaceSchemalessXMLCorrect}}
   115  	xmlAbsURLTests = append(xmlAbsURLBenchTests, append(sanityTests, extraTestsXML...)...)
   116  	srcsetTests    = []test{{srcsetBasic, srcsetBasicCorrect}, {srcsetSingleQuote, srcsetSingleQuoteCorrect}, {srcsetVariations, srcsetVariationsCorrect}}
   117  	srcsetXMLTests = []test{
   118  		{srcsetXMLBasic, srcsetXMLBasicCorrect},
   119  		{srcsetXMLSingleQuote, srcsetXMLSingleQuoteCorrect},
   120  		{srcsetXMLVariations, srcsetXMLVariationsCorrect}}
   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  
   159  func TestRelativeURL(t *testing.T) {
   160  	tr := transform.New(NewAbsURLTransformer(helpers.GetDottedRelativePath(filepath.FromSlash("/post/sub/"))))
   161  
   162  	applyWithPath(t.Errorf, tr, relurlTests)
   163  
   164  }
   165  
   166  func TestAbsURLSrcSet(t *testing.T) {
   167  	tr := transform.New(NewAbsURLTransformer(testBaseURL))
   168  
   169  	apply(t.Errorf, tr, srcsetTests)
   170  }
   171  
   172  func TestAbsXMLURLSrcSet(t *testing.T) {
   173  	tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL))
   174  
   175  	apply(t.Errorf, tr, srcsetXMLTests)
   176  }
   177  
   178  func BenchmarkXMLAbsURL(b *testing.B) {
   179  	tr := transform.New(NewAbsURLInXMLTransformer(""))
   180  
   181  	b.ResetTimer()
   182  	for i := 0; i < b.N; i++ {
   183  		apply(b.Errorf, tr, xmlAbsURLBenchTests)
   184  	}
   185  }
   186  
   187  func TestXMLAbsURL(t *testing.T) {
   188  	tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL))
   189  	apply(t.Errorf, tr, xmlAbsURLTests)
   190  }
   191  
   192  func apply(ef errorf, tr transform.Chain, tests []test) {
   193  	applyWithPath(ef, tr, tests)
   194  }
   195  
   196  func applyWithPath(ef errorf, tr transform.Chain, tests []test) {
   197  	out := bp.GetBuffer()
   198  	defer bp.PutBuffer(out)
   199  
   200  	in := bp.GetBuffer()
   201  	defer bp.PutBuffer(in)
   202  
   203  	for _, test := range tests {
   204  		var err error
   205  		in.WriteString(test.content)
   206  		err = tr.Apply(out, in)
   207  		if err != nil {
   208  			ef("Unexpected error: %s", err)
   209  		}
   210  		if test.expected != out.String() {
   211  			ef("Expected:\n%s\nGot:\n%s", test.expected, out.String())
   212  		}
   213  		out.Reset()
   214  		in.Reset()
   215  	}
   216  }
   217  
   218  type test struct {
   219  	content  string
   220  	expected string
   221  }
   222  
   223  type errorf func(string, ...interface{})