package transform import ( "bytes" "strings" "testing" ) const H5_JS_CONTENT_DOUBLE_QUOTE = "
content foobar. Follow up
" const H5_JS_CONTENT_SINGLE_QUOTE = "
content foobar. Follow up
" const H5_JS_CONTENT_ABS_URL = "
content foobar. Follow up
" const H5_JS_CONTENT_ABS_URL_SCHEMALESS = "
content foobar. Follow up
" const CORRECT_OUTPUT_SRC_HREF_SQ = "
content foobar. Follow up
" const H5_XML_CONTENT_ABS_URL = "<p><a href="/foobar">foobar</a></p> <p>A video: <iframe src='/foo'></iframe></p>" const CORRECT_OUTPUT_SRC_HREF_IN_XML = "<p><a href="http://base/foobar">foobar</a></p> <p>A video: <iframe src='http://base/foo'></iframe></p>" const H5_XML_CONTENT_GUARDED = "<p><a href="//foobar">foobar</a></p> <p>A video: <iframe src='//foo'></iframe></p>" // additional sanity tests for replacements testing const REPLACE_1 = "No replacements." const REPLACE_2 = "ᚠᛇᚻ ᛒᛦᚦ ᚠᚱᚩᚠᚢᚱ\nᚠᛁᚱᚪ ᚷᛖᚻᚹᛦᛚᚳᚢᛗ" // Issue: 816, schemaless links combined with others const REPLACE_SCHEMALESS_HTML = `Pre. src='//schemaless' src='/normal' Schemaless. normal. Post.` const REPLACE_SCHEMALESS_HTML_CORRECT = `Pre. src='//schemaless' src='http://base/normal' Schemaless. normal. Post.` const REPLACE_SCHEMALESS_XML = `Pre. src="//schemaless" src="/normal" Schemaless. normal. Post.` const REPLACE_SCHEMALESS_XML_CORRECT = `Pre. src="//schemaless" src="http://base/normal" Schemaless. normal. Post.` var abs_url_bench_tests = []test{ {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}, } var xml_abs_url_bench_tests = []test{ {H5_XML_CONTENT_ABS_URL, CORRECT_OUTPUT_SRC_HREF_IN_XML}, {H5_XML_CONTENT_GUARDED, H5_XML_CONTENT_GUARDED}, } var sanity_tests = []test{{REPLACE_1, REPLACE_1}, {REPLACE_2, REPLACE_2}} 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...)...) 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.") } } func BenchmarkAbsUrl(b *testing.B) { absURL, _ := AbsURL("http://base") tr := NewChain(absURL...) b.ResetTimer() for i := 0; i < b.N; i++ { apply(b.Errorf, tr, abs_url_bench_tests) } } func TestAbsUrl(t *testing.T) { absURL, _ := AbsURL("http://base") tr := NewChain(absURL...) apply(t.Errorf, tr, abs_url_tests) } func BenchmarkXmlAbsUrl(b *testing.B) { absURLInXML, _ := AbsURLInXML("http://base") tr := NewChain(absURLInXML...) b.ResetTimer() for i := 0; i < b.N; i++ { apply(b.Errorf, tr, xml_abs_url_bench_tests) } } func TestXMLAbsUrl(t *testing.T) { absURLInXML, _ := AbsURLInXML("http://base") tr := NewChain(absURLInXML...) apply(t.Errorf, tr, xml_abs_url_tests) } 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 }