-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguard_test.go
71 lines (54 loc) · 1.64 KB
/
guard_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
package traefik_guard_test
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
guard "github.com/SimpaiX-net/traefik-guard"
)
func TestGuard(t *testing.T) {
config := guard.CreateConfig()
config.IPHeaders = []string{"cf-connecting-ip", "X-Forwarded-For"}
config.TTL = time.Duration(time.Hour * 6).String()
config.Timeout = time.Duration(time.Millisecond * 500).String()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
guard, err := guard.New(context.Background(), next, config, "guard@test")
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest(http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Add("cf-connecting-ip", "1.1.1.1")
recorder := httptest.NewRecorder()
guard.ServeHTTP(recorder, req)
for k, v := range req.Header {
if len(v) == 0 { continue }
t.Logf("%s: %v", k, v[0])
}
}
func BenchmarkGuard(b *testing.B) {
config := guard.CreateConfig()
config.IPHeaders = []string{"cf-connecting-ip", "X-Forwarded-For"}
config.TTL = time.Duration(time.Hour * 6).String()
config.Timeout = time.Duration(time.Millisecond * 200).String()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {})
guard, err := guard.New(context.Background(), next, config, "guard@test")
if err != nil {
b.Fatal(err)
}
b.RunParallel(func(p *testing.PB) {
for p.Next() {
req, err := http.NewRequest(http.MethodGet, "http://localhost", nil)
if err != nil {
b.Logf("error: %s", err)
continue
}
req.Header.Add("cf-connecting-ip", "1.1.1.1")
recorder := httptest.NewRecorder()
guard.ServeHTTP(recorder, req)
}
})
}