This repository has been archived by the owner on Jul 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
links_test.go
122 lines (101 loc) · 2.7 KB
/
links_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
package halgo
import (
"encoding/json"
"testing"
)
type MyResource struct {
Links
Name string
}
var exampleJson string = `{"_links":{"ea:admin":[{"href":"/admins/2","title":"Fred"},{"href":"/admins/5","title":"Kate"}],"ea:find":{"href":"/orders{?id}","templated":true},"next":{"href":"/orders?page=2"},"self":{"href":"/orders"}},"Name":"James"}`
func TestMarshalLinksToJSON(t *testing.T) {
res := MyResource{
Name: "James",
Links: Links{}.
Self("/orders").
Next("/orders?page=2").
Link("ea:find", "/orders{?id}").
Add("ea:admin", Link{Href: "/admins/2", Title: "Fred"}, Link{Href: "/admins/5", Title: "Kate"}),
}
b, err := json.Marshal(res)
if err != nil {
t.Fatal(err)
}
if string(b) != exampleJson {
t.Errorf("Unexpected JSON %s", b)
}
}
func TestEmptyMarshalLinksToJSON(t *testing.T) {
res := MyResource{
Name: "James",
Links: Links{},
}
b, err := json.Marshal(res)
if err != nil {
t.Fatal(err)
}
if string(b) != `{"Name":"James"}` {
t.Errorf("Unexpected JSON %s", b)
}
}
func TestUnmarshalLinksToJSON(t *testing.T) {
res := MyResource{}
err := json.Unmarshal([]byte(exampleJson), &res)
if err != nil {
t.Fatal(err)
}
if res.Name != "James" {
t.Error("Expected name to be unmarshaled")
}
href, err := res.Href("self")
if err != nil {
t.Fatal(err)
}
if expected := "/orders"; href != expected {
t.Errorf("Expected self to be %s, got %s", expected, href)
}
href, err = res.Href("next")
if err != nil {
t.Fatal(err)
}
if expected := "/orders?page=2"; href != expected {
t.Errorf("Expected next to be %s, got %s", expected, href)
}
href, err = res.HrefParams("ea:find", P{"id": 123})
if err != nil {
t.Fatal(err)
}
if expected := "/orders?id=123"; href != expected {
t.Errorf("Expected ea:find to be %s, got %s", expected, href)
}
// TODO: handle multiple here
href, err = res.Href("ea:admin")
if err != nil {
t.Fatal(err)
}
if expected := "/admins/2"; href != expected {
t.Errorf("Expected ea:admin to be %s, got %s", expected, href)
}
}
func TestLinkFormatting(t *testing.T) {
l := Links{}.
Link("no-format", "/a/url/%s").
Link("format", "/a/url/%d", 10)
if v, _ := l.Href("no-format"); v != "/a/url/%s" {
t.Errorf("Expected no-format to match '/a/url/%%s', got %s", v)
}
if v, _ := l.Href("format"); v != "/a/url/10" {
t.Errorf("Expected no-format to match '/a/url/10', got %s", v)
}
}
func TestAutoSettingOfTemplated(t *testing.T) {
l := Links{}.
Link("not-templated", "/a/b/c").
Link("templated", "/a/b/{c}")
if l.Items["not-templated"][0].Templated != false {
t.Error("not-templated should have Templated=false")
}
if l.Items["templated"][0].Templated != true {
t.Error("not-templated should have Templated=true")
}
}