-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_test.go
More file actions
125 lines (95 loc) · 3.89 KB
/
core_test.go
File metadata and controls
125 lines (95 loc) · 3.89 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
package gatewayflowcontroller_test
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
flowcontroller "github.com/RSS3-Network/gatewayflowcontroller"
)
func TestCore(t *testing.T) {
t.Parallel()
// Prepare plugin
cfg := flowcontroller.CreateConfig()
ctx := context.Background()
nextHandlerCalled := false
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { nextHandlerCalled = true })
handler, err := flowcontroller.New(ctx, next, cfg, "gateway-flowcontroller-core")
if err != nil {
t.Fatal(fmt.Errorf("initialize plugin: %w", err))
}
// Everything is ready, let's start requesting
requestCountPerRound := cfg.Burst * 2 // Ensure exceeds burst stage
requestMethod := http.MethodGet
requestURL := "http://localhost/gateway-flowcontroller"
corePhase1(ctx, t, cfg, handler, &nextHandlerCalled, requestCountPerRound, requestMethod, requestURL)
corePhase2(ctx, t, cfg, handler, &nextHandlerCalled, requestCountPerRound, requestMethod, requestURL)
t.Log("rate limiter only test finish")
}
func corePhase1(ctx context.Context, t *testing.T, cfg *flowcontroller.Config, handler http.Handler, nextHandlerCalled *bool, requestCountPerRound int64, requestMethod string, requestURL string) {
// Phase 1: without key
for i := int64(0); i < requestCountPerRound; i++ {
req, err := http.NewRequestWithContext(ctx, requestMethod, requestURL, nil)
if err != nil {
t.Fatal(fmt.Errorf("prepare request: %w", err))
}
req.Header.Set("X-Forwarded-For", "127.0.2.1, 192.168.0.1, 172.16.35.4")
req.RemoteAddr = "127.0.2.1:12553"
recorder := httptest.NewRecorder()
*nextHandlerCalled = false
// Make request
handler.ServeHTTP(recorder, req)
res := recorder.Result()
//t.Log(fmt.Sprintf("request %d, status %d, Retry-After: %s, X-Retry-In: %s", i, res.StatusCode, res.Header.Get("Retry-After"), res.Header.Get("X-Retry-In")))
if i < cfg.Burst {
if res.StatusCode != http.StatusOK {
t.Error(fmt.Errorf("phase 1: unexpected status (%d) at response (%d)", res.StatusCode, i))
}
if !*nextHandlerCalled {
t.Error(fmt.Errorf("phase 1: next handler not called at response (%d)", i))
}
} else {
if res.StatusCode != http.StatusTooManyRequests {
t.Error(fmt.Errorf("phase 1: unexpected status (%d) at response (%d)", res.StatusCode, i))
}
if *nextHandlerCalled {
t.Error(fmt.Errorf("phase 1: next handler called at response (%d)", i))
}
}
_ = res.Body.Close()
}
}
func corePhase2(ctx context.Context, t *testing.T, cfg *flowcontroller.Config, handler http.Handler, nextHandlerCalled *bool, requestCountPerRound int64, requestMethod string, requestURL string) {
// Phase 2: with invalid key
for i := int64(0); i < requestCountPerRound; i++ {
req, err := http.NewRequestWithContext(ctx, requestMethod, requestURL, nil)
if err != nil {
t.Fatal(fmt.Errorf("prepare request: %w", err))
}
req.Header.Set(cfg.KeyHeader, "some-meaningless-bytes")
req.Header.Set("X-Forwarded-For", "127.0.2.2, 192.168.0.1, 172.16.35.4")
req.RemoteAddr = "127.0.2.2:12553"
recorder := httptest.NewRecorder()
*nextHandlerCalled = false
// Make request
handler.ServeHTTP(recorder, req)
res := recorder.Result()
//t.Log(fmt.Sprintf("request %d, status %d, Retry-After: %s, X-Retry-In: %s", i, res.StatusCode, res.Header.Get("Retry-After"), res.Header.Get("X-Retry-In")))
if i < cfg.Burst {
if res.StatusCode != http.StatusOK {
t.Error(fmt.Errorf("phase 2: unexpected status (%d) at response (%d)", res.StatusCode, i))
}
if !*nextHandlerCalled {
t.Error(fmt.Errorf("phase 2: next handler not called at response (%d)", i))
}
} else {
if res.StatusCode != http.StatusTooManyRequests {
t.Error(fmt.Errorf("phase 2: unexpected status (%d) at response (%d)", res.StatusCode, i))
}
if *nextHandlerCalled {
t.Error(fmt.Errorf("phase 2: next handler called at response (%d)", i))
}
}
_ = res.Body.Close()
}
}