forked from hacdias/indielib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathverification_test.go
102 lines (90 loc) · 2.54 KB
/
verification_test.go
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
package indieauth
import (
"fmt"
"testing"
)
var validProfileURLs = []string{
"https://example.com/",
"https://example.com/username",
"https://example.com/users?id=100",
}
func TestValidProfileURL(t *testing.T) {
for _, profileURL := range validProfileURLs {
err := IsValidProfileURL(profileURL)
if err != nil {
t.Error("profile URL is valid, but errored", err)
}
}
}
var invalidProfileURLs = []struct {
URL string
Error error
}{
{"example.com", ErrInvalidScheme},
{"mailto:[email protected]", ErrInvalidScheme},
{"https://example.com/foo/../bar", ErrInvalidPath},
{"https://example.com/#me", ErrInvalidFragment},
{"https://user:[email protected]/", ErrUserIsSet},
{"https://example.com:8443/", ErrPortIsSet},
{"https://172.28.92.51/", ErrIsIP},
}
func TestInvalidProfileURL(t *testing.T) {
for _, test := range invalidProfileURLs {
err := IsValidProfileURL(test.URL)
if err != test.Error {
t.Error("expected to error with", test.Error, "but errored with", err)
}
}
}
var validClientIdentifiers = []string{
"https://example.com/",
"https://example.com/username",
"https://example.com/users?id=100",
"https://example.com:8443/",
"https://127.0.0.1/",
"https://[::1]/",
}
func TestValidClientIdentifier(t *testing.T) {
for _, clientID := range validClientIdentifiers {
err := IsValidClientIdentifier(clientID)
if err != nil {
t.Error("client ID is valid, but errored", err)
}
}
}
var invalidClientIdentifier = []struct {
URL string
Error error
}{
{"example.com", ErrInvalidScheme},
{"mailto:[email protected]", ErrInvalidScheme},
{"https://example.com/foo/../bar", ErrInvalidPath},
{"https://example.com/#me", ErrInvalidFragment},
{"https://user:[email protected]/", ErrUserIsSet},
{"https://172.28.92.51/", ErrIsNonLoopback},
}
func TestInvalidClientIdentifier(t *testing.T) {
for _, test := range invalidClientIdentifier {
err := IsValidClientIdentifier(test.URL)
if err != test.Error {
t.Error("expected to error with", test.Error, "but errored with", err)
}
}
}
var canonicalizeTests = [][2]string{
{"example.com", "https://example.com/"},
{"http://example.com", "http://example.com/"},
{"https://example.com", "https://example.com/"},
{"example.com/", "https://example.com/"},
}
func TestCanonicalizeURL(t *testing.T) {
for _, test := range canonicalizeTests {
if CanonicalizeURL(test[0]) != test[1] {
t.Errorf("canonicalize: expected %s, got %s", test[1], CanonicalizeURL(test[0]))
}
}
}
func ExampleCanonicalizeURL() {
fmt.Println(CanonicalizeURL("example.com"))
// Output: https://example.com/
}