-
Notifications
You must be signed in to change notification settings - Fork 0
/
hal_test.go
150 lines (132 loc) · 3.64 KB
/
hal_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
package hypermedia
import (
"bytes"
"encoding/json"
"reflect"
"testing"
)
var HALDocumentJSONTests = []struct {
json []byte
doc HALDocument
}{
{[]byte(`{}`), HALDocument{}},
{[]byte(`{"a":"xyz","b":true,"c":123.4}`),
HALDocument{Attributes: Attributes{
"a": "xyz",
"b": true,
"c": 123.4,
}}},
{[]byte(`{"_links":{"a":{"href":"xyz"},"b":[{"name":"j:k"},{"templated":true}]}}`),
HALDocument{Links: Links{
"a": {{"href": "xyz"}},
"b": {{"name": "j:k"}, {"templated": true}},
}}},
{[]byte(`{"_links":{"curies":[{"href":"any","name":"pre","templated":true}]}}`),
HALDocument{Links: Links{
"curies": {{"name": "pre", "href": "any", "templated": true}},
}}},
{[]byte(`{"_embedded":{"a":{"x":"y"},"b":[{"c":"d"},{}]}}`),
HALDocument{Embedded: Resources{
"a": {&Resource{Attributes: Attributes{"x": "y"}}},
"b": {&Resource{Attributes: Attributes{"c": "d"}}, &Resource{}},
}}},
}
func TestHALDocumentMarshalJSON(t *testing.T) {
for _, tt := range HALDocumentJSONTests {
result, err := json.Marshal(tt.doc)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(result, tt.json) {
t.Errorf("Expected '%s', got '%s'", tt.json, result)
}
}
}
func TestHALDocumentMarshalJSONExcludesEmptyTransitions(t *testing.T) {
for _, tt := range []struct {
json []byte
doc HALDocument
}{
{[]byte(`{"_links":{"a":{"href":"xyz"}}}`),
HALDocument{Links: Links{
"a": {{"href": "xyz"}},
"n": {},
}}},
{[]byte(`{"_embedded":{"a":{"x":"y"}}}`),
HALDocument{Embedded: Resources{
"a": {&Resource{Attributes: Attributes{"x": "y"}}},
"n": {},
}}},
} {
result, err := json.Marshal(tt.doc)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(result, tt.json) {
t.Errorf("Expected '%s', got '%s'", tt.json, result)
}
}
}
func TestHALDocumentUnmarshalJSON(t *testing.T) {
var emptyMaps func(*Resource)
emptyMaps = func(r *Resource) {
if r.Attributes == nil {
r.Attributes = make(Attributes)
}
if r.Embedded == nil {
r.Embedded = make(Resources)
} else {
for _, s := range r.Embedded {
for _, r := range s {
emptyMaps(r)
}
}
}
if r.Links == nil {
r.Links = make(Links)
}
}
for _, tt := range HALDocumentJSONTests {
var result HALDocument
err := json.Unmarshal(tt.json, &result)
if err != nil {
t.Fatal(err)
}
// reflect.DeepEqual distinguishes between empty map and nil map.
// We expect empty.
doc := tt.doc
emptyMaps((*Resource)(&doc))
if !reflect.DeepEqual(result, doc) {
t.Errorf("Expected %#v, got %#v", doc, result)
// Print the content of Resource pointers
for k, v := range doc.Embedded {
for i, r := range v {
if !reflect.DeepEqual(result.Embedded[k][i], r) {
t.Logf("%q,%v: expected %#v, got %#v", k, i, r, result.Embedded[k][i])
}
}
}
}
}
}
func TestHALDocumentUnmarshalJSONErrors(t *testing.T) {
for _, tt := range []string{
// Its root object MUST be a Resource Object.
``, `null`, `[]`, `123`,
// It is an object whose property names are link relation types (as
// defined by [RFC5988]) and values are either a Link Object or an array
// of Link Objects.
`{"_links":null}`, `{"_links":[]}`, `{"_links":123}`,
`{"_links":{"a":null}}`, `{"_links":{"a":123}}`,
// It is an object whose property names are link relation types (as
// defined by [RFC5988]) and values are either a Resource Object or an
// array of Resource Objects.
`{"_embedded":null}`, `{"_embedded":[]}`, `{"_embedded":123}`,
`{"_embedded":{"a":null}}`, `{"_embedded":{"a":123}}`,
} {
err := json.Unmarshal([]byte(tt), new(HALDocument))
if err == nil {
t.Errorf("Expected error for invalid hal+json: '%s'", tt)
}
}
}