This repository was archived by the owner on Jan 14, 2023. It is now read-only.
forked from badgateway/ketting
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetting_test.go
More file actions
194 lines (175 loc) · 3.95 KB
/
getting_test.go
File metadata and controls
194 lines (175 loc) · 3.95 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package getting
import (
"encoding/json"
"errors"
"fmt"
// "io"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/identbase/getting/pkg/resource"
"github.com/identbase/getting/pkg/resource/representor"
)
// Unit tests
func Test_Getting_New(t *testing.T) {
tests := []struct {
name string
b string
want *Getting
err error
}{
{
"success",
"localhost:8000",
&Getting{
bookmark: "localhost:8000",
},
nil,
},
{
"error bookmark unspecified",
"",
nil,
errors.New("bookmark unspecified"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g, e := New(tt.b)
if tt.err != nil && e == nil {
t.Errorf("getting.New() should error with %v, got %v", tt.err, e)
} else if tt.err == nil && e != nil {
t.Errorf("getting.New() errored with %v when it shouldnt have", e)
}
if tt.want != nil && g == nil {
t.Errorf("getting.New() should create Getting object, got nil")
} else if tt.want == nil && g != nil {
t.Errorf("getting.New() should not have created Getting object but got one")
} else if tt.want != nil && g != nil {
if g.bookmark != tt.want.bookmark {
t.Errorf("getting.New() = %v, want %v", g, tt.want)
}
}
})
}
}
func Test_Getting_Go_UrlParse(t *testing.T) {
endpoint, _ := url.Parse("http://localhost:8000/api/endpoint")
client, _ := New("http://localhost:8000")
tests := []struct {
name string
u string
want *resource.Resource
err error
}{
{
"success",
"/api/endpoint",
&resource.Resource{
Client: client,
URI: endpoint,
},
nil,
},
{
"error",
fmt.Sprintf("%c", rune(0x7f)),
nil,
errors.New("net/url: invalid control character in URL"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, e := client.Go(tt.u)
if tt.err != nil && e == nil {
t.Errorf("getting.Go() should error, got nil")
} else if tt.err == nil && e != nil {
t.Errorf("getting.Go() errored with %v when it shouldnt have", e)
}
if tt.want != nil && r == nil {
t.Errorf("getting.Go() should create Resource object, got nil")
} else if tt.want == nil && r != nil {
t.Errorf("getting.Go() should not have created Resource object but got one")
} else if tt.want != nil && r != nil {
if (*r).URI.String() != (*tt.want).URI.String() {
t.Errorf("getting.Go() = %v, want %v", r, tt.want)
}
}
})
}
}
// Acceptance tests
func Test_Getting_SimpleResourceGet(t *testing.T) {
tests := []struct {
name string
want interface{}
err error
}{
{
"success",
representor.HALBody{
Links: map[string][]representor.HALLink{
"self": []representor.HALLink{
representor.HALLink{
HRef: "/",
Title: "Test",
},
},
},
Properties: map[string]interface{}{
"foo": "bar",
},
Embedded: map[string]representor.HALBody{},
},
nil,
},
}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := map[string]interface{}{
"_links": map[string]interface{}{
"self": map[string]string{
"href": "/",
"title": "Test",
},
"test": map[string]string{
"href": "/",
"title": "WINNING",
},
},
"foo": "bar",
}
js, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/hal+json")
w.Write(js)
}))
defer s.Close()
g, err := New(s.URL)
if err != nil {
t.Error(err)
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, err := g.Follow("test", nil)
if err != nil {
t.Error(err)
}
o, err := r.Get()
if err != nil {
t.Error(err)
}
for k, v := range tt.want.(representor.HALBody).Links {
if _, ok := o.(representor.HALBody).Links[k]; !ok {
t.Error("object mismatch")
}
if o.(representor.HALBody).Links[k][0] != v[0] {
t.Error("object mismatch")
}
}
})
}
}