github.com/olliephillips/hugo@v0.42.2/transform/chain_test.go (about)

     1  // Copyright 2015 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 transform
    15  
    16  import (
    17  	"bytes"
    18  	"path/filepath"
    19  	"strings"
    20  	"testing"
    21  
    22  	"github.com/gohugoio/hugo/helpers"
    23  	"github.com/stretchr/testify/assert"
    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 TestChainZeroTransformers(t *testing.T) {
   126  	tr := NewChain()
   127  	in := new(bytes.Buffer)
   128  	out := new(bytes.Buffer)
   129  	if err := tr.Apply(in, out, []byte("")); err != nil {
   130  		t.Errorf("A zero transformer chain returned an error.")
   131  	}
   132  }
   133  
   134  func TestChaingMultipleTransformers(t *testing.T) {
   135  	f1 := func(ct contentTransformer) {
   136  		ct.Write(bytes.Replace(ct.Content(), []byte("f1"), []byte("f1r"), -1))
   137  	}
   138  	f2 := func(ct contentTransformer) {
   139  		ct.Write(bytes.Replace(ct.Content(), []byte("f2"), []byte("f2r"), -1))
   140  	}
   141  	f3 := func(ct contentTransformer) {
   142  		ct.Write(bytes.Replace(ct.Content(), []byte("f3"), []byte("f3r"), -1))
   143  	}
   144  
   145  	f4 := func(ct contentTransformer) {
   146  		ct.Write(bytes.Replace(ct.Content(), []byte("f4"), []byte("f4r"), -1))
   147  	}
   148  
   149  	tr := NewChain(f1, f2, f3, f4)
   150  
   151  	out := new(bytes.Buffer)
   152  	if err := tr.Apply(out, strings.NewReader("Test: f4 f3 f1 f2 f1 The End."), []byte("")); err != nil {
   153  		t.Errorf("Multi transformer chain returned an error: %s", err)
   154  	}
   155  
   156  	expected := "Test: f4r f3r f1r f2r f1r The End."
   157  
   158  	if string(out.Bytes()) != expected {
   159  		t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
   160  	}
   161  }
   162  
   163  func BenchmarkAbsURL(b *testing.B) {
   164  	tr := NewChain(AbsURL)
   165  
   166  	b.ResetTimer()
   167  	for i := 0; i < b.N; i++ {
   168  		apply(b.Errorf, tr, absURLlBenchTests)
   169  	}
   170  }
   171  
   172  func BenchmarkAbsURLSrcset(b *testing.B) {
   173  	tr := NewChain(AbsURL)
   174  
   175  	b.ResetTimer()
   176  	for i := 0; i < b.N; i++ {
   177  		apply(b.Errorf, tr, srcsetTests)
   178  	}
   179  }
   180  
   181  func BenchmarkXMLAbsURLSrcset(b *testing.B) {
   182  	tr := NewChain(AbsURLInXML)
   183  
   184  	b.ResetTimer()
   185  	for i := 0; i < b.N; i++ {
   186  		apply(b.Errorf, tr, srcsetXMLTests)
   187  	}
   188  }
   189  
   190  func TestAbsURL(t *testing.T) {
   191  	tr := NewChain(AbsURL)
   192  
   193  	apply(t.Errorf, tr, absURLTests)
   194  
   195  }
   196  
   197  func TestRelativeURL(t *testing.T) {
   198  	tr := NewChain(AbsURL)
   199  
   200  	applyWithPath(t.Errorf, tr, relurlTests, helpers.GetDottedRelativePath(filepath.FromSlash("/post/sub/")))
   201  
   202  }
   203  
   204  func TestAbsURLSrcSet(t *testing.T) {
   205  	tr := NewChain(AbsURL)
   206  
   207  	apply(t.Errorf, tr, srcsetTests)
   208  }
   209  
   210  func TestAbsXMLURLSrcSet(t *testing.T) {
   211  	tr := NewChain(AbsURLInXML)
   212  
   213  	apply(t.Errorf, tr, srcsetXMLTests)
   214  }
   215  
   216  func BenchmarkXMLAbsURL(b *testing.B) {
   217  	tr := NewChain(AbsURLInXML)
   218  
   219  	b.ResetTimer()
   220  	for i := 0; i < b.N; i++ {
   221  		apply(b.Errorf, tr, xmlAbsURLBenchTests)
   222  	}
   223  }
   224  
   225  func TestXMLAbsURL(t *testing.T) {
   226  	tr := NewChain(AbsURLInXML)
   227  	apply(t.Errorf, tr, xmlAbsURLTests)
   228  }
   229  
   230  func TestNewEmptyTransforms(t *testing.T) {
   231  	transforms := NewEmptyTransforms()
   232  	assert.Equal(t, 20, cap(transforms))
   233  }
   234  
   235  type errorf func(string, ...interface{})
   236  
   237  func applyWithPath(ef errorf, tr chain, tests []test, path string) {
   238  	for _, test := range tests {
   239  		out := new(bytes.Buffer)
   240  		var err error
   241  		err = tr.Apply(out, strings.NewReader(test.content), []byte(path))
   242  		if err != nil {
   243  			ef("Unexpected error: %s", err)
   244  		}
   245  		if test.expected != string(out.Bytes()) {
   246  			ef("Expected:\n%s\nGot:\n%s", test.expected, string(out.Bytes()))
   247  		}
   248  	}
   249  }
   250  
   251  func apply(ef errorf, tr chain, tests []test) {
   252  	applyWithPath(ef, tr, tests, testBaseURL)
   253  }
   254  
   255  type test struct {
   256  	content  string
   257  	expected string
   258  }