"Fossies" - the Fresh Open Source Software Archive

Member "hugo-0.63.2/transform/urlreplacers/absurlreplacer_test.go" (27 Jan 2020, 11212 Bytes) of package /linux/www/hugo-0.63.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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     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=&#39;//schemaless&#39; src=&#39;/normal&#39;  <a href=&#39;//schemaless&#39;>Schemaless</a>. <a href=&#39;/normal&#39;>normal</a>. Post.`
   50     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.`
   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=&#34;/img/small.jpg 200w, /img/big.jpg 700w&#34; alt=&#34;text&#34; src=&#34;/img/foo.jpg&#34;>`
   60     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;>`
   61     srcsetXMLSingleQuote        = `Pre. <img srcset=&#34;/img/small.jpg 200w, /img/big.jpg 700w&#34; alt=&#34;text&#34; src=&#34;/img/foo.jpg&#34;>`
   62     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;>`
   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: &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. 
   80 &lt;img srcset=&#39;/img.jpg&#39;&gt; 
   81 schemaless: &lt;img srcset=&#39;//img.jpg&#39; src=&#39;//basic.jpg&#39;&gt;
   82 schemaless2: &lt;img srcset=&quot;//img.jpg&quot; src=&quot;//basic.jpg2&gt; POST
   83 `
   84     srcsetXMLVariationsCorrect = `Pre. 
   85 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. 
   86 &lt;img srcset=&#39;http://base/img.jpg&#39;&gt; 
   87 schemaless: &lt;img srcset=&#39;//img.jpg&#39; src=&#39;//basic.jpg&#39;&gt;
   88 schemaless2: &lt;img srcset=&quot;//img.jpg&quot; src=&quot;//basic.jpg2&gt; POST
   89 `
   90 
   91     relPathVariations        = `PRE. a href="/img/small.jpg" input action="/foo.html" POST.`
   92     relPathVariationsCorrect = `PRE. a href="../../img/small.jpg" input action="../../foo.html" POST.`
   93 
   94     testBaseURL = "http://base/"
   95 )
   96 
   97 var (
   98     absURLlBenchTests = []test{
   99         {h5JsContentDoubleQuote, corectOutputSrcHrefDq},
  100         {h5JsContentSingleQuote, corectOutputSrcHrefSq},
  101         {h5JsContentAbsURL, h5JsContentAbsURL},
  102         {h5JsContentAbsURLSchemaless, h5JsContentAbsURLSchemaless},
  103     }
  104 
  105     xmlAbsURLBenchTests = []test{
  106         {h5XMLXontentAbsURL, 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     relurlTests = []test{{relPathVariations, relPathVariationsCorrect}}
  122 )
  123 
  124 func BenchmarkAbsURL(b *testing.B) {
  125     tr := transform.New(NewAbsURLTransformer(testBaseURL))
  126 
  127     b.ResetTimer()
  128     for i := 0; i < b.N; i++ {
  129         apply(b.Errorf, tr, absURLlBenchTests)
  130     }
  131 }
  132 
  133 func BenchmarkAbsURLSrcset(b *testing.B) {
  134     tr := transform.New(NewAbsURLTransformer(testBaseURL))
  135 
  136     b.ResetTimer()
  137     for i := 0; i < b.N; i++ {
  138         apply(b.Errorf, tr, srcsetTests)
  139     }
  140 }
  141 
  142 func BenchmarkXMLAbsURLSrcset(b *testing.B) {
  143     tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL))
  144 
  145     b.ResetTimer()
  146     for i := 0; i < b.N; i++ {
  147         apply(b.Errorf, tr, srcsetXMLTests)
  148     }
  149 }
  150 
  151 func TestAbsURL(t *testing.T) {
  152     tr := transform.New(NewAbsURLTransformer(testBaseURL))
  153 
  154     apply(t.Errorf, tr, absURLTests)
  155 
  156 }
  157 
  158 func TestAbsURLUnqoted(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 
  180 func TestAbsURLSrcSet(t *testing.T) {
  181     tr := transform.New(NewAbsURLTransformer(testBaseURL))
  182 
  183     apply(t.Errorf, tr, srcsetTests)
  184 }
  185 
  186 func TestAbsXMLURLSrcSet(t *testing.T) {
  187     tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL))
  188 
  189     apply(t.Errorf, tr, srcsetXMLTests)
  190 }
  191 
  192 func BenchmarkXMLAbsURL(b *testing.B) {
  193     tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL))
  194 
  195     b.ResetTimer()
  196     for i := 0; i < b.N; i++ {
  197         apply(b.Errorf, tr, xmlAbsURLBenchTests)
  198     }
  199 }
  200 
  201 func TestXMLAbsURL(t *testing.T) {
  202     tr := transform.New(NewAbsURLInXMLTransformer(testBaseURL))
  203     apply(t.Errorf, tr, xmlAbsURLTests)
  204 }
  205 
  206 func apply(ef errorf, tr transform.Chain, tests []test) {
  207     applyWithPath(ef, tr, tests)
  208 }
  209 
  210 func applyWithPath(ef errorf, tr transform.Chain, tests []test) {
  211     out := bp.GetBuffer()
  212     defer bp.PutBuffer(out)
  213 
  214     in := bp.GetBuffer()
  215     defer bp.PutBuffer(in)
  216 
  217     for _, test := range tests {
  218         var err error
  219         in.WriteString(test.content)
  220         err = tr.Apply(out, in)
  221         if err != nil {
  222             ef("Unexpected error: %s", err)
  223         }
  224         if test.expected != out.String() {
  225             ef("Expected:\n%s\nGot:\n%s", test.expected, out.String())
  226         }
  227         out.Reset()
  228         in.Reset()
  229     }
  230 }
  231 
  232 type test struct {
  233     content  string
  234     expected string
  235 }
  236 
  237 type errorf func(string, ...interface{})