-
Notifications
You must be signed in to change notification settings - Fork 3
/
main_test.go
630 lines (534 loc) · 17.5 KB
/
main_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
// Copyright 2020-2023 Siemens AG
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
// SPDX-License-Identifier: MPL-2.0
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"runtime"
"sort"
"strings"
"testing"
"time"
"github.com/siemens/link-checker-service/infrastructure"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/siemens/link-checker-service/server"
"github.com/stretchr/testify/assert"
)
const firstRequest = `
{
"urls": [
{
"url":"https://google.com",
"context": "1"
},
{
"url":"https://ashasdfdfkjhdf.com/kajhsd",
"context": "2"
}
]
}
`
const secondRequest = `
{
"urls": [
{
"url":"https://bing.com",
"context": "1"
},
{
"url":"https://asdlfasdgisd.com/akdsjfsd",
"context": "2"
},
{
"url":"https://google.com",
"context": "3"
},
{
"url":"https://ashdfkjasdfhdf.com/kajhsd",
"context": "4"
}
]
}
`
const endpoint = "/checkUrls"
const statsEndpoint = "/stats"
const streamingEndpoint = "/checkUrls/stream"
func TestCommonCheckUrlsUsage(t *testing.T) {
setUpViperTestConfiguration()
testServer := server.NewServer()
router := testServer.Detail()
// first request
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
response := fireAndParseRequest(t, router, req)
assert.Len(t, response.Urls, 2)
// first response
okResponse1 := responseContaining("google", response)
assert.Equal(t, "1", okResponse1.Context)
assert.Equal(t, "", okResponse1.Error)
assert.Greater(t, okResponse1.ElapsedMs, int64(0))
// sleep for a bit
time.Sleep(2 * time.Second)
// second request
req, _ = http.NewRequest("POST", endpoint, strings.NewReader(secondRequest))
response = fireAndParseRequest(t, router, req)
assert.Len(t, response.Urls, 4)
// google.com
okResponse2 := responseContaining("google", response)
assert.Equal(t, "3", okResponse2.Context)
assert.Equal(t, okResponse2.FetchedAtEpochSeconds, okResponse1.FetchedAtEpochSeconds, "the response should have been cached")
newResponse := responseContaining("bing", response)
assert.Equal(t, "1", newResponse.Context)
assert.Greater(t, newResponse.FetchedAtEpochSeconds, okResponse1.FetchedAtEpochSeconds, "the new url should have been checked later than the cached one")
assert.Greater(t, infrastructure.GlobalStats().GetDomainStats().DomainStats["google.com"].Ok, int64(0))
}
func responseContaining(urlSubstring string, response server.CheckURLsResponse) server.URLStatusResponse {
for _, response := range response.Urls {
if strings.Contains(response.URL, urlSubstring) {
return response
}
}
return server.URLStatusResponse{}
}
func fireAndParseRequest(t *testing.T, router *gin.Engine, req *http.Request) server.CheckURLsResponse {
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, 200, w.Code)
response := unmarshalCheckURLsResponse(t, w)
return response
}
func setUpViperTestConfiguration() {
viper.SetEnvPrefix("LCS")
viper.Set("proxy", os.Getenv("LCS_PROXY"))
viper.Set("cacheUseRistretto", false)
}
func TestCORS(t *testing.T) {
setUpViperTestConfiguration()
const okOrigin = "http://localhost:8080"
const wrongOrigin = "http://localhost:80"
// start with CORS configuration
testServer := server.NewServerWithOptions(&server.Options{
CORSOrigins: []string{
okOrigin,
},
})
router := testServer.Detail()
// no Origin header
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "the call without an origin header should have succeeded")
// with a correct Origin header
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
req.Header.Add("Origin", okOrigin)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "correct origin should be allowed by CORS")
// with an incorrect Origin header
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
req.Header.Add("Origin", wrongOrigin)
router.ServeHTTP(w, req)
assert.NotEqual(t, http.StatusOK, w.Code, "incorrect origin header should be blocked by CORS")
// start without a CORS configuration
testServer = server.NewServer()
router = testServer.Detail()
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
req.Header.Add("Origin", wrongOrigin)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "default server should allow any origin")
}
func TestRateLimiting(t *testing.T) {
setUpViperTestConfiguration()
// start a rate-limited server
testServer := server.NewServerWithOptions(&server.Options{
IPRateLimit: "10-S",
})
router := testServer.Detail()
// cache the first request
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "first call should have succeeded")
for r := 0; r < 20; r++ {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
router.ServeHTTP(w, req)
if r > 10 {
assert.NotEqual(t, http.StatusOK, w.Code, "calls above 10 within a second should have failed")
}
}
// start a rate-unlimited server
testServer = server.NewServerWithOptions(&server.Options{
IPRateLimit: "",
})
router = testServer.Detail()
// all requests should succeed
for r := 0; r < 21; r++ {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code, "all requests should have succeeded")
}
}
func TestPayloadLimiting(t *testing.T) {
setUpViperTestConfiguration()
// start a rate-limited server
testServer := server.NewServerWithOptions(&server.Options{
MaxURLsInRequest: 2,
})
router := testServer.Detail()
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(secondRequest))
router.ServeHTTP(w, req)
assert.NotEqual(t, http.StatusOK, w.Code, "4 urls should not have been allowed")
}
func TestUrlBlacklisting(t *testing.T) {
setUpViperTestConfiguration()
testServer := server.NewServerWithOptions(&server.Options{
DomainBlacklistGlobs: []string{
"test?atter*.*",
},
})
router := testServer.Detail()
request := `
{
"urls": [
{
"url":"https://testpattern.com",
"context": "1"
},
{
"url":"https://google.com",
"context": "2"
}
]
}
`
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(request))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
response := unmarshalCheckURLsResponse(t, w)
assert.Len(t, response.Urls, 2)
sort.Slice(response.Urls, func(i, j int) bool {
return response.Urls[i].Context < response.Urls[j].Context
})
assert.Contains(t, response.Urls[0].Status, "skip")
assert.Contains(t, response.Urls[1].Status, "ok")
}
func TestBadRequests(t *testing.T) {
setUpViperTestConfiguration()
testServer := server.NewServer()
router := testServer.Detail()
// bad route
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/", nil)
router.ServeHTTP(w, req)
assert.NotEqual(t, http.StatusOK, w.Code, "request to / should not have worked")
// json parsing
badRequestBody := `<xml>not json</xml>`
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", endpoint, strings.NewReader(badRequestBody))
router.ServeHTTP(w, req)
assert.NotEqual(t, http.StatusOK, w.Code, "bad post body should not have worked")
// wrong method
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", endpoint, strings.NewReader(firstRequest))
router.ServeHTTP(w, req)
assert.NotEqual(t, http.StatusOK, w.Code, "GET with the correct request body should not have worked")
}
func TestDuplicateURLs(t *testing.T) {
setUpViperTestConfiguration()
viper.Set("maxConcurrentHTTPRequests", 1)
testServer := server.NewServer()
router := testServer.Detail()
request := `
{
"urls": [
{
"url":"https://google.com",
"context": "1"
},
{
"url":"https://google.com ",
"context": "2"
},
{
"url":"https://google.com/",
"context": "3"
}
]
}
`
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(request))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
response := unmarshalCheckURLsResponse(t, w)
assert.Len(t, response.Urls, 3)
assert.Equal(t, "ok", response.Urls[0].Status)
assert.Equal(t, "ok", response.Urls[1].Status)
assert.Equal(t, "ok", response.Urls[2].Status)
assert.NotEqual(t, response.Urls[0].Context, response.Urls[1].Context, "context should have been preserved")
assert.NotEqual(t, response.Urls[2].Context, response.Urls[1].Context, "context should have been preserved")
assert.NotEqual(t, response.Urls[0].URL, response.Urls[1].URL, "original URL should have been preserved")
assert.NotEqual(t, response.Urls[2].URL, response.Urls[1].URL, "original URL should have been preserved")
}
func TestBadResultsAreRecheckedAfterGracePeriod(t *testing.T) {
infrastructure.ResetGlobalStats()
setUpViperTestConfiguration()
viper.Set("retryFailedAfter", "30s")
testServer := server.NewServer()
router := testServer.Detail()
response1, response2 := fireTwoConsecutiveBadURLRequests(t, router)
assert.Equal(
t,
int64(1),
infrastructure.GlobalStats().GetStats().OutgoingRequests,
"there should have been only one call",
)
// assert
assert.Equal(
t,
response2.Urls[0].FetchedAtEpochSeconds,
response1.Urls[0].FetchedAtEpochSeconds,
"the result should have been cached",
)
viper.Set("retryFailedAfter", "0s")
testServer = server.NewServer()
router = testServer.Detail()
response1, response2 = fireTwoConsecutiveBadURLRequests(t, router)
// assert
assert.Greater(
t,
response2.Urls[0].FetchedAtEpochSeconds,
response1.Urls[0].FetchedAtEpochSeconds,
"the result should have re-fetched, as retryFailedAfter=0s grace period is configured",
)
}
func TestRistrettoCache(t *testing.T) {
setUpViperTestConfiguration()
viper.Set("cacheCleanupInterval", "1m")
viper.Set("cacheUseRistretto", true)
viper.Set("cacheMaxSize", 10 /*bytes, unrealistic*/)
testServer := server.NewServer()
router := testServer.Detail()
response1, response2 := fireTwoConsecutiveOkRequests(t, router)
// assert
assert.Greater(
t,
response2.Urls[0].FetchedAtEpochSeconds,
response1.Urls[0].FetchedAtEpochSeconds,
"the result should not have been cached, as the max cost should have been exceeded",
)
// now, set the maximum size higher
viper.Set("cacheMaxSize", 10_000_000)
testServer = server.NewServer()
router = testServer.Detail()
response1, response2 = fireTwoConsecutiveOkRequests(t, router)
// assert
assert.Equal(
t,
response2.Urls[0].FetchedAtEpochSeconds,
response1.Urls[0].FetchedAtEpochSeconds,
"the result should have been cached, as the cache now has a sufficient maximum size",
)
}
func fireTwoConsecutiveOkRequests(t *testing.T, router *gin.Engine) (server.CheckURLsResponse, server.CheckURLsResponse) {
const alwaysOkRequest = `
{
"urls": [
{
"url":"https://google.com",
"context": "1"
}
]
}
`
// first request
w := requestCheck(alwaysOkRequest, router)
assert.Equal(t, http.StatusOK, w.Code)
response1 := unmarshalCheckURLsResponse(t, w)
assert.Len(t, response1.Urls, 1)
assert.Equal(t, response1.Urls[0].Status, "ok")
// sleep a bit
time.Sleep(1 * time.Second)
// second request
w = requestCheck(alwaysOkRequest, router)
assert.Equal(t, http.StatusOK, w.Code)
response2 := unmarshalCheckURLsResponse(t, w)
assert.Len(t, response2.Urls, 1)
assert.Equal(t, response2.Urls[0].Status, "ok")
return response1, response2
}
func fireTwoConsecutiveBadURLRequests(t *testing.T, router *gin.Engine) (server.CheckURLsResponse, server.CheckURLsResponse) {
request := `
{
"urls": [
{
"url":"https://ajdhfsadflkjhasdf.com",
"context": "0"
}
]
}
`
// first request
w := requestCheck(request, router)
assert.Equal(t, http.StatusOK, w.Code)
response1 := unmarshalCheckURLsResponse(t, w)
assert.Len(t, response1.Urls, 1)
assert.NotEqual(t, response1.Urls[0].Status, "ok")
// sleep a bit
time.Sleep(1 * time.Second)
// second request
w = requestCheck(request, router)
assert.Equal(t, http.StatusOK, w.Code)
response2 := unmarshalCheckURLsResponse(t, w)
assert.Len(t, response2.Urls, 1)
assert.NotEqual(t, response2.Urls[0].Status, "ok")
return response1, response2
}
func requestCheck(request string, router *gin.Engine) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(request))
router.ServeHTTP(w, req)
return w
}
func unmarshalCheckURLsResponse(t *testing.T, w *httptest.ResponseRecorder) server.CheckURLsResponse {
response := server.CheckURLsResponse{}
err := json.Unmarshal(w.Body.Bytes(), &response)
assert.NoError(t, err, "unmarshalling the response should have worked")
return response
}
func TestEmptyRequestsShouldFail(t *testing.T) {
setUpViperTestConfiguration()
testServer := server.NewServer()
router := testServer.Detail()
// async
w := newCloseNotifyRecorder()
req := httptest.NewRequest("POST", streamingEndpoint, strings.NewReader(`{"urls": []}`))
router.ServeHTTP(w, req)
body := w.Body.String()
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, strings.ToLower(body), "no")
// sync
w2 := httptest.NewRecorder()
router.ServeHTTP(w2, req)
body = w.Body.String()
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, strings.ToLower(body), "no")
// empty urls
req = httptest.NewRequest("POST", streamingEndpoint, strings.NewReader(`{}`))
w2 = httptest.NewRecorder()
router.ServeHTTP(w2, req)
body = w.Body.String()
assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, strings.ToLower(body), "no")
}
func TestStreamingResponse(t *testing.T) {
setUpViperTestConfiguration()
testServer := server.NewServer()
router := testServer.Detail()
w := newCloseNotifyRecorder()
req := httptest.NewRequest("POST", streamingEndpoint, strings.NewReader(firstRequest))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
if http.StatusOK != w.Code {
// "didn't continue -> abort
t.Fail()
return
}
body := w.Body.String()
// both contexts:
assert.Contains(t, body, "\"1\"")
assert.Contains(t, body, "\"2\"")
}
func TestJWTAuthentication(t *testing.T) {
setUpViperTestConfiguration()
pubKey, privKey, priv := createTestCertificates()
testServer := server.NewServerWithOptions(&server.Options{
JWTValidationOptions: &server.JWTValidationOptions{
PrivKeyFile: privKey,
PubKeyFile: pubKey,
SigningAlgorithm: "RS384",
},
})
router := testServer.Detail()
// /version is not authenticated
w := httptest.NewRecorder()
versionReq, _ := http.NewRequest("GET", "/version", nil)
router.ServeHTTP(w, versionReq)
assert.Equal(t, http.StatusOK, w.Code, "the version endpoint should not be authenticated")
// missing authentication
w = httptest.NewRecorder()
req, _ := http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnauthorized, w.Code, "the call without a bearer token should fail")
// /stats is authenticated
w = httptest.NewRecorder()
req, _ = http.NewRequest("GET", statsEndpoint, strings.NewReader(firstRequest))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnauthorized, w.Code, "the call to /stats without a bearer token should fail")
// correct token
token, _ := createJWTToken(priv)
req, _ = http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
req.Header.Add("Authorization", "Bearer "+token)
response := fireAndParseRequest(t, router, req)
assert.Len(t, response.Urls, 2)
// bad token
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", endpoint, strings.NewReader(firstRequest))
req.Header.Add("Authorization", "Bearer !bad!"+token)
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusUnauthorized, w.Code, "A bad JWT token should not have been valid")
// cleanup
_ = os.Remove(privKey)
_ = os.Remove(pubKey)
}
func TestQuickResultsShouldArriveFirst(t *testing.T) {
if runtime.GOMAXPROCS(-1) < 2 {
t.Skip("To avoid sporadic failure, running this only when sufficient parallelism is given")
return
}
setUpViperTestConfiguration()
// first
viper.Set("urlCheckerPlugins", []string{"_ok_after_1s_on_delay.com"})
testServer := server.NewServer()
router := testServer.Detail()
// async
w := newCloseNotifyRecorder()
// first request should be delayed, and thus come second in the response
req := httptest.NewRequest("POST", streamingEndpoint, strings.NewReader(
strings.ReplaceAll(firstRequest, "google", "delay")))
router.ServeHTTP(w, req)
assert.Equal(t, http.StatusOK, w.Code)
if http.StatusOK != w.Code {
// "didn't continue -> abort
t.Fail()
return
}
body := w.Body.String()
responses := parseStreamingResponses(t, body)
assert.Len(t, responses, 2)
assert.NotContains(t, responses[0].URL, "delay")
assert.Contains(t, responses[1].URL, "delay")
}
func parseStreamingResponses(t *testing.T, body string) []*server.URLStatusResponse {
var res []*server.URLStatusResponse
for _, line := range strings.Split(body, "\n") {
if line != "" {
response := server.URLStatusResponse{}
err := json.Unmarshal([]byte(line), &response)
assert.NoError(t, err, "unmarshalling the response should have worked")
res = append(res, &response)
}
}
return res
}