-
Notifications
You must be signed in to change notification settings - Fork 6
/
api-types_test.go
384 lines (319 loc) · 13.3 KB
/
api-types_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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
Copyright Siyabonga Dlamini ([email protected]).
All rights reserved.
Modified by HourlyHost, Inc.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package gorest
import (
"net/http"
"strings"
"testing"
)
var cook = new(http.Cookie)
var xrefStr = "?xsrft=12345"
var str = "Hell"
func testTypes(t *testing.T) {
cook.Name = "X-Xsrf-Cookie"
cook.Value = "12345"
getString(t)
getStringSimilarPath(t)
getVarArgsString(t)
getInteger(t)
getBool(t)
getFloat(t)
getMapInt(t)
getMapStruct(t)
getArrayStruct(t)
postVarArgs(t)
postVarArgs(t)
postString(t)
postInteger(t)
postBool(t)
postFloat(t)
postMapInt(t)
postMapStruct(t)
postArrayStruct(t)
head(t)
options(t)
doDelete(t)
doDeleteMixed1(t)
doDeleteMixed2(t)
}
func getVarArgs(t *testing.T) {
//getVarArgs EndPoint `method:"GET" path:"/var/{...:int}" output:"string" role:"var-user"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/var/1/2/3/4/5/6/7/8" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Get(&str, 200)
AssertEqual(res.StatusCode, 200, "Get var-args Int ResponseCode", t)
AssertEqual(str, "Start12345678End", "Get var-args Int", t)
}
func postVarArgs(t *testing.T) {
//postVarArgs EndPoint `method:"POST" path:"/var/{...:int}" postdata:"string"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/var/5/24567" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Post("hello")
AssertEqual(res.StatusCode, 200, "Post Var args", t)
}
func getVarArgsString(t *testing.T) {
//getVarArgsString EndPoint `method:"GET" path:"/varstring/{...:string}" output:"string"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/varstring/One/Two/Three" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Get(&str, 200)
AssertEqual(res.StatusCode, 200, "Get var-args string ResponseCode", t)
AssertEqual(str, "StartOneTwoThreeEnd", "Get var-args string", t)
}
func getString(t *testing.T) {
//getString EndPoint `method:"GET" path:"/string/{Bool:bool}/{Int:int}?{flow:int}&{name:string}" output:"string" role:"string-user"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/string/true/5" + xrefStr + "&name=Nameed&flow=6")
rb.AddCookie(cook)
//GET string
res, _ := rb.Get(&str, 200)
AssertEqual(res.StatusCode, 200, "Get string ResponseCode", t)
AssertEqual(str, "Hellotrue5/Nameed6", "Get string", t)
}
func getStringSimilarPath(t *testing.T) {
//getStringSimilarPath EndPoint `method:"GET" path:"/strin?{name:string}" output:"string"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/strin" + xrefStr + "&name=Nameed")
rb.AddCookie(cook)
res, _ := rb.Get(&str, 200)
AssertEqual(res.StatusCode, 200, "Get string ResponseCode", t)
AssertEqual(str, "Yebo-Yes-Nameed", "Get string similar path", t)
rb, _ = NewRequestBuilder(RootPath + "types-service/string/true/5" + xrefStr + "&name=Nameed")
rb.AddCookie(cook)
res, _ = rb.Get(&str, 200)
AssertEqual(res.StatusCode, 200, "Get string ResponseCode", t)
AssertEqual(str, "Hellotrue5/Nameed0", "Get string", t)
rb, _ = NewRequestBuilder(RootPath + "types-service/string/true/5" + xrefStr + "&flow=6")
rb.AddCookie(cook)
res, _ = rb.Get(&str, 200)
AssertEqual(res.StatusCode, 200, "Get string ResponseCode", t)
AssertEqual(str, "Hellotrue5/6", "Get string", t)
rb, _ = NewRequestBuilder(RootPath + "types-service/string/true/5" + xrefStr + "&flow=")
rb.AddCookie(cook)
res, _ = rb.Get(&str, 200)
AssertEqual(res.StatusCode, 200, "Get string ResponseCode", t)
AssertEqual(str, "Hellotrue5/0", "Get string", t)
rb, _ = NewRequestBuilder(RootPath + "types-service/string/true/5" + xrefStr + "&flow")
rb.AddCookie(cook)
res, _ = rb.Get(&str, 200)
AssertEqual(res.StatusCode, 200, "Get string ResponseCode", t)
AssertEqual(str, "Hellotrue5/0", "Get string", t)
}
func getInteger(t *testing.T) {
//getInteger EndPoint `method:"GET" path:"/int/{Bool:bool}/int/yes/{Int:int}/for" output:"int"`
//*******************************
inter := -2
rb, _ := NewRequestBuilder(RootPath + "types-service/int/true/int/yes/2/for" + xrefStr + "&name=Nameed&flow=6")
rb.AddCookie(cook)
res, _ := rb.Get(&inter, 200) //The query aurguments here just to be ignored
AssertEqual(res.StatusCode, 200, "Get int ResponseCode", t)
AssertEqual(inter, -3, "Get int", t)
}
func getBool(t *testing.T) {
//getBool EndPoint `method:"GET" path:"/bool/{Bool:bool}/{Int:int}" output:"bool"`
//*******************************
bl := true
rb, _ := NewRequestBuilder(RootPath + "types-service/bool/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Get(&bl, 200)
AssertEqual(res.StatusCode, 200, "Get int ResponseCode", t)
AssertEqual(bl, false, "Get Bool", t)
}
func getFloat(t *testing.T) {
//getFloat EndPoint `method:"GET" path:"/float/{Bool:bool}/{Int:int}" output:"float64"`
//*******************************
fl := 2.4
rb, _ := NewRequestBuilder(RootPath + "types-service/float/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Get(&fl, 200)
AssertEqual(res.StatusCode, 200, "Get Float ResponseCode", t)
AssertEqual(fl, 222.222, "Get Float", t)
}
func getMapInt(t *testing.T) {
//getMapInt EndPoint `method:"GET" path:"/mapint/{Bool:bool}/{Int:int}" output:"map[string]int"`
//*******************************
mp := make(map[string]int)
rb, _ := NewRequestBuilder(RootPath + "types-service/mapint/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Get(&mp, 200)
AssertEqual(res.StatusCode, 200, "Get Float ResponseCode", t)
AssertEqual(mp["One"], 1, "Get Map Int", t)
AssertEqual(mp["Two"], 2, "Get Map Int", t)
}
func getMapStruct(t *testing.T) {
//getMapStruct EndPoint `method:"GET" path:"/mapstruct/{Bool:bool}/{Int:int}" output:"map[string]User"`
//*******************************
mpu := make(map[string]User)
rb, _ := NewRequestBuilder(RootPath + "types-service/mapstruct/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Get(&mpu, 200)
AssertEqual(res.StatusCode, 200, "Get Map struct ResponseCode", t)
AssertEqual(mpu["One"].Id, "1", "Get Map struct", t)
AssertEqual(mpu["Two"].Id, "2", "Get Map struct", t)
AssertEqual(mpu["Two"].FirstName, "David2", "Get Map struct", t)
AssertEqual(mpu["Two"].LastName, "Gueta2", "Get Map struct", t)
}
func getArrayStruct(t *testing.T) {
//getArrayStruct EndPoint `method:"GET" path:"/arraystruct/{FName:string}/{Age:int}" output:"[]User"`
//*******************************
au := make([]User, 0)
rb, _ := NewRequestBuilder(RootPath + "types-service/arraystruct/Sandy/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Get(&au, 200)
AssertEqual(res.StatusCode, 200, "Get Array struct ResponseCode", t)
if res.StatusCode == 200 {
AssertEqual(au[0].Id, "user1", "Get Array Struct", t)
AssertEqual(au[0].FirstName, "Sandy", "Get Array Struct", t)
}
}
func postString(t *testing.T) {
//postString EndPoint `method:"POST" path:"/string/{Bool:bool}/{Int:int}" postdata:"string" role:"post-user"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/string/true/5" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Post("Hello")
AssertEqual(res.StatusCode, 200, "Post String", t)
}
func postInteger(t *testing.T) {
//postInteger EndPoint `method:"POST" path:"/int/{Bool:bool}/{Int:int}" postdata:"int" role:"postInt-user"`
//*******************************
//POST Int requires the postInt-user role, which only user fox has
rb, _ := NewRequestBuilder(RootPath + "types-service/int/true/5" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Post(6)
AssertEqual(res.StatusCode, 403, "Post Integer wrong user", t)
cook2 := new(http.Cookie)
cook2.Name = "X-Xsrf-Cookie"
cook2.Value = "fox"
xrefStr2 := "?xsrft=fox"
rb2, _ := NewRequestBuilder(RootPath + "types-service/int/true/5" + xrefStr2)
rb2.AddCookie(cook2)
res, _ = rb2.Post(6)
AssertEqual(res.StatusCode, 200, "Post Integer correct user", t)
}
func postBool(t *testing.T) {
//postBool EndPoint `method:"POST" path:"/bool/{Bool:bool}/{Int:int}" postdata:"bool" `
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/bool/true/5" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Post(false)
AssertEqual(res.StatusCode, 200, "Post Boolean", t)
}
func postFloat(t *testing.T) {
//postFloat EndPoint `method:"POST" path:"/float/{Bool:bool}/{Int:int}" postdata:"float64" `
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/float/true/5" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Post(34.56788)
AssertEqual(res.StatusCode, 200, "Post Float", t)
}
func postMapInt(t *testing.T) {
//postMapInt EndPoint `method:"POST" path:"/mapint/{Bool:bool}/{Int:int}" postdata:"map[string]int" `
//*******************************
mi := make(map[string]int, 0)
mi["One"] = 111
mi["Two"] = 222
rb, _ := NewRequestBuilder(RootPath + "types-service/mapint/true/5" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Post(mi)
AssertEqual(res.StatusCode, 200, "Post Integer Map", t)
}
func postMapStruct(t *testing.T) {
//postMapStruct EndPoint `method:"POST" path:"/mapstruct/{Bool:bool}/{Int:int}" postdata:"map[string]User" `
//*******************************
mu := make(map[string]User, 0)
mu["One"] = User{"111", "David1", "Gueta1", 35, 123}
mu["Two"] = User{"222", "David2", "Gueta2", 35, 123}
rb, _ := NewRequestBuilder(RootPath + "types-service/mapstruct/true/5" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Post(mu)
AssertEqual(res.StatusCode, 200, "Post Struct Map", t)
//AssertEqual(res., 200, "Post Struct Map", t)
}
func postArrayStruct(t *testing.T) {
//postArrayStruct EndPoint `method:"POST" path:"/arraystruct/{Bool:bool}/{Int:int}" postdata:"[]User"`
//*******************************
users := make([]User, 0)
users = append(users, User{"user1", "Joe", "Soap", 19, 89.7})
users = append(users, User{"user2", "Jose", "Soap2", 15, 89.7})
rb, _ := NewRequestBuilder(RootPath + "types-service/arraystruct/true/5" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Post(users)
AssertEqual(res.StatusCode, 200, "Post Struct Array", t)
}
func head(t *testing.T) {
//head EndPoint `method:"HEAD" path:"/bool/{Bool:bool}/{Int:int}"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/bool/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Head()
AssertEqual(res.StatusCode, 200, "Head ResponseCode", t)
AssertEqual(res.Header.Get("ETag"), "12345", "Head Header ETag", t)
AssertEqual(len(res.Header["Age"]), 1, "Head Header Age - slice length", t)
if len(res.Header["Age"]) == 1 {
AssertEqual(strings.Trim(res.Header["Age"][0], " "), "1800", "Head Header Age", t)
}
}
func options(t *testing.T) {
//options EndPoint `method:"OPTIONS" path:"/bool/{Bool:bool}/{Int:int}"`
//*******************************
strArr := make([]string, 0)
rb, _ := NewRequestBuilder(RootPath + "types-service/bool/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Options(&strArr)
AssertEqual(res.StatusCode, 200, "Options ResponseCode", t)
AssertEqual(len(strArr), 3, "Options - slice length", t)
if len(strArr) == 3 {
AssertEqual(strArr[0], GET, "Options", t)
AssertEqual(strArr[1], HEAD, "Options", t)
AssertEqual(strArr[2], POST, "Options", t)
}
}
func doDelete(t *testing.T) {
//doDelete EndPoint `method:"DELETE" path:"/bool/{Bool:bool}/{Int:int}"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/bool/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Delete()
AssertEqual(res.StatusCode, 200, "Delete ResponseCode", t)
}
func doDeleteMixed1(t *testing.T) {
//doDeleteMixed1 EndPoint `method:"DELETE" path:"/bool/{Bool:bool}/mix1/{Int:int}"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/bool/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Delete()
AssertEqual(res.StatusCode, 200, "Delete ResponseCode", t)
}
func doDeleteMixed2(t *testing.T) {
//doDeleteMixed2 EndPoint `method:"DELETE" path:"/bool/{Bool:bool}/mix2/{Int:int}"`
//*******************************
rb, _ := NewRequestBuilder(RootPath + "types-service/bool/false/2" + xrefStr)
rb.AddCookie(cook)
res, _ := rb.Delete()
AssertEqual(res.StatusCode, 200, "Delete ResponseCode", t)
}