aboutsummaryrefslogtreecommitdiff
path: root/human_test.go
blob: cb0b86c102341e9dcfc3fcb855a300a329c18f42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package human_test

import (
	"encoding/json"
	"fmt"
	"testing"
	"time"

	"git.anhgelus.world/anhgelus/go-human.json"
)

func TestURL_Json(t *testing.T) {
	var r human.URL
	err := json.Unmarshal([]byte(`"http://example.com"`), &r)
	if err != nil {
		t.Fatal(err)
	}
	if r.Scheme != "http" {
		t.Errorf("invalid scheme: %s", r.Scheme)
	}
	if r.Host != "example.com" {
		t.Errorf("invalid host: %s", r.Host)
	}
	if r.Path != "" {
		t.Errorf("invalid path: %s", r.Path)
	}

	err = json.Unmarshal([]byte(`"http://example.com:80/"`), &r)
	if err != nil {
		t.Fatal(err)
	}
	if r.Scheme != "http" {
		t.Errorf("invalid scheme: %s", r.Scheme)
	}
	if r.Host != "example.com" {
		t.Errorf("invalid host: %s", r.Host)
	}
	if r.Path != "" {
		t.Errorf("invalid path: %s", r.Path)
	}

	err = json.Unmarshal([]byte(`"https://foo.example.com:443/hello/world/"`), &r)
	if err != nil {
		t.Fatal(err)
	}
	if r.Scheme != "https" {
		t.Errorf("invalid scheme: %s", r.Scheme)
	}
	if r.Host != "foo.example.com" {
		t.Errorf("invalid host: %s", r.Host)
	}
	if r.Path != "/hello/world" {
		t.Errorf("invalid path: %s", r.Path)
	}

	err = json.Unmarshal([]byte(`"https://example.com:80/hello/world/?foo=bar"`), &r)
	if err != nil {
		t.Fatal(err)
	}
	if r.Scheme != "https" {
		t.Errorf("invalid scheme: %s", r.Scheme)
	}
	if r.Host != "example.com:80" {
		t.Errorf("invalid host: %s", r.Host)
	}
	if r.Path != "/hello/world" {
		t.Errorf("invalid path: %s", r.Path)
	}
}

func genericMatch(base *human.URL, test string) bool {
	u, _ := human.NewURL(test)
	return base.Match(u)
}

func TestURL_Match(t *testing.T) {
	base, _ := human.NewURL(`https://example.org/~alice`)

	if !genericMatch(base, `https://example.org/~alice`) {
		t.Errorf("same must match")
	}
	if !genericMatch(base, `https://example.org/~alice/blog`) {
		t.Errorf("sub must match")
	}
	if !genericMatch(base, `https://example.org/~alice/blog/my-post`) {
		t.Errorf("any depth sub must match")
	}

	if genericMatch(base, `https://example.org/~bob`) {
		t.Errorf("different path mustn't match")
	}
	if genericMatch(base, `https://example.org/`) {
		t.Errorf("home musn't match")
	}
	if genericMatch(base, `https://example.org/~alice-two`) {
		t.Errorf("different path musn't match")
	}

	if genericMatch(base, `https://foo.example.org/~alice`) {
		t.Errorf("different host musn't match")
	}
	if genericMatch(base, `http://example.org/~alice`) {
		t.Errorf("different scheme musn't match")
	}

	if !genericMatch(base, `https://example.org:443/~alice`) {
		t.Errorf("std port must match")
	}
}

func TestVouch_Json(t *testing.T) {
	var v human.Vouch
	err := json.Unmarshal([]byte(`{"url": "https://bob.example.com","vouched_at": "2026-01-15"}`), &v)
	if err != nil {
		t.Fatal(err)
	}
	if v.URL.String() != "https://bob.example.com" {
		t.Errorf("invalid url: %v", v.URL)
	}
	if v.VouchedAt.Format(time.DateOnly) != "2026-01-15" {
		t.Errorf("invalid vouched_at: %v", v.VouchedAt)
	}

	u, _ := human.NewURL(`https://bob.example.com:443/`)
	now := time.Now()
	v = human.Vouch{
		URL:       u,
		VouchedAt: now,
	}
	b, err := json.Marshal(&v)
	if err != nil {
		t.Fatal(err)
	}
	if string(b) != fmt.Sprintf(`{"url":"https://bob.example.com","vouched_at":"%s"}`, now.Format(time.DateOnly)) {
		t.Errorf("invalid marshal: %s", b)
	}
}