-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi_test.go
More file actions
696 lines (541 loc) · 14.8 KB
/
api_test.go
File metadata and controls
696 lines (541 loc) · 14.8 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
package golax
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strconv"
"strings"
"testing"
)
func bodyBytes(r *http.Request) []byte {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
return body
}
func bodyString(r *http.Request) string {
return string(bodyBytes(r))
}
func Test_404_ok(t *testing.T) {
world := NewWorld()
defer world.Destroy()
response := world.Request("GET", "/hello").Do()
if http.StatusNotFound != response.StatusCode {
t.Error("Status code '404' is expected")
}
}
func Test_405_ok(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.Method("POST", func(c *Context) {
// Do nothing
})
response := world.Request("GET", "/").Do()
if http.StatusMethodNotAllowed != response.StatusCode {
t.Error("Status code '405' is expected")
}
}
/**
* What happens if path is empty string
*/
func Test_border_case_1(t *testing.T) {
world := NewWorld()
defer world.Destroy()
response := world.Request("GET", "").Do()
if http.StatusMethodNotAllowed != response.StatusCode {
t.Error("Status code '405' is expected")
}
}
func Test_Prefix(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Prefix = "/my/prefix/v3"
world.Api.Root.Node("resource").Method(
"GET",
func(c *Context) {
fmt.Fprint(c.Response, "My resource")
},
)
response := world.Request("GET", "/my/prefix/v3/resource").Do()
if "My resource" != response.BodyString() {
t.Error("Body 'My resource' is expected")
}
}
/**
* Test if standard methods (and a invented one) are handleable.
* A valid response should return the non standard `432` status code.
*/
func Test_Methods_ok(t *testing.T) {
methods := []string{
"OPTIONS", "GET", "HEAD",
"POST", "PUT", "DELETE",
"TRACE", "CONNECT", "PATCH",
"INVENTED",
}
for _, method := range methods {
world := NewWorld()
world.Api.Root.Node("hello").Method(method, func(c *Context) {
c.Response.WriteHeader(432)
})
response := world.Request(method, "/hello").Do()
if 432 != response.StatusCode {
t.Error("Method '" + method + "': Status code '432' is expected")
}
world.Destroy()
}
}
/**
* Test if standard methods (and a invented one) are handleable if are not
* defined but the asterisk method is defined.
* A valid response should return the non standard `432` status code.
*/
func Test_Method_asterisk_ok(t *testing.T) {
methods := []string{
"OPTIONS", "GET", "HEAD",
"POST", "PUT", "DELETE",
"TRACE", "CONNECT", "PATCH",
"INVENTED",
}
for _, method := range methods {
world := NewWorld()
world.Api.Root.Node("hello").Method("*", func(c *Context) {
c.Response.WriteHeader(432)
})
response := world.Request(method, "/hello").Do()
if 432 != response.StatusCode {
t.Error("Method '" + method + "': Status code '432' is expected")
}
world.Destroy()
}
}
/**
* Test method precedence (all methods over asterisk)
* Status code `432` should be returned
*/
func Test_Method_not_asterisk_ok(t *testing.T) {
methods := []string{
"OPTIONS", "GET", "HEAD",
"POST", "PUT", "DELETE",
"TRACE", "CONNECT", "PATCH",
"INVENTED",
}
for _, method := range methods {
world := NewWorld()
world.Api.Root.Node("hello").Method(method, func(c *Context) {
c.Response.WriteHeader(432)
})
world.Api.Root.Node("hello").Method("*", func(c *Context) {
c.Response.WriteHeader(431)
})
response := world.Request(method, "/hello").Do()
if 432 != response.StatusCode {
t.Error("Method '" + method + "': Status code '432' is expected")
}
world.Destroy()
}
}
/**
* methods defined as lower case should be also handled
*/
func Test_Method_lowercase_ok(t *testing.T) {
methods := []string{
"options", "get", "head",
"post", "put", "delete",
"trace", "connect", "patch",
"invented", "opTionS", "Put", "pOst", "dELETE",
}
for _, method := range methods {
world := NewWorld()
world.Api.Root.Node("hello").Method(method, func(c *Context) {
c.Response.WriteHeader(432)
})
METHOD := strings.ToUpper(method)
response := world.Request(METHOD, "/hello").Do()
if 432 != response.StatusCode {
t.Error("Method '" + method + "': Status code '432' is expected")
}
world.Destroy()
}
}
/**
* methods defined as upper case but the http request is lowercase
*/
func Test_Method_uppercase_ok(t *testing.T) {
methods := []string{
"options", "get", "head",
"post", "put", "delete",
"trace", "connect", "patch",
"invented", "opTionS", "Put", "pOst", "dELETE",
}
for _, method := range methods {
world := NewWorld()
world.Api.Root.Node("hello").Method(method, func(c *Context) {
c.Response.WriteHeader(432)
})
METHOD := strings.ToLower(method)
response := world.Request(METHOD, "/hello").Do()
if 432 != response.StatusCode {
t.Error("Method '" + method + "': Status code '432' is expected")
}
world.Destroy()
}
}
/**
* Call to context.Error `555`
*/
func Test_Method_error_555(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.Interceptor(&Interceptor{
After: func(c *Context) {
if nil != c.LastError {
c.Response.WriteHeader(c.LastError.StatusCode)
}
},
})
world.Api.Root.Node("hello").Method("GET", func(c *Context) {
c.Error(555, "Sample error")
})
response := world.Request("GET", "/hello").Do()
if 555 != response.StatusCode {
t.Error("Status code '555' is expected")
}
}
func Test_Parameter(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.Node("users").Node("{id}").Method("GET", func(c *Context) {
fmt.Fprintln(c.Response, "The user is "+c.Parameter)
})
response := world.Request("GET", "/users/42").Do()
if 200 != response.StatusCode {
t.Error("Status code '200' is expected")
}
if "The user is 42\n" != response.BodyString() {
t.Error("Body 'The user is 42\\n' is expected")
}
}
func Test_Parameters(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.
Node("{a}").
Node("{b}").
Node("(^[0-9]+[A-Z]$)").
Node("{{*}}").
Method("GET", func(c *Context) {
json.NewEncoder(c.Response).Encode(c.Parameters)
})
response := world.Request("GET", "/1/2/33Z/444/555/666").Do()
obtainedBody := response.BodyJson()
expectedBody := map[string]interface{}{
"a": "1",
"b": "2",
"^[0-9]+[A-Z]$": "33Z",
"*": "444/555/666",
}
if !reflect.DeepEqual(obtainedBody, expectedBody) {
t.Error("Parameters are not being collected")
}
}
func Test_Parameters_collision(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.Node("{a}").Node("{a}").Method("GET", func(c *Context) {
json.NewEncoder(c.Response).Encode(c.Parameters)
})
response := world.Request("GET", "/1/2").Do()
obtainedBody := response.BodyJson()
expectedBody := map[string]interface{}{
"a": "2",
}
if !reflect.DeepEqual(obtainedBody, expectedBody) {
t.Error("Parameters are not being collected")
}
}
/**
* The users node has two nodes in order:
* - stats
* - {user_id}
* GET /users/stats should return 200 `There are 2000 users`
* GET /users/1231 should return 200 `User 1231`
* Get /users/9999 should return 404 `User 9999 does not exist`
*/
func Test_Parameter_precedence(t *testing.T) {
world := NewWorld()
defer world.Destroy()
root := world.Api.Root
root.Interceptor(&Interceptor{
After: func(c *Context) {
if nil != c.LastError {
c.Response.WriteHeader(c.LastError.StatusCode)
fmt.Fprint(c.Response, c.LastError.Description)
}
},
})
users := root.Node("users")
stats := users.Node("stats")
stats.Method("GET", func(c *Context) {
fmt.Fprint(c.Response, "There are 2000 users")
})
user := users.Node("{user_id}")
user.Method("GET", func(c *Context) {
userID, _ := strconv.Atoi(c.Parameter)
if userID > 2000 {
c.Error(404, "User "+c.Parameter+" does not exist")
return
}
fmt.Fprint(c.Response, "User "+c.Parameter)
})
response1 := world.Request("GET", "/users/stats").Do()
if 200 != response1.StatusCode {
t.Error("Status code `200` is expected")
}
if "There are 2000 users" != response1.BodyString() {
t.Error("Body `There are 2000 users` is expected")
}
response2 := world.Request("GET", "/users/1231").Do()
if 200 != response2.StatusCode {
t.Error("Status code `200` is expected")
}
if "User 1231" != response2.BodyString() {
t.Error("Body `User 1231` is expected")
}
response3 := world.Request("GET", "/users/9999").Do()
if 404 != response3.StatusCode {
t.Error("Status code `404` is expected")
}
if "User 9999 does not exist" != response3.BodyString() {
t.Error("Body `User 9999 does not exist` is expected")
}
}
/**
* https://github.com/fulldump/golax/issues/5
* If a parameter is not the last one, it is not possible getting its value
*/
func Test_ParameterBug_issue_5(t *testing.T) {
world := NewWorld()
defer world.Destroy()
myInterceptor := &Interceptor{
Before: func(c *Context) {
c.Set("my_parameter", c.Parameter)
},
}
getProfile := func(c *Context) {
myParameter, _ := c.Get("my_parameter")
fmt.Fprint(c.Response, "parameter: "+myParameter.(string))
}
world.Api.Root.
Node("users").
Node("{aa}").
Interceptor(myInterceptor).
Node("profile").Method("GET", getProfile)
response := world.Request("GET", "/users/-the-value-/profile").Do()
body := response.BodyString()
if "parameter: -the-value-" != body {
t.Error("Body does not match")
}
}
func Test_handling(t *testing.T) {
world := NewWorld()
defer world.Destroy()
wrapper := func(text string) *Interceptor {
return &Interceptor{
Before: func(c *Context) {
fmt.Println(text)
fmt.Fprintf(c.Response, "%s(", text)
},
After: func(c *Context) {
fmt.Println("/" + text)
fmt.Fprintf(c.Response, ")%s", text)
},
}
}
root := world.Api.Root
root.Interceptor(wrapper("root"))
a := root.Node("a")
a.Interceptor(wrapper("a"))
b := a.Node("b")
b.Interceptor(wrapper("b"))
c := b.Node("c")
c.Interceptor(wrapper("c"))
c.Method("GET", func(c *Context) {
fmt.Println("Hello world, I am C")
fmt.Fprint(c.Response, "Hello world, I am C")
})
response := world.Request("GET", "/a/b/c").Do()
body := response.BodyString()
if "root(a(b(c(Hello world, I am C)c)b)a)root" != body {
t.Error("Body does not match")
}
}
func Test_RegexParameter_ok(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.Node("(^a+$)").Method("GET", func(c *Context) {
fmt.Fprint(c.Response, "a+:", c.Parameter)
})
world.Api.Root.Node("(^b+$)").Method("GET", func(c *Context) {
fmt.Fprint(c.Response, "b+:", c.Parameter)
})
world.Api.Root.Node("abba").Method("GET", func(c *Context) {
fmt.Fprint(c.Response, "static:abba")
})
r1 := world.Request("GET", "/a").Do()
if "a+:a" != r1.BodyString() {
t.Error("r1: Body a+ does not match")
}
r2 := world.Request("GET", "/bbbbb").Do()
if "b+:bbbbb" != r2.BodyString() {
t.Error("Body b+ does not match")
}
r3 := world.Request("GET", "/abba").Do()
if "static:abba" != r3.BodyString() {
t.Error("Body abba does not match")
}
r4 := world.Request("GET", "/").Do()
if 405 != r4.StatusCode {
t.Error("r4: Status code does not match")
}
}
func Test_FullPath(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Prefix = "/service"
world.Api.Root.Node("files").Node("{{*}}").Method(
"GET",
func(c *Context) {
fmt.Fprint(c.Response, c.Parameter)
},
)
response := world.Request("GET", "/service/files/static/docs/document.txt").Do()
body := response.BodyString()
if "static/docs/document.txt" != body {
t.Error("Parameter does not match")
}
}
func alwaysBreak(name string) *Interceptor {
return &Interceptor{
Before: func(c *Context) {
c.Response.Header().Add("Interceptors", "[BREAK "+name+"]")
c.Error(666, "Break "+name)
},
}
}
func alwaysWork(name string) *Interceptor {
return &Interceptor{
Before: func(c *Context) {
c.Response.Header().Add("Interceptors", "[WORK "+name+"]")
},
}
}
func Test_Interceptors_ErrorChain0(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.
Interceptor(alwaysWork("1")).
Interceptor(alwaysWork("2")).
Interceptor(alwaysBreak("Z")).
Method("GET", func(c *Context) {
c.Response.Header().Add("Interceptors", "[ROOT]")
}).
Node("node").
Interceptor(alwaysWork("3")).
Interceptor(alwaysWork("4")).
Method("GET", func(c *Context) {
c.Response.Header().Add("Interceptors", "[NODE]")
})
r := world.Request("GET", "/node").Do()
status := r.StatusCode
chain := strings.Join(r.Header["Interceptors"], "")
fmt.Println(r.StatusCode, chain)
if 666 != status {
t.Error("Status code should be 666")
}
if "[WORK 1][WORK 2][BREAK Z]" != chain {
t.Error("Chain does not match")
}
}
func Test_Interceptors_ErrorChain1(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.
Interceptor(alwaysWork("1")).
Interceptor(alwaysBreak("Z")).
Interceptor(alwaysWork("2")).
Method("GET", func(c *Context) {
c.Response.Header().Add("Interceptors", "[ROOT]")
}).
Node("node").
Interceptor(alwaysWork("3")).
Interceptor(alwaysWork("4")).
Method("GET", func(c *Context) {
c.Response.Header().Add("Interceptors", "[NODE]")
})
r := world.Request("GET", "/node").Do()
status := r.StatusCode
chain := strings.Join(r.Header["Interceptors"], "")
fmt.Println(r.StatusCode, chain)
if 666 != status {
t.Error("Status code should be 666")
}
if "[WORK 1][BREAK Z]" != chain {
t.Error("Chain does not match")
}
}
func Test_Interceptors_ErrorChain2(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.
Interceptor(alwaysWork("1")).
Interceptor(alwaysWork("2")).
Method("GET", func(c *Context) {
c.Response.Header().Add("Interceptors", "[ROOT]")
}).
Node("node").
Interceptor(alwaysWork("3")).
Interceptor(alwaysWork("4")).
Method("GET", func(c *Context) {
c.Response.Header().Add("Interceptors", "[NODE]")
})
r := world.Request("GET", "/node").Do()
status := r.StatusCode
chain := strings.Join(r.Header["Interceptors"], "")
fmt.Println(r.StatusCode, chain)
if 200 != status {
t.Error("Status code should be 666")
}
if "[WORK 1][WORK 2][WORK 3][WORK 4][NODE]" != chain {
t.Error("Chain does not match")
}
}
func Test_Interceptors_ErrorChain3(t *testing.T) {
world := NewWorld()
defer world.Destroy()
world.Api.Root.
Interceptor(alwaysWork("1")).
Interceptor(alwaysWork("2")).
Method("GET", func(c *Context) {
c.Response.Header().Add("Interceptors", "[ROOT]")
}).
Node("node").
Interceptor(alwaysWork("3")).
Interceptor(alwaysBreak("Z")).
Interceptor(alwaysWork("4")).
Method("GET", func(c *Context) {
c.Response.Header().Add("Interceptors", "[NODE]")
})
r := world.Request("GET", "/node").Do()
status := r.StatusCode
chain := strings.Join(r.Header["Interceptors"], "")
fmt.Println(r.StatusCode, chain)
if 666 != status {
t.Error("Status code should be 666")
}
if "[WORK 1][WORK 2][WORK 3][BREAK Z]" != chain {
t.Error("Chain does not match")
}
}