2013-10-01 17:26:21 -04:00
|
|
|
|
package transform
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2015-02-12 06:17:59 -05:00
|
|
|
|
"strings"
|
2013-10-01 17:26:21 -04:00
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
2015-02-12 06:17:59 -05:00
|
|
|
|
const H5_JS_CONTENT_DOUBLE_QUOTE = "<!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>"
|
|
|
|
|
const H5_JS_CONTENT_SINGLE_QUOTE = "<!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>"
|
|
|
|
|
const H5_JS_CONTENT_ABS_URL = "<!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>"
|
|
|
|
|
const H5_JS_CONTENT_ABS_URL_SCHEMALESS = "<!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>"
|
|
|
|
|
const CORRECT_OUTPUT_SRC_HREF_DQ = "<!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>"
|
|
|
|
|
const CORRECT_OUTPUT_SRC_HREF_SQ = "<!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>"
|
2013-10-01 17:26:21 -04:00
|
|
|
|
|
2014-12-18 14:59:39 -05:00
|
|
|
|
const H5_XML_CONTENT_ABS_URL = "<?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>"
|
2015-02-16 04:48:15 -05:00
|
|
|
|
const CORRECT_OUTPUT_SRC_HREF_IN_XML = "<?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>"
|
2015-02-12 06:17:59 -05:00
|
|
|
|
const H5_XML_CONTENT_GUARDED = "<?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>"
|
2014-12-18 14:59:39 -05:00
|
|
|
|
|
2015-02-16 04:48:15 -05:00
|
|
|
|
// additional sanity tests for replacements testing
|
|
|
|
|
const REPLACE_1 = "No replacements."
|
|
|
|
|
const REPLACE_2 = "ᚠᛇᚻ ᛒᛦᚦ ᚠᚱᚩᚠᚢᚱ\nᚠᛁᚱᚪ ᚷᛖᚻᚹᛦᛚᚳᚢᛗ"
|
|
|
|
|
|
2015-02-16 22:33:44 -05:00
|
|
|
|
// Issue: 816, schemaless links combined with others
|
|
|
|
|
const REPLACE_SCHEMALESS_HTML = `Pre. src='//schemaless' src='/normal' <a href="//schemaless">Schemaless</a>. <a href="/normal">normal</a>. Post.`
|
|
|
|
|
const REPLACE_SCHEMALESS_HTML_CORRECT = `Pre. src='//schemaless' src='http://base/normal' <a href="//schemaless">Schemaless</a>. <a href="http://base/normal">normal</a>. Post.`
|
|
|
|
|
const REPLACE_SCHEMALESS_XML = `Pre. src="//schemaless" src="/normal" <a href='//schemaless'>Schemaless</a>. <a href='/normal'>normal</a>. Post.`
|
|
|
|
|
const REPLACE_SCHEMALESS_XML_CORRECT = `Pre. src="//schemaless" src="http://base/normal" <a href='//schemaless'>Schemaless</a>. <a href='http://base/normal'>normal</a>. Post.`
|
|
|
|
|
|
2015-02-16 04:48:15 -05:00
|
|
|
|
var abs_url_bench_tests = []test{
|
2015-02-12 06:17:59 -05:00
|
|
|
|
{H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_DQ},
|
|
|
|
|
{H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF_SQ},
|
|
|
|
|
{H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL},
|
|
|
|
|
{H5_JS_CONTENT_ABS_URL_SCHEMALESS, H5_JS_CONTENT_ABS_URL_SCHEMALESS},
|
2013-10-01 17:26:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 04:48:15 -05:00
|
|
|
|
var xml_abs_url_bench_tests = []test{
|
2014-12-18 14:59:39 -05:00
|
|
|
|
{H5_XML_CONTENT_ABS_URL, CORRECT_OUTPUT_SRC_HREF_IN_XML},
|
2015-02-12 06:17:59 -05:00
|
|
|
|
{H5_XML_CONTENT_GUARDED, H5_XML_CONTENT_GUARDED},
|
2014-12-18 14:59:39 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 04:48:15 -05:00
|
|
|
|
var sanity_tests = []test{{REPLACE_1, REPLACE_1}, {REPLACE_2, REPLACE_2}}
|
2015-02-16 22:33:44 -05:00
|
|
|
|
var extra_tests_html = []test{{REPLACE_SCHEMALESS_HTML, REPLACE_SCHEMALESS_HTML_CORRECT}}
|
|
|
|
|
var abs_url_tests = append(abs_url_bench_tests, append(sanity_tests, extra_tests_html...)...)
|
|
|
|
|
var extra_tests_xml = []test{{REPLACE_SCHEMALESS_XML, REPLACE_SCHEMALESS_XML_CORRECT}}
|
|
|
|
|
var xml_abs_url_tests = append(xml_abs_url_bench_tests, append(sanity_tests, extra_tests_xml...)...)
|
2015-02-16 04:48:15 -05:00
|
|
|
|
|
2013-11-05 17:28:06 -05:00
|
|
|
|
func TestChainZeroTransformers(t *testing.T) {
|
|
|
|
|
tr := NewChain()
|
|
|
|
|
in := new(bytes.Buffer)
|
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
|
if err := tr.Apply(in, out); err != nil {
|
|
|
|
|
t.Errorf("A zero transformer chain returned an error.")
|
|
|
|
|
}
|
2013-10-29 23:24:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 06:17:59 -05:00
|
|
|
|
func BenchmarkAbsUrl(b *testing.B) {
|
|
|
|
|
absURL, _ := AbsURL("http://base")
|
2013-11-05 17:28:06 -05:00
|
|
|
|
tr := NewChain(absURL...)
|
2013-11-01 01:14:11 -04:00
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
2013-10-29 23:24:29 -04:00
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-02-16 04:48:15 -05:00
|
|
|
|
apply(b.Errorf, tr, abs_url_bench_tests)
|
2015-02-12 06:17:59 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestAbsUrl(t *testing.T) {
|
|
|
|
|
absURL, _ := AbsURL("http://base")
|
|
|
|
|
tr := NewChain(absURL...)
|
|
|
|
|
|
|
|
|
|
apply(t.Errorf, tr, abs_url_tests)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BenchmarkXmlAbsUrl(b *testing.B) {
|
2015-02-16 04:48:15 -05:00
|
|
|
|
absURLInXML, _ := AbsURLInXML("http://base")
|
2015-02-12 06:17:59 -05:00
|
|
|
|
tr := NewChain(absURLInXML...)
|
|
|
|
|
|
|
|
|
|
b.ResetTimer()
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2015-02-16 04:48:15 -05:00
|
|
|
|
apply(b.Errorf, tr, xml_abs_url_bench_tests)
|
2013-10-29 23:24:29 -04:00
|
|
|
|
}
|
2013-10-01 17:26:21 -04:00
|
|
|
|
}
|
2014-12-18 14:59:39 -05:00
|
|
|
|
|
|
|
|
|
func TestXMLAbsUrl(t *testing.T) {
|
2015-02-16 04:48:15 -05:00
|
|
|
|
absURLInXML, _ := AbsURLInXML("http://base")
|
2014-12-18 14:59:39 -05:00
|
|
|
|
tr := NewChain(absURLInXML...)
|
|
|
|
|
apply(t.Errorf, tr, xml_abs_url_tests)
|
|
|
|
|
}
|
2015-02-12 06:17:59 -05:00
|
|
|
|
|
|
|
|
|
type errorf func(string, ...interface{})
|
|
|
|
|
|
|
|
|
|
func apply(ef errorf, tr chain, tests []test) {
|
|
|
|
|
for _, test := range tests {
|
|
|
|
|
out := new(bytes.Buffer)
|
|
|
|
|
err := tr.Apply(out, strings.NewReader(test.content))
|
|
|
|
|
if err != nil {
|
|
|
|
|
ef("Unexpected error: %s", err)
|
|
|
|
|
}
|
|
|
|
|
if test.expected != string(out.Bytes()) {
|
|
|
|
|
ef("Expected:\n%s\nGot:\n%s", test.expected, string(out.Bytes()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type test struct {
|
|
|
|
|
content string
|
|
|
|
|
expected string
|
|
|
|
|
}
|