forked from a-kr/gor
/
integration_test.go
198 lines (149 loc) · 4.05 KB
/
integration_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
package main
import (
"fmt"
"net/http"
"strconv"
"sync/atomic"
"testing"
"time"
"github.com/buger/gor/listener"
"github.com/buger/gor/replay"
)
func isEqual(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Error("Original and Replayed request not match\n", a, "!=", b)
}
}
var envs int
type Env struct {
Verbose bool
ListenHandler http.HandlerFunc
ReplayHandler http.HandlerFunc
ReplayLimit int
ListenerLimit int
}
func (e *Env) start() (p int) {
p = 50000 + envs*10
go e.startHTTP(p, http.HandlerFunc(e.ListenHandler))
go e.startHTTP(p+2, http.HandlerFunc(e.ReplayHandler))
go e.startListener(p, p+1)
go e.startReplay(p+1, p+2)
// Time to start http and gor instances
time.Sleep(time.Millisecond * 100)
envs++
return
}
func (e *Env) startListener(port int, replayPort int) {
listener.Settings.Verbose = e.Verbose
listener.Settings.Address = "127.0.0.1"
listener.Settings.ReplayAddress = "127.0.0.1:" + strconv.Itoa(replayPort)
listener.Settings.Port = port
if e.ListenerLimit != 0 {
listener.Settings.ReplayLimit = e.ListenerLimit
}
listener.Run()
}
func (e *Env) startReplay(port int, forwardPort int) {
replay.Settings.Verbose = e.Verbose
replay.Settings.Host = "127.0.0.1"
replay.Settings.Address = "127.0.0.1:" + strconv.Itoa(port)
replay.Settings.ForwardAddress = "127.0.0.1:" + strconv.Itoa(forwardPort)
replay.Settings.Port = port
if e.ReplayLimit != 0 {
replay.Settings.ForwardAddress += "|" + strconv.Itoa(e.ReplayLimit)
}
replay.Run()
}
func (e *Env) startHTTP(port int, handler http.Handler) {
err := http.ListenAndServe(":"+strconv.Itoa(port), handler)
if err != nil {
fmt.Println("Error while starting http server:", err)
}
}
func getRequest(port int) *http.Request {
req, _ := http.NewRequest("GET", "http://localhost:"+strconv.Itoa(port)+"/test", nil)
ck1 := new(http.Cookie)
ck1.Name = "test"
ck1.Value = "value"
req.AddCookie(ck1)
return req
}
func TestReplay(t *testing.T) {
var request *http.Request
received := make(chan int)
listenHandler := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "OK", http.StatusAccepted)
}
replayHandler := func(w http.ResponseWriter, r *http.Request) {
isEqual(t, r.URL.Path, request.URL.Path)
if len(r.Cookies()) > 0 {
isEqual(t, r.Cookies()[0].Value, request.Cookies()[0].Value)
} else {
t.Error("Cookies should not be blank")
}
http.Error(w, "OK", http.StatusAccepted)
if t.Failed() {
fmt.Println("\nReplayed:", r, "\nOriginal:", request)
}
received <- 1
}
env := &Env{
Verbose: true,
ListenHandler: listenHandler,
ReplayHandler: replayHandler,
}
p := env.start()
request = getRequest(p)
_, err := http.DefaultClient.Do(request)
if err != nil {
t.Error("Can't make request", err)
}
select {
case <-received:
case <-time.After(time.Second):
t.Error("Timeout error")
}
}
func rateLimitEnv(replayLimit int, listenerLimit int, connCount int) int32 {
var processed int32
listenHandler := func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "OK", http.StatusAccepted)
}
replayHandler := func(w http.ResponseWriter, r *http.Request) {
atomic.AddInt32(&processed, 1)
http.Error(w, "OK", http.StatusAccepted)
}
env := &Env{
ListenHandler: listenHandler,
ReplayHandler: replayHandler,
ReplayLimit: replayLimit,
ListenerLimit: listenerLimit,
}
p := env.start()
req := getRequest(p)
for i := 0; i < connCount; i++ {
go func() {
http.DefaultClient.Do(req)
}()
}
time.Sleep(time.Millisecond * 500)
return processed
}
func TestWithoutReplayRateLimit(t *testing.T) {
processed := rateLimitEnv(0, 0, 10)
if processed != 10 {
t.Error("It should forward all requests without rate-limiting", processed)
}
}
func TestReplayRateLimit(t *testing.T) {
processed := rateLimitEnv(5, 0, 10)
if processed != 5 {
t.Error("It should forward only 5 requests with rate-limiting", processed)
}
}
func TestListenerRateLimit(t *testing.T) {
processed := rateLimitEnv(0, 3, 100)
if processed != 3 {
t.Error("It should forward only 3 requests with rate-limiting", processed)
}
}