package human
import (
"context"
"encoding/json"
"net/http"
"net/url"
"testing"
)
func TestGetHumanFromHTML(t *testing.T) {
client := http.DefaultClient
// using human.json author's website
resp, err := client.Get(`https://robida.net/`)
if err != nil {
t.Fatal(err)
}
t.Logf("html fetched")
h, err := GetHumanFromHTML(context.Background(), client, resp)
if err != nil {
t.Fatal(err)
}
if h == nil {
t.Fatal("human.json is nil")
}
b, err := json.Marshal(h)
if err != nil {
t.Fatal(err)
}
t.Logf("%s", b)
}
func TestGetURLFromHTML(t *testing.T) {
base, _ := url.Parse(`https://example.org/foo/`)
u, err := GetURLFromHTML([]byte(``), 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(``), 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(``), 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(``), 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)
}
}