forked from ant0ine/go-webfinger
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjrd_test.go
102 lines (93 loc) · 3.33 KB
/
jrd_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 webfinger
import (
"testing"
"time"
"github.com/google/go-cmp/cmp"
)
func TestParseJRD(t *testing.T) {
// Example JRD from http://tools.ietf.org/html/rfc6415#appendix-A
blob := `
{
"subject":"http://blog.example.com/article/id/314",
"expires":"2010-01-30T09:30:00Z",
"aliases":[
"http://blog.example.com/cool_new_thing",
"http://blog.example.com/steve/article/7"],
"properties":{
"http://blgx.example.net/ns/version":"1.3",
"http://blgx.example.net/ns/ext":null
},
"links":[
{
"rel":"author",
"type":"text/html",
"href":"http://blog.example.com/author/steve",
"titles":{
"default":"About the Author",
"en-us":"Author Information"
},
"properties":{
"http://example.com/role":"editor"
}
},
{
"rel":"author",
"href":"http://example.com/author/john",
"titles":{
"default":"The other author"
}
},
{
"rel":"copyright",
"template":"http://example.com/copyright?id={uri}"
}
]
}
`
obj, err := ParseJRD([]byte(blob))
if err != nil {
t.Fatal(err)
}
if got, want := obj.Subject, "http://blog.example.com/article/id/314"; got != want {
t.Errorf("JRD.Subject is %q, want %q", got, want)
}
expires := time.Date(2010, 01, 30, 9, 30, 0, 0, time.UTC)
if got, want := obj.Expires, &expires; !cmp.Equal(got, want) {
t.Errorf("JRD.Expires is %q, want %q", got, want)
}
// Properties
if got, want := obj.GetProperty("http://blgx.example.net/ns/version"), "1.3"; got != want {
t.Errorf("obj.GetProperty('http://blgx.example.net/ns/version') returned %q, want %q", got, want)
}
if got, want := obj.GetProperty("http://blgx.example.net/ns/ext"), ""; got != want {
t.Errorf("obj.GetProperty('http://blgx.example.net/ns/ext') returned %q, want %q", got, want)
}
if got, want := obj.GetProperty("does-not-exist"), ""; got != want {
t.Errorf("obj.GetProperty('does-not-exist') returned %q, want %q", got, want)
}
// Links
if obj.GetLinkByRel("copyright") == nil {
t.Error("obj.GetLinkByRel('copyright') returned nil, want non-nil value")
}
if got, want := obj.GetLinkByRel("copyright").Template, "http://example.com/copyright?id={uri}"; got != want {
t.Errorf("obj.GetLinkByRel('copyright').Template returned %q, want %q", got, want)
}
if got, want := obj.GetLinkByRel("author").Titles["default"], "About the Author"; got != want {
t.Errorf("obj.GetLinkByRel('author').Titles['default'] returned %q, want %q", got, want)
}
if got, want := obj.GetLinkByRel("author").GetProperty("http://example.com/role"), "editor"; got != want {
t.Errorf("obj.GetLinkByRel('author').GetProperty('http://example.com/role') returned %q, want %q", got, want)
}
if got, want := obj.GetLinkByRel("does-not-exist"), (*Link)(nil); got != want {
t.Errorf("obj.GetLinkByRel('does-not-exist') returned %q, want %q", got, want)
}
if got, want := obj.GetLinkByRel("author").GetProperty("does-not-exist"), ""; got != want {
t.Errorf("obj.GetLinkByRel('author').GetProperty('does-not-exist') returned %q, want %q", got, want)
}
}
func TestParseJRD_error(t *testing.T) {
_, err := ParseJRD([]byte("`"))
if err == nil {
t.Errorf("ParseJRD(`) did not return expected error")
}
}