-
Notifications
You must be signed in to change notification settings - Fork 6
/
webfist_test.go
103 lines (86 loc) · 2.28 KB
/
webfist_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
package webfinger
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"github.com/ant0ine/go-webfinger/jrd"
)
var (
wfMux *http.ServeMux
wfServer *httptest.Server
wfTestHost string
)
func webFistSetup() {
setup()
wfMux = http.NewServeMux()
wfServer = httptest.NewTLSServer(wfMux)
u, _ := url.Parse(wfServer.URL)
wfTestHost = u.Host
client.WebFistServer = wfTestHost
}
func webFistTearDown() {
teardown()
wfServer.Close()
}
func TestWebFistLookup(t *testing.T) {
webFistSetup()
defer webFistTearDown()
mux.HandleFunc("/webfinger.json", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/jrd+json")
fmt.Fprint(w, `{"subject":"[email protected]"}`)
})
// simulate WebFist protocol
wfMux.HandleFunc("/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) {
resource := r.FormValue("resource")
if want := "acct:bob@" + testHost; resource != want {
t.Errorf("Requested resource: %v, want %v", resource, want)
}
w.Header().Add("content-type", "application/jrd+json")
fmt.Fprint(w, `{
"links": [{
"rel": "http://webfist.org/spec/rel",
"href": "`+server.URL+`/webfinger.json"
}]
}`)
})
JRD, err := client.Lookup("bob@"+testHost, nil)
if err != nil {
t.Errorf("Unexpected error lookup up webfinger: %#v", err)
}
want := &jrd.JRD{Subject: "[email protected]"}
if !reflect.DeepEqual(JRD, want) {
t.Errorf("Lookup returned %#v, want %#v", JRD, want)
}
}
func TestWebFistLookup_noLink(t *testing.T) {
webFistSetup()
defer webFistTearDown()
wfMux.HandleFunc("/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/jrd+json")
fmt.Fprint(w, `{}`)
})
_, err := client.Lookup("bob@"+testHost, nil)
if err == nil {
t.Errorf("Expected webfist error.")
}
}
func TestWebFistLookup_invalidLink(t *testing.T) {
webFistSetup()
defer webFistTearDown()
wfMux.HandleFunc("/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("content-type", "application/jrd+json")
fmt.Fprint(w, `{
"links": [{
"rel": "http://webfist.org/spec/rel",
"href": "#"
}]
}`)
})
_, err := client.Lookup("bob@"+testHost, nil)
if err == nil {
t.Errorf("Expected webfist error.")
}
}