aboutsummaryrefslogtreecommitdiff
path: root/html_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'html_test.go')
-rw-r--r--html_test.go97
1 files changed, 97 insertions, 0 deletions
diff --git a/html_test.go b/html_test.go
new file mode 100644
index 0000000..ab2bda7
--- /dev/null
+++ b/html_test.go
@@ -0,0 +1,97 @@
+package human
+
+import (
+ "net/url"
+ "testing"
+)
+
+func TestGetURLFromHTML(t *testing.T) {
+ base, _ := url.Parse(`https://example.org/foo/`)
+ u, err := GetURLFromHTML([]byte(`<link rel=human-json href=/human.json>`), base)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if u == nil {
+ t.Fatal("not found")
+ }
+ if u.Path != "/human.json" {
+ t.Errorf("invalid path: %s", u.Path)
+ }
+ if u.Host != "example.org" {
+ t.Errorf("invalid host: %s", u.Host)
+ }
+
+ u, err = GetURLFromHTML([]byte(`<link rel="human-json" href="/human.json">`), base)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if u == nil {
+ t.Fatal("not found")
+ }
+ if u.Path != "/human.json" {
+ t.Errorf("invalid path: %s", u.Path)
+ }
+ if u.Host != "example.org" {
+ t.Errorf("invalid host: %s", u.Host)
+ }
+
+ u, err = GetURLFromHTML([]byte(`<link rel="human-json" href="human.json">`), base)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if u == nil {
+ t.Fatal("not found")
+ }
+ if u.Path != "/foo/human.json" {
+ t.Errorf("invalid path: %s", u.Path)
+ }
+ if u.Host != "example.org" {
+ t.Errorf("invalid host: %s", u.Host)
+ }
+
+ u, err = GetURLFromHTML([]byte(`<link rel="human-json" href="../human.json">`), base)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if u == nil {
+ t.Fatal("not found")
+ }
+ if u.Path != "/human.json" {
+ t.Errorf("invalid path: %s", u.Path)
+ }
+ if u.Host != "example.org" {
+ t.Errorf("invalid host: %s", u.Host)
+ }
+}
+
+func TestParseArgs(t *testing.T) {
+ args := parseArgs(`key="hello world">`)
+ if args["key"] != "hello world" {
+ t.Errorf("invalid arg: %v", args)
+ }
+
+ args = parseArgs(`key=hello>`)
+ if args["key"] != "hello" {
+ t.Errorf("invalid arg: %v", args)
+ }
+
+ args = parseArgs(`key=hello world>`)
+ if args["key"] != "hello" {
+ t.Errorf("invalid arg: %v", args)
+ }
+
+ args = parseArgs(`key=hello/>`)
+ if args["key"] != "hello" {
+ t.Errorf("invalid arg: %v", args)
+ }
+
+ args = parseArgs(`key=hello`)
+ if args["key"] != "hello" {
+ t.Errorf("invalid arg: %v", args)
+ }
+
+ args = parseArgs(`key=word foo=bar`)
+ if args["key"] != "word" || args["foo"] != "bar" {
+ t.Errorf("invalid args: %v", args)
+ }
+}