-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhttp_test.go
344 lines (298 loc) · 7.22 KB
/
http_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
package httpcache
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"sync/atomic"
"testing"
"time"
"github.com/krakendio/httpcache"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/encoding"
"github.com/luraproject/lura/v2/proxy"
"github.com/luraproject/lura/v2/transport/http/client"
)
var maxRequests = 100
func tearDown() {
globalCache = nil
globalLruCache = nil
}
func TestClient_shared(t *testing.T) {
testCases := []struct {
Name string
Cfg map[string]interface{}
}{
{
Name: "shared memory cache",
Cfg: map[string]interface{}{
"shared": true,
},
},
{
Name: "shared LRU cache",
Cfg: map[string]interface{}{
"shared": true,
"max_items": 10,
"max_size": 100000,
},
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
globalCache = httpcache.NewMemoryCache()
defer tearDown()
cfg := &config.Backend{
Decoder: encoding.JSONDecoder,
ExtraConfig: map[string]interface{}{
Namespace: testCase.Cfg,
},
}
b := newBackend(300)
defer b.Close()
testClient(t, cfg, b.URL(), maxRequests)
testClient(t, cfg, b.URL(), maxRequests)
testClient(t, cfg, b.URL(), maxRequests)
testClient(t, cfg, b.URL(), maxRequests)
if hits := b.Count(); hits != 1 {
t.Errorf("unexpected number of hits. got: %d, want: 1", hits)
}
})
}
}
func TestClient_refresh(t *testing.T) {
testCases := []struct {
Name string
Cfg map[string]interface{}
}{
{
Name: "shared memory cache",
Cfg: map[string]interface{}{
"shared": true,
},
},
{
Name: "dedicated memory cache",
Cfg: map[string]interface{}{
"shared": false,
},
},
{
Name: "shared LRU cache",
Cfg: map[string]interface{}{
"shared": true,
"max_items": 10,
"max_size": 100000,
},
},
{
Name: "dedicated LRU cache",
Cfg: map[string]interface{}{
"shared": false,
"max_items": 10,
"max_size": 100000,
},
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
globalCache = httpcache.NewMemoryCache()
defer tearDown()
cfg := &config.Backend{
Decoder: encoding.JSONDecoder,
ExtraConfig: map[string]interface{}{
Namespace: testCase.Cfg,
},
}
b := newBackend(1)
defer b.Close()
testClient(t, cfg, b.URL(), maxRequests)
<-time.After(1500 * time.Millisecond)
testClient(t, cfg, b.URL(), maxRequests)
<-time.After(1500 * time.Millisecond)
testClient(t, cfg, b.URL(), maxRequests)
if hits := b.Count(); hits != 3 {
t.Errorf("unexpected number of hits. got: %d, want: 3", hits)
}
})
}
}
func TestClient_dedicated(t *testing.T) {
testCases := []struct {
Name string
Cfg map[string]interface{}
}{
{
Name: "dedicated memory cache",
Cfg: map[string]interface{}{
"shared": false,
},
},
{
Name: "dedicated LRU cache",
Cfg: map[string]interface{}{
"shared": false,
"max_items": 10,
"max_size": 100000,
},
},
}
for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
globalCache = httpcache.NewMemoryCache()
defer tearDown()
b := newBackend(300)
defer b.Close()
{
cfg := &config.Backend{
Decoder: encoding.JSONDecoder,
ExtraConfig: map[string]interface{}{
Namespace: testCase.Cfg,
},
}
testClient(t, cfg, b.URL(), maxRequests)
if hits := b.Count(); hits != 1 {
t.Errorf("unexpected number of hits. got: %d, want: 1", hits)
}
}
{
cfg := &config.Backend{
Decoder: encoding.JSONDecoder,
ExtraConfig: map[string]interface{}{
Namespace: map[string]interface{}{},
},
}
testClient(t, cfg, b.URL(), maxRequests)
if hits := b.Count(); hits != 2 {
t.Errorf("unexpected number of hits. got: %d, want: 2", hits)
}
}
})
}
}
func TestClient_lruEvictions(t *testing.T) {
globalCache = httpcache.NewMemoryCache()
defer tearDown()
// Each backend response is around 180 bytes
b1 := newBackend(300)
b2 := newBackend(300)
defer b1.Close()
defer b2.Close()
cfg := &config.Backend{
Decoder: encoding.JSONDecoder,
ExtraConfig: map[string]interface{}{
Namespace: map[string]interface{}{
"shared": true,
"max_items": 100,
"max_size": 250, // Only one backend response will fit in the cache
},
},
}
testClient(t, cfg, b1.URL(), 1) // LRU size increases ~180 bytes
testClient(t, cfg, b2.URL(), 1) // LRU size increases ~180 bytes, evicts previous entry
testClient(t, cfg, b1.URL(), 1) // Should hit backend
if hits := b1.Count(); hits != 2 {
t.Errorf("unexpected number of hits. got: %d, want: 2", hits)
}
if hits := b2.Count(); hits != 1 {
t.Errorf("unexpected number of hits. got: %d, want: 1", hits)
}
}
func TestClient_noCache(t *testing.T) {
globalCache = httpcache.NewMemoryCache()
defer tearDown()
b := newBackend(300)
defer b.Close()
cfg := &config.Backend{
Decoder: encoding.JSONDecoder,
ExtraConfig: map[string]interface{}{},
}
testClient(t, cfg, b.URL(), maxRequests)
if hits := b.Count(); hits != uint64(maxRequests) {
t.Errorf("unexpected number of hits. got: %d, want: %d", hits, uint64(maxRequests))
}
}
func TestClient_backendFactory(t *testing.T) {
globalCache = httpcache.NewMemoryCache()
defer tearDown()
b := newBackend(300)
defer b.Close()
sampleCfg := &config.Backend{
Decoder: encoding.JSONDecoder,
ExtraConfig: map[string]interface{}{
Namespace: map[string]interface{}{},
},
}
httpClientFactory := NewHTTPClient(sampleCfg, client.NewHTTPClient)
backendFactory := proxy.CustomHTTPProxyFactory(httpClientFactory)
backendProxy := backendFactory(sampleCfg)
ctx := context.Background()
URL, _ := url.Parse(b.URL())
for i := 0; i < maxRequests; i++ {
req := &proxy.Request{
Method: "GET",
URL: URL,
Body: io.NopCloser(bytes.NewBufferString("")),
}
resp, err := backendProxy(ctx, req)
if err != nil {
t.Error(err)
return
}
if !resp.IsComplete {
t.Error("incomplete response:", *resp)
return
}
}
if hits := b.Count(); hits != 1 {
t.Errorf("unexpected number of hits. got: %d, want: %d", hits, 1)
}
}
func testClient(t *testing.T, cfg *config.Backend, URL string, numRequests int) {
c := NewHTTPClient(cfg, client.NewHTTPClient)(context.Background())
for i := 0; i < numRequests; i++ {
resp, err := c.Get(URL)
if err != nil {
t.Error(err)
return
}
response, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Error(err)
return
}
if string(response) != statusOKMsg {
t.Error("unexpected body:", string(response))
}
}
}
const statusOKMsg = `{"status": "ok"}`
func newBackend(ttl int) backend {
var ops uint64
return backend{
server: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
atomic.AddUint64(&ops, 1)
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", ttl))
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, statusOKMsg)
})),
ops: &ops,
}
}
type backend struct {
server *httptest.Server
ops *uint64
}
func (b backend) Close() {
b.server.Close()
}
func (b backend) Count() uint64 {
return atomic.LoadUint64(b.ops)
}
func (b backend) URL() string {
return b.server.URL
}