forked from ant0ine/go-webfinger
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
154 lines (135 loc) · 3.83 KB
/
client_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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package webfinger
import (
"crypto/tls"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/google/go-cmp/cmp"
)
// setup a local HTTP server for testing
func setup() (client *Client, mux *http.ServeMux, host string, teardown func()) {
// test server
mux = http.NewServeMux()
server := httptest.NewTLSServer(mux)
u, _ := url.Parse(server.URL)
// for testing, use an HTTP client which doesn't check certs
client = NewClient(&http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
})
return client, mux, u.Host, server.Close
}
func TestNewClient_EmptyClient(t *testing.T) {
client := NewClient(nil)
if client.client != http.DefaultClient {
t.Errorf("NewClient(nil) did not use http.DefaultClient")
}
}
func TestResource_Parse(t *testing.T) {
tests := []struct {
input string
want *Resource
}{
// URL with host
{"http://example.com/", &Resource{Scheme: "http", Host: "example.com", Path: "/"}},
// email-like identifier
{"[email protected]", &Resource{Scheme: "acct", Opaque: "[email protected]"}},
}
for _, tt := range tests {
got, err := Parse(tt.input)
if err != nil {
t.Errorf("Parse(%q) returned error: %v", tt.input, err)
}
if !cmp.Equal(got, tt.want) {
t.Errorf("Parse(%q) returned %#v, want %#v", tt.input, got, tt.want)
}
}
}
func TestResource_Parse_error(t *testing.T) {
_, err := Parse("example.com")
if err == nil {
t.Error("Expected parse error")
}
_, err = Parse("%")
if err == nil {
t.Error("Expected parse error")
}
}
func TestResource_WebFingerHost(t *testing.T) {
tests := []struct {
input string
want string
}{
// URL with host
{"http://example.com/", "example.com"},
// emai-like identifier
{"[email protected]", "example.com"},
// mailto URL
{"mailto:[email protected]", "example.com"},
// URL with no host
{"file:///example", ""},
// Email style local account
{"acct:juliet%[email protected]", "shoppingsite.example"},
{"acct:[email protected]@shoppingsite.example", "shoppingsite.example"},
}
for _, tt := range tests {
r, _ := Parse(tt.input)
got := r.WebFingerHost()
if !cmp.Equal(got, tt.want) {
t.Errorf("WebFingerHost(%q) returned %#v, want %#v", tt.input, got, tt.want)
}
}
}
func TestResource_JRDURL(t *testing.T) {
r, _ := Parse("[email protected]")
got := r.JRDURL([]string{"a", "b"})
want, _ := url.Parse("https://example.com/.well-known/webfinger?" +
"rel=a&rel=b&resource=acct%3Abob%40example.com")
if !cmp.Equal(got, want) {
t.Errorf("JRDURL() returned: %#v, want %#v", got, want)
}
}
func TestResource_String(t *testing.T) {
r, _ := Parse("[email protected]")
if got, want := r.String(), "acct:[email protected]"; got != want {
t.Errorf("String() returned: %#v, want %#v", got, want)
}
}
func TestLookup(t *testing.T) {
client, mux, host, teardown := setup()
defer teardown()
mux.HandleFunc("/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) {
resource := r.FormValue("resource")
if want := "acct:bob@" + host; resource != want {
t.Errorf("Requested resource: %v, want %v", resource, want)
}
w.Header().Add("content-type", "application/jrd+json")
fmt.Fprint(w, `{"subject":"[email protected]"}`)
})
jrd, err := client.Lookup("acct:bob@"+host, nil)
if err != nil {
t.Errorf("Unexpected error lookup up webfinger: %v", err)
}
want := &JRD{Subject: "[email protected]"}
if !cmp.Equal(jrd, want) {
t.Errorf("Lookup returned %#v, want %#v", jrd, want)
}
}
func TestLookup_parseError(t *testing.T) {
// use default client here, just to make sure that gets tested
_, err := Lookup("bob", nil)
if err == nil {
t.Error("Expected parse error")
}
}
func TestLookup_404(t *testing.T) {
client, _, host, teardown := setup()
defer teardown()
_, err := client.Lookup("bob@"+host, nil)
if err == nil {
t.Error("Expected error")
}
}