-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_test.go
277 lines (249 loc) · 9.09 KB
/
resource_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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package gorest
import (
"net/http"
"reflect"
"testing"
)
// TestResourceGetSupport verifies the type conversion for resource supporting
// GET method.
func TestResourceGetSupport(t *testing.T) {
res, ok := (Resource(testResourceWithGet{})).(GetSupported)
if !ok {
t.Fatalf("Get should be supported by %s", reflect.TypeOf(testResourceWithGet{}).String())
}
code, resp := res.Get(nil)
if code != 200 {
t.Fatalf("Unexpected status code. Expected: %d - Found: %d.", 200, code)
}
if resp != nil {
t.Fatalf("Unexpected resp. Shoud be `nil` - Found: %+v.", resp)
}
_, ok = (Resource(testResourceWithGet{})).(PostSupported)
if ok {
t.Fatalf("Post should not be supported by %s", reflect.TypeOf(testResourceWithGet{}).String())
}
_, ok = (Resource(testResourceWithGet{})).(PutSupported)
if ok {
t.Fatalf("Put should not be supported by %s", reflect.TypeOf(testResourceWithGet{}).String())
}
_, ok = (Resource(testResourceWithGet{})).(DeleteSupported)
if ok {
t.Fatalf("Delete should not be supported by %s", reflect.TypeOf(testResourceWithGet{}).String())
}
_, ok = (Resource(testResourceWithGet{})).(HeadSupported)
if ok {
t.Fatalf("Head should not be supported by %s", reflect.TypeOf(testResourceWithGet{}).String())
}
_, ok = (Resource(testResourceWithGet{})).(PatchSupported)
if ok {
t.Fatalf("Patch should not be supported by %s", reflect.TypeOf(testResourceWithGet{}).String())
}
}
// TestResourcePostSupport verifies the type conversion for resource supporting
// POST method.
func TestResourcePostSupport(t *testing.T) {
res, ok := (Resource(testResourceWithPost{})).(PostSupported)
if !ok {
t.Fatalf("Post should be supported by %s", reflect.TypeOf(testResourceWithPost{}).String())
}
code, resp := res.Post(nil)
if code != 200 {
t.Fatalf("Unexpected status code. Expected: %d - Found: %d.", 200, code)
}
if resp != nil {
t.Fatalf("Unexpected resp. Shoud be `nil` - Found: %+v.", resp)
}
_, ok = (Resource(testResourceWithPost{})).(GetSupported)
if ok {
t.Fatalf("Get should not be supported by %s", reflect.TypeOf(testResourceWithPost{}).String())
}
_, ok = (Resource(testResourceWithPost{})).(PutSupported)
if ok {
t.Fatalf("Put should not be supported by %s", reflect.TypeOf(testResourceWithPost{}).String())
}
_, ok = (Resource(testResourceWithPost{})).(DeleteSupported)
if ok {
t.Fatalf("Delete should not be supported by %s", reflect.TypeOf(testResourceWithPost{}).String())
}
_, ok = (Resource(testResourceWithPost{})).(HeadSupported)
if ok {
t.Fatalf("Head should not be supported by %s", reflect.TypeOf(testResourceWithPost{}).String())
}
_, ok = (Resource(testResourceWithPost{})).(PatchSupported)
if ok {
t.Fatalf("Patch should not be supported by %s", reflect.TypeOf(testResourceWithPost{}).String())
}
}
// TestResourcePutSupport verifies the type conversion for resource supporting
// PUT method.
func TestResourcePutSupport(t *testing.T) {
res, ok := (Resource(testResourceWithPut{})).(PutSupported)
if !ok {
t.Fatalf("Put should be supported by %s", reflect.TypeOf(testResourceWithPut{}).String())
}
code, resp := res.Put(nil)
if code != 200 {
t.Fatalf("Unexpected status code. Expected: %d - Found: %d.", 200, code)
}
if resp != nil {
t.Fatalf("Unexpected resp. Shoud be `nil` - Found: %+v.", resp)
}
_, ok = (Resource(testResourceWithPut{})).(GetSupported)
if ok {
t.Fatalf("Get should not be supported by %s", reflect.TypeOf(testResourceWithPut{}).String())
}
_, ok = (Resource(testResourceWithPut{})).(PostSupported)
if ok {
t.Fatalf("Post should not be supported by %s", reflect.TypeOf(testResourceWithPut{}).String())
}
_, ok = (Resource(testResourceWithPut{})).(DeleteSupported)
if ok {
t.Fatalf("Delete should not be supported by %s", reflect.TypeOf(testResourceWithPut{}).String())
}
_, ok = (Resource(testResourceWithPut{})).(HeadSupported)
if ok {
t.Fatalf("Head should not be supported by %s", reflect.TypeOf(testResourceWithPut{}).String())
}
_, ok = (Resource(testResourceWithPut{})).(PatchSupported)
if ok {
t.Fatalf("Patch should not be supported by %s", reflect.TypeOf(testResourceWithPut{}).String())
}
}
// TestResourceDeleteSupport verifies the type conversion for resource supporting
// DELETE method.
func TestResourceDeleteSupport(t *testing.T) {
res, ok := (Resource(testResourceWithDelete{})).(DeleteSupported)
if !ok {
t.Fatalf("Delete should be supported by %s", reflect.TypeOf(testResourceWithDelete{}).String())
}
code, resp := res.Delete(nil)
if code != 200 {
t.Fatalf("Unexpected status code. Expected: %d - Found: %d.", 200, code)
}
if resp != nil {
t.Fatalf("Unexpected resp. Shoud be `nil` - Found: %+v.", resp)
}
_, ok = (Resource(testResourceWithDelete{})).(GetSupported)
if ok {
t.Fatalf("Get should not be supported by %s", reflect.TypeOf(testResourceWithDelete{}).String())
}
_, ok = (Resource(testResourceWithDelete{})).(PostSupported)
if ok {
t.Fatalf("Post should not be supported by %s", reflect.TypeOf(testResourceWithDelete{}).String())
}
_, ok = (Resource(testResourceWithDelete{})).(PutSupported)
if ok {
t.Fatalf("Put should not be supported by %s", reflect.TypeOf(testResourceWithDelete{}).String())
}
_, ok = (Resource(testResourceWithDelete{})).(HeadSupported)
if ok {
t.Fatalf("Head should not be supported by %s", reflect.TypeOf(testResourceWithDelete{}).String())
}
_, ok = (Resource(testResourceWithDelete{})).(PatchSupported)
if ok {
t.Fatalf("Patch should not be supported by %s", reflect.TypeOf(testResourceWithDelete{}).String())
}
}
// TestResourceHeadSupport verifies the type conversion for resource supporting
// HEAD method.
func TestResourceHeadSupport(t *testing.T) {
res, ok := (Resource(testResourceWithHead{})).(HeadSupported)
if !ok {
t.Fatalf("Head should be supported by %s", reflect.TypeOf(testResourceWithHead{}).String())
}
code, resp := res.Head(nil)
if code != 200 {
t.Fatalf("Unexpected status code. Expected: %d - Found: %d.", 200, code)
}
if resp != nil {
t.Fatalf("Unexpected resp. Shoud be `nil` - Found: %+v.", resp)
}
_, ok = (Resource(testResourceWithHead{})).(GetSupported)
if ok {
t.Fatalf("Get should not be supported by %s", reflect.TypeOf(testResourceWithHead{}).String())
}
_, ok = (Resource(testResourceWithHead{})).(PostSupported)
if ok {
t.Fatalf("Post should not be supported by %s", reflect.TypeOf(testResourceWithHead{}).String())
}
_, ok = (Resource(testResourceWithHead{})).(PutSupported)
if ok {
t.Fatalf("Put should not be supported by %s", reflect.TypeOf(testResourceWithHead{}).String())
}
_, ok = (Resource(testResourceWithHead{})).(DeleteSupported)
if ok {
t.Fatalf("Delete should not be supported by %s", reflect.TypeOf(testResourceWithHead{}).String())
}
_, ok = (Resource(testResourceWithHead{})).(PatchSupported)
if ok {
t.Fatalf("Patch should not be supported by %s", reflect.TypeOf(testResourceWithHead{}).String())
}
}
// TestResourcePatchSupport verifies the type conversion for resource supporting
// PATCH method.
func TestResourcePatchSupport(t *testing.T) {
res, ok := (Resource(testResourceWithPatch{})).(PatchSupported)
if !ok {
t.Fatalf("Patch should be supported by %s", reflect.TypeOf(testResourceWithPatch{}).String())
}
code, resp := res.Patch(nil)
if code != 200 {
t.Fatalf("Unexpected status code. Expected: %d - Found: %d.", 200, code)
}
if resp != nil {
t.Fatalf("Unexpected resp. Shoud be `nil` - Found: %+v.", resp)
}
_, ok = (Resource(testResourceWithPatch{})).(GetSupported)
if ok {
t.Fatalf("Get should not be supported by %s", reflect.TypeOf(testResourceWithPatch{}).String())
}
_, ok = (Resource(testResourceWithPatch{})).(PostSupported)
if ok {
t.Fatalf("Post should not be supported by %s", reflect.TypeOf(testResourceWithPatch{}).String())
}
_, ok = (Resource(testResourceWithPatch{})).(PutSupported)
if ok {
t.Fatalf("Put should not be supported by %s", reflect.TypeOf(testResourceWithPatch{}).String())
}
_, ok = (Resource(testResourceWithPatch{})).(DeleteSupported)
if ok {
t.Fatalf("Delete should not be supported by %s", reflect.TypeOf(testResourceWithPatch{}).String())
}
_, ok = (Resource(testResourceWithPatch{})).(HeadSupported)
if ok {
t.Fatalf("Head should not be supported by %s", reflect.TypeOf(testResourceWithPatch{}).String())
}
}
//
// Structures for test
//
type testResourceWithGet struct{}
func (testResourceWithGet) Get(r *http.Request) (int, Response) {
return 200, nil
}
type testResourceWithGetAndResponse struct {
response testResponse
}
func (t testResourceWithGetAndResponse) Get(r *http.Request) (int, Response) {
return 200, &t.response
}
type testResourceWithPost struct{}
func (testResourceWithPost) Post(r *http.Request) (int, Response) {
return 200, nil
}
type testResourceWithPut struct{}
func (testResourceWithPut) Put(r *http.Request) (int, Response) {
return 200, nil
}
type testResourceWithDelete struct{}
func (testResourceWithDelete) Delete(r *http.Request) (int, Response) {
return 200, nil
}
type testResourceWithHead struct{}
func (testResourceWithHead) Head(r *http.Request) (int, Response) {
return 200, nil
}
type testResourceWithPatch struct{}
func (testResourceWithPatch) Patch(r *http.Request) (int, Response) {
return 200, nil
}