forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
699 lines (580 loc) · 16.7 KB
/
api_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
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
697
698
699
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/garyburd/redigo/redis"
"github.com/gorilla/mux"
"github.com/TykTechnologies/tyk/apidef"
)
const apiTestDef = `{
"api_id": "1",
"org_id": "default",
"definition": {
"location": "header",
"key": "version"
},
"auth": {
"auth_header_name": "authorization"
},
"version_data": {
"versions": {
"Default": {
"name": "Default"
}
}
},
"proxy": {
"listen_path": "/v1",
"target_url": "` + testHttpAny + `"
}
}`
func loadSampleAPI(t *testing.T, def string) {
spec := createSpecTest(t, def)
loadApps([]*APISpec{spec}, discardMuxer)
}
type testAPIDefinition struct {
apidef.APIDefinition
ID string `json:"id"`
}
func TestHealthCheckEndpoint(t *testing.T) {
uri := "/tyk/health/?api_id=1"
recorder := httptest.NewRecorder()
loadSampleAPI(t, apiTestDef)
req := testReq(t, "GET", uri, nil)
healthCheckhandler(recorder, req)
if recorder.Code != 200 {
t.Error("Recorder should return 200 for health check")
}
}
func createSampleSession() *SessionState {
return &SessionState{
Rate: 5.0,
Allowance: 5.0,
LastCheck: time.Now().Unix(),
Per: 8.0,
QuotaRenewalRate: 300, // 5 minutes
QuotaRenews: time.Now().Unix(),
QuotaRemaining: 10,
QuotaMax: 10,
AccessRights: map[string]AccessDefinition{
"1": {
APIName: "Test",
APIID: "1",
Versions: []string{"Default"},
},
},
}
}
func TestApiHandler(t *testing.T) {
uris := []string{"/tyk/apis/", "/tyk/apis"}
for _, uri := range uris {
sampleKey := createSampleSession()
recorder := httptest.NewRecorder()
loadSampleAPI(t, apiTestDef)
req := withAuth(testReq(t, "GET", uri, sampleKey))
mainRouter.ServeHTTP(recorder, req)
// We can't deserialize BSON ObjectID's if they are not in th test base!
var apiList []testAPIDefinition
json.NewDecoder(recorder.Body).Decode(&apiList)
if len(apiList) != 1 {
t.Error("API's not returned, len was: \n", len(apiList), recorder.Body.String(), uri)
} else if apiList[0].APIID != "1" {
t.Error("Response is incorrect - no API ID value in struct :\n", recorder.Body.String(), uri)
}
}
}
func TestApiHandlerGetSingle(t *testing.T) {
uri := "/tyk/apis/1"
sampleKey := createSampleSession()
recorder := httptest.NewRecorder()
loadSampleAPI(t, apiTestDef)
req := withAuth(testReq(t, "GET", uri, sampleKey))
mainRouter.ServeHTTP(recorder, req)
// We can't deserialize BSON ObjectID's if they are not in th test base!
var apiDef testAPIDefinition
json.NewDecoder(recorder.Body).Decode(&apiDef)
if apiDef.APIID != "1" {
t.Error("Response is incorrect - no API ID value in struct :\n", recorder.Body.String())
}
}
func TestApiHandlerPost(t *testing.T) {
uri := "/tyk/apis/1"
recorder := httptest.NewRecorder()
req := withAuth(testReq(t, "POST", uri, apiTestDef))
mainRouter.ServeHTTP(recorder, req)
var success APIModifyKeySuccess
json.NewDecoder(recorder.Body).Decode(&success)
if success.Status != "ok" {
t.Error("Response is incorrect - not success :\n", recorder.Body.String())
}
}
func TestApiHandlerPostDupPath(t *testing.T) {
specs := func() (res []*APISpec) {
for _, id := range []string{"2", "3"} {
def := strings.Replace(apiTestDef, `"1"`, `"`+id+`"`, 1)
res = append(res, createSpecTest(t, def))
}
return res
}
var s2, s3 *APISpec
// both dups added at the same time
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
loadApps(specs(), discardMuxer)
s2 = getApiSpec("2")
if want, got := "/v1-2", s2.Proxy.ListenPath; want != got {
t.Errorf("API spec %s want path %s, got %s", "2", want, got)
}
s3 = getApiSpec("3")
if want, got := "/v1-3", s3.Proxy.ListenPath; want != got {
t.Errorf("API spec %s want path %s, got %s", "3", want, got)
}
// one dup was there first, gets to keep its path. apiids are
// not used to mandate priority. survives multiple reloads too.
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
loadApps(specs()[1:], discardMuxer)
loadApps(specs(), discardMuxer)
loadApps(specs(), discardMuxer)
s2 = getApiSpec("2")
if want, got := "/v1-2", s2.Proxy.ListenPath; want != got {
t.Errorf("API spec %s want path %s, got %s", "2", want, got)
}
s3 = getApiSpec("3")
if want, got := "/v1", s3.Proxy.ListenPath; want != got {
t.Errorf("API spec %s want path %s, got %s", "3", want, got)
}
// both dups were there first, neither gets to keep its original
// path.
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
loadApps(specs(), discardMuxer)
loadApps(specs(), discardMuxer)
s2 = getApiSpec("2")
if want, got := "/v1-2", s2.Proxy.ListenPath; want != got {
t.Errorf("API spec %s want path %s, got %s", "2", want, got)
}
s3 = getApiSpec("3")
if want, got := "/v1-3", s3.Proxy.ListenPath; want != got {
t.Errorf("API spec %s want path %s, got %s", "3", want, got)
}
}
func TestApiHandlerPostDbConfig(t *testing.T) {
uri := "/tyk/apis/1"
globalConf.UseDBAppConfigs = true
defer func() { globalConf.UseDBAppConfigs = false }()
recorder := httptest.NewRecorder()
req := withAuth(testReq(t, "POST", uri, apiTestDef))
mainRouter.ServeHTTP(recorder, req)
var success APIModifyKeySuccess
json.NewDecoder(recorder.Body).Decode(&success)
if success.Status == "ok" {
t.Error("Response is incorrect - expected error due to use_db_app_config :\n", recorder.Body.String())
}
}
func TestApiHandlerMethodAPIID(t *testing.T) {
base := "/tyk/apis"
tests := [...]struct {
method, path string
code int
}{
// GET and POST can do either
{"GET", "/", 200},
{"GET", "/missing", 404},
{"POST", "/", 200},
{"POST", "/1", 200},
// DELETE and PUT must use one
{"DELETE", "/1", 200},
{"DELETE", "/", 400},
{"PUT", "/1", 200},
{"PUT", "/", 400},
// apiid mismatch
{"POST", "/mismatch", 400},
{"PUT", "/mismatch", 400},
}
for _, tc := range tests {
recorder := httptest.NewRecorder()
url := base + tc.path
req := withAuth(testReq(t, tc.method, url, apiTestDef))
mainRouter.ServeHTTP(recorder, req)
if tc.code != recorder.Code {
t.Errorf("%s %s got %d, want %d", tc.method, url,
recorder.Code, tc.code)
}
}
}
func TestKeyHandlerNewKey(t *testing.T) {
for _, api_id := range []string{"1", "none", ""} {
uri := "/tyk/keys/1234"
sampleKey := createSampleSession()
recorder := httptest.NewRecorder()
param := make(url.Values)
loadSampleAPI(t, apiTestDef)
if api_id != "" {
param.Set("api_id", api_id)
}
req := withAuth(testReq(t, "POST", uri+param.Encode(), sampleKey))
mainRouter.ServeHTTP(recorder, req)
newSuccess := APIModifyKeySuccess{}
json.NewDecoder(recorder.Body).Decode(&newSuccess)
if newSuccess.Status != "ok" {
t.Error("key not created, status error:\n", recorder.Body.String())
}
if newSuccess.Action != "added" {
t.Error("Response is incorrect - action is not 'added' :\n", recorder.Body.String())
}
}
}
func TestKeyHandlerUpdateKey(t *testing.T) {
for _, api_id := range []string{"1", "none", ""} {
uri := "/tyk/keys/1234"
sampleKey := createSampleSession()
recorder := httptest.NewRecorder()
param := make(url.Values)
loadSampleAPI(t, apiTestDef)
if api_id != "" {
param.Set("api_id", api_id)
}
req := withAuth(testReq(t, "PUT", uri+param.Encode(), sampleKey))
mainRouter.ServeHTTP(recorder, req)
newSuccess := APIModifyKeySuccess{}
json.NewDecoder(recorder.Body).Decode(&newSuccess)
if newSuccess.Status != "ok" {
t.Error("key not created, status error:\n", recorder.Body.String())
}
if newSuccess.Action != "modified" {
t.Error("Response is incorrect - action is not 'modified' :\n", recorder.Body.String())
}
}
}
func TestKeyHandlerGetKey(t *testing.T) {
for _, pathSuffix := range []string{"/", "/1234"} {
for _, api_id := range []string{"1", "none", ""} {
loadSampleAPI(t, apiTestDef)
createKey(t)
uri := "/tyk/keys" + pathSuffix
recorder := httptest.NewRecorder()
param := make(url.Values)
if api_id != "" {
param.Set("api_id", api_id)
}
req := withAuth(testReq(t, "GET", uri+"?"+param.Encode(), nil))
mainRouter.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Error("key not requested, status error:\n", recorder.Body.String())
}
}
}
}
func createKey(t *testing.T) {
uri := "/tyk/keys/1234"
sampleKey := createSampleSession()
recorder := httptest.NewRecorder()
req := withAuth(testReq(t, "POST", uri, sampleKey))
mainRouter.ServeHTTP(recorder, req)
}
func TestKeyHandlerDeleteKey(t *testing.T) {
for _, api_id := range []string{"1", "none", ""} {
createKey(t)
uri := "/tyk/keys/1234?"
recorder := httptest.NewRecorder()
param := make(url.Values)
loadSampleAPI(t, apiTestDef)
if api_id != "" {
param.Set("api_id", api_id)
}
req := withAuth(testReq(t, "DELETE", uri+param.Encode(), nil))
mainRouter.ServeHTTP(recorder, req)
newSuccess := APIModifyKeySuccess{}
json.NewDecoder(recorder.Body).Decode(&newSuccess)
if newSuccess.Status != "ok" {
t.Error("key not deleted, status error:\n", recorder.Body.String())
}
if newSuccess.Action != "deleted" {
t.Error("Response is incorrect - action is not 'deleted' :\n", recorder.Body.String())
}
}
}
func TestMethodNotSupported(t *testing.T) {
recorder := httptest.NewRecorder()
req := withAuth(testReq(t, "POST", "/tyk/reload/", nil))
mainRouter.ServeHTTP(recorder, req)
if recorder.Code != 405 {
t.Fatal(`Wanted response to be 405 since the wrong method was used`)
}
}
func TestCreateKeyHandlerCreateNewKey(t *testing.T) {
for _, api_id := range []string{"1", "none", ""} {
createKey(t)
uri := "/tyk/keys/create"
sampleKey := createSampleSession()
recorder := httptest.NewRecorder()
param := make(url.Values)
loadSampleAPI(t, apiTestDef)
if api_id != "" {
param.Set("api_id", api_id)
}
req := withAuth(testReq(t, "POST", uri+param.Encode(), sampleKey))
mainRouter.ServeHTTP(recorder, req)
newSuccess := APIModifyKeySuccess{}
json.NewDecoder(recorder.Body).Decode(&newSuccess)
if newSuccess.Status != "ok" {
t.Error("key not created, status error:\n", recorder.Body.String())
}
if newSuccess.Action != "added" {
t.Error("Response is incorrect - action is not 'create' :\n", recorder.Body.String())
}
}
}
func TestAPIAuthFail(t *testing.T) {
uri := "/tyk/health/?api_id=1"
loadSampleAPI(t, apiTestDef)
recorder := httptest.NewRecorder()
req := testReq(t, "GET", uri, nil)
req.Header.Set("x-tyk-authorization", "12345")
mainRouter.ServeHTTP(recorder, req)
if recorder.Code == 200 {
t.Error("Access to API should have been blocked, but response code was: ", recorder.Code)
}
}
func TestAPIAuthOk(t *testing.T) {
uri := "/tyk/health/?api_id=1"
recorder := httptest.NewRecorder()
req := withAuth(testReq(t, "GET", uri, nil))
loadSampleAPI(t, apiTestDef)
mainRouter.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Error("Access to API should have gone through, but response code was: ", recorder.Code)
}
}
func TestInvalidateCache(t *testing.T) {
for _, suffix := range []string{"", "/"} {
loadSampleAPI(t, apiTestDef)
// TODO: Note that this test is fairly dumb, as it doesn't check
// that the cache is empty and will pass even if the apiID does
// not exist. This test should be improved to check that the
// endpoint actually did what it's supposed to.
rec := httptest.NewRecorder()
req := withAuth(testReq(t, "DELETE", "/tyk/cache/1"+suffix, nil))
mainRouter.ServeHTTP(rec, req)
if rec.Code != 200 {
t.Errorf("Could not invalidate cache: %v\n%v", rec.Code, rec.Body)
}
}
}
func TestGetOAuthClients(t *testing.T) {
testAPIID := "1"
var responseCode int
_, responseCode = getOauthClients(testAPIID)
if responseCode != 400 {
t.Fatal("Retrieving OAuth clients from nonexistent APIs must return error.")
}
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisByID[testAPIID] = &APISpec{}
apisMu.Unlock()
_, responseCode = getOauthClients(testAPIID)
if responseCode != 400 {
t.Fatal("Retrieving OAuth clients from APIs with no OAuthManager must return an error.")
}
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
}
func TestResetHandler(t *testing.T) {
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
loadSampleAPI(t, apiTestDef)
recorder := httptest.NewRecorder()
req := testReq(t, "GET", "/tyk/reload", nil)
var wg sync.WaitGroup
wg.Add(1)
resetHandler(wg.Done)(recorder, req)
if recorder.Code != 200 {
t.Fatal("Hot reload failed, response code was: ", recorder.Code)
}
reloadTick <- time.Time{}
wg.Wait()
apisMu.RLock()
if len(apisByID) == 0 {
t.Fatal("Hot reload was triggered but no APIs were found.")
}
apisMu.RUnlock()
}
func TestGroupResetHandler(t *testing.T) {
didSubscribe := make(chan bool)
didReload := make(chan bool)
cacheStore := RedisClusterStorageManager{}
cacheStore.Connect()
go func() {
err := cacheStore.StartPubSubHandler(RedisPubSubChannel, func(v interface{}) {
switch x := v.(type) {
case redis.Subscription:
didSubscribe <- true
case redis.Message:
notf := Notification{}
if err := json.Unmarshal(x.Data, ¬f); err != nil {
t.Fatal(err)
}
if notf.Command == NoticeGroupReload {
didReload <- true
}
}
})
if err != nil {
t.Log(err)
t.Fail()
close(didReload)
}
}()
uri := "/tyk/reload/group"
apisMu.Lock()
apisByID = make(map[string]*APISpec)
apisMu.Unlock()
loadSampleAPI(t, apiTestDef)
recorder := httptest.NewRecorder()
// If we don't wait for the subscription to be done, we might do
// the reload before pub/sub is in place to receive our message.
<-didSubscribe
req := withAuth(testReq(t, "GET", uri, nil))
mainRouter.ServeHTTP(recorder, req)
if recorder.Code != 200 {
t.Fatal("Hot reload (group) failed, response code was: ", recorder.Code)
}
apisMu.RLock()
if len(apisByID) == 0 {
t.Fatal("Hot reload (group) was triggered but no APIs were found.")
}
apisMu.RUnlock()
// We wait for the right notification (NoticeGroupReload), other
// type of notifications may be received during tests, as this
// is the cluster channel:
<-didReload
}
func TestHotReloadSingle(t *testing.T) {
oldRouter := mainRouter
var wg sync.WaitGroup
if !reloadURLStructure(wg.Done) {
t.Fatal("reload wasn't queued")
}
wg.Add(1)
reloadTick <- time.Time{}
wg.Wait()
if mainRouter == oldRouter {
t.Fatal("router wasn't swapped")
}
}
func TestHotReloadMany(t *testing.T) {
done := 0
var wg sync.WaitGroup
// Spike of 25 reloads all at once, not giving any time for the
// reload worker to pick up any of them. A single one is queued
// and waits.
for i := 0; i < 25; i++ {
if reloadURLStructure(wg.Done) {
wg.Add(1)
done++
}
}
if want := 1; done != want {
t.Fatalf("wanted actual reloads to be %d, was %d", want, done)
}
// pick it up and finish it
reloadTick <- time.Time{}
wg.Wait()
// 5 reloads, but this time slower - the reload worker has time
// to do all of them.
for i := 0; i < 5; i++ {
if reloadURLStructure(wg.Done) {
wg.Add(1)
done++
}
// pick it up and finish it
reloadTick <- time.Time{}
wg.Wait()
}
if want := 6; done != want {
t.Fatalf("wanted actual reloads to be %d, was %d", want, done)
}
}
const apiBenchDef = `{
"api_id": "REPLACE",
"org_id": "default",
"definition": {
"location": "header",
"key": "version"
},
"auth": {
"auth_header_name": "authorization"
},
"version_data": {
"not_versioned": true,
"versions": {
"Default": {
"name": "Default"
}
}
},
"proxy": {
"listen_path": "/listen-REPLACE",
"target_url": "` + testHttpAny + `"
}
}`
func BenchmarkApiReload(b *testing.B) {
specs := make([]*APISpec, 1000)
for i := range specs {
id := strconv.Itoa(i + 1)
def := strings.Replace(apiBenchDef, "REPLACE", id, -1)
spec := createDefinitionFromString(def)
specs[i] = spec
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
newMuxes := mux.NewRouter()
loadAPIEndpoints(newMuxes)
loadApps(specs, newMuxes)
}
}
func TestContextData(t *testing.T) {
r := new(http.Request)
if ctxGetData(r) != nil {
t.Fatal("expected ctxGetData to return nil")
}
ctxSetData(r, map[string]interface{}{"foo": "bar"})
if ctxGetData(r) == nil {
t.Fatal("expected ctxGetData to return non-nil")
}
defer func() {
if r := recover(); r == nil {
t.Fatal("expected ctxSetData of zero val to panic")
}
}()
ctxSetData(r, nil)
}
func TestContextSession(t *testing.T) {
r := new(http.Request)
if ctxGetSession(r) != nil {
t.Fatal("expected ctxGetSession to return nil")
}
ctxSetSession(r, &SessionState{})
if ctxGetSession(r) == nil {
t.Fatal("expected ctxGetSession to return non-nil")
}
defer func() {
if r := recover(); r == nil {
t.Fatal("expected ctxSetSession of zero val to panic")
}
}()
ctxSetSession(r, nil)
}