2013-09-17 16:04:28 -04:00
|
|
|
package transform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2013-09-18 12:15:46 -04:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2013-09-17 16:04:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const H5_JS_CONTENT_DOUBLE_QUOTE = "<!DOCTYPE html><html><head><script src=\"foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href='/foobar'>foobar</a>. Follow up</article></body></html>"
|
|
|
|
const H5_JS_CONTENT_SINGLE_QUOTE = "<!DOCTYPE html><html><head><script src='foobar.js'></script></head><body><nav><h1>title</h1></nav><article>content <a href='/foobar'>foobar</a>. Follow up</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>"
|
2013-09-18 12:15:46 -04:00
|
|
|
|
2013-09-17 16:04:28 -04:00
|
|
|
// URL doesn't recognize authorities. BUG?
|
|
|
|
//const H5_JS_CONTENT_ABS_URL = "<!DOCTYPE html><html><head><script src=\"//host/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 CORRECT_OUTPUT_SRC_HREF = "<!DOCTYPE html><html><head><script src=\"http://base/foobar.js\"></script></head><body><nav><h1>title</h1></nav><article>content <a href=\"http://base/foobar\">foobar</a>. Follow up</article></body></html>"
|
|
|
|
|
|
|
|
func TestAbsUrlify(t *testing.T) {
|
2013-10-01 17:26:21 -04:00
|
|
|
|
|
|
|
tr := &AbsURL{
|
|
|
|
BaseURL: "http://base",
|
|
|
|
}
|
|
|
|
|
2013-10-29 23:24:29 -04:00
|
|
|
apply(t.Errorf, tr, abs_url_tests)
|
2013-10-01 17:26:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type test struct {
|
2013-10-01 22:59:35 -04:00
|
|
|
content string
|
2013-10-01 17:26:21 -04:00
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
2013-10-01 22:59:35 -04:00
|
|
|
var abs_url_tests = []test{
|
|
|
|
{H5_JS_CONTENT_DOUBLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
|
|
|
|
{H5_JS_CONTENT_SINGLE_QUOTE, CORRECT_OUTPUT_SRC_HREF},
|
|
|
|
{H5_JS_CONTENT_ABS_URL, H5_JS_CONTENT_ABS_URL},
|
|
|
|
}
|
2013-09-17 16:04:28 -04:00
|
|
|
|
2013-10-29 23:24:29 -04:00
|
|
|
type errorf func (string, ...interface{})
|
|
|
|
|
|
|
|
func apply(ef errorf, tr Transformer, tests []test) {
|
2013-09-17 16:04:28 -04:00
|
|
|
for _, test := range tests {
|
2013-09-18 12:15:46 -04:00
|
|
|
out := new(bytes.Buffer)
|
2013-10-01 17:42:08 -04:00
|
|
|
err := tr.Apply(out, strings.NewReader(test.content))
|
2013-09-18 12:15:46 -04:00
|
|
|
if err != nil {
|
2013-10-29 23:24:29 -04:00
|
|
|
ef("Unexpected error: %s", err)
|
2013-09-18 12:15:46 -04:00
|
|
|
}
|
|
|
|
if test.expected != string(out.Bytes()) {
|
2013-10-29 23:24:29 -04:00
|
|
|
ef("Expected:\n%s\nGot:\n%s", test.expected, string(out.Bytes()))
|
2013-09-18 12:15:46 -04:00
|
|
|
}
|
2013-09-17 16:04:28 -04:00
|
|
|
}
|
|
|
|
}
|