-
Notifications
You must be signed in to change notification settings - Fork 8
/
config_test.go
385 lines (353 loc) · 10.5 KB
/
config_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
// Copyright 2015-present, Cyrill @ Schumacher.fm and the CoreStore contributors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package caddyesi
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/corestoreio/caddy-esi/helper"
"github.com/stretchr/testify/assert"
)
var _ fmt.Stringer = (*PathConfigs)(nil)
var _ fmt.Stringer = (*PathConfig)(nil)
func TestPathConfigs_String(t *testing.T) {
pc := PathConfigs{
&PathConfig{
Scope: "/catalog/product",
MaxBodySize: 4,
},
&PathConfig{
Scope: "/checkout/cart",
MaxBodySize: 3,
},
}
assert.Exactly(t,
"PathConfig Count: 2\nScope:\"/catalog/product\"; MaxBodySize:4; Timeout:0s; PageIDSource:[]; AllowedMethods:[]; LogFile:\"\"; LogLevel:\"\"; EntityCount: 0\nScope:\"/checkout/cart\"; MaxBodySize:3; Timeout:0s; PageIDSource:[]; AllowedMethods:[]; LogFile:\"\"; LogLevel:\"\"; EntityCount: 0\n",
pc.String())
}
const weirdLongURL = `https://app.usunu.com/-/login?u=https%3A%2F%2Fapp.usunu.com%2F0%2Fsearch%2F2385944396396%2F81453167684176&e=emailaddress%40gmail.com&passive=1`
func TestPathConfig_PageID(t *testing.T) {
t.Parallel()
runner := func(pageIDSource []string, r *http.Request, wantSum uint64) func(*testing.T) {
return func(t *testing.T) {
t.Parallel()
pc := NewPathConfig()
pc.PageIDSource = pageIDSource
if have, want := pc.pageID(r), wantSum; have != want {
t.Errorf("Test %q\nHave: %x Want: %x\nHave: %d Want: %d", t.Name(), have, want, have, want)
}
}
}
t.Run("Default Host & Path (empty)", runner(
nil,
httptest.NewRequest("GET", "/", nil),
0x7a6e1f1822179273,
))
t.Run("Default Host & Path (test)", runner(
nil,
httptest.NewRequest("GET", "/test", nil),
0x2e3a61d5bfffd7d2,
))
t.Run("Default Host & Path (tEst)", runner(
nil,
httptest.NewRequest("GET", "/tEst", nil),
0x9a11d866604d519f,
))
t.Run("Cookie correct", runner(
[]string{"cookie-xtestKeks"},
func() *http.Request {
r := httptest.NewRequest("GET", "/test", nil)
r.AddCookie(&http.Cookie{Name: "xtestKeks", Value: "xVal"})
return r
}(),
0xf12f535bf90d7060,
))
t.Run("Cookie config wrong, fall back to default", runner(
[]string{"Cookie-xtestKeks"},
func() *http.Request {
r := httptest.NewRequest("GET", "/test", nil)
r.AddCookie(&http.Cookie{Name: "xtestKeks", Value: "xVal"})
return r
}(),
0x2e3a61d5bfffd7d2, // equal to default because Cookie is upper case
))
t.Run("Header correct", runner(
[]string{"header-xtestHeader"},
func() *http.Request {
r := httptest.NewRequest("GET", "/test", nil)
r.Header.Set("xtestHeader", "xVal2")
return r
}(),
0xbcf6bbff89b2d7d6,
))
t.Run("Header config wrong, fall back to default", runner(
[]string{"Header-xtestHeader"},
func() *http.Request {
r := httptest.NewRequest("GET", "/test", nil)
r.Header.Set("xtestHeader", "xVal2")
return r
}(),
0x2e3a61d5bfffd7d2, // equal to default because Header is upper case
))
t.Run("remote addr", runner(
[]string{"remoteaddr"},
func() *http.Request {
r := httptest.NewRequest("GET", "/test", nil)
r.RemoteAddr = "127.0.0.2"
return r
}(),
0xf024aa02b95193e,
))
t.Run("realip", runner(
[]string{"realip"},
func() *http.Request {
r := httptest.NewRequest("GET", "/test", nil)
r.Header.Set(helper.XClusterClientIP, "127.0.0.2")
return r
}(),
0x11155771612facfc, // hash of the byte slice of cluster client IP
))
t.Run("scheme", runner(
[]string{"scheme"},
httptest.NewRequest("GET", "https://caddyserver.com/test", nil),
0x909acbb899ed37e6,
))
t.Run("host", runner(
[]string{"host"},
httptest.NewRequest("GET", weirdLongURL, nil),
0x16a46f35c998d63d,
))
t.Run("path", runner(
[]string{"path"},
httptest.NewRequest("GET", weirdLongURL, nil),
0x163de6d2f60202bc, // path is: /-/login
))
t.Run("rawpath", runner(
[]string{"rawpath"},
httptest.NewRequest("GET", weirdLongURL, nil),
0xb4b967239b2b0817, // rawpath is: app.usunu.com/-/login
))
t.Run("rawquery", runner(
[]string{"rawquery"},
httptest.NewRequest("GET", weirdLongURL, nil),
0xb08e9c9fd24079b4, // rawquery is: u=https%3A%2F%2Fapp.usunu.com%2F0%2Fsearch%2F2385944396396%2F81453167684176&e=emailaddress%40gmail.com&passive=1
))
t.Run("url", runner(
[]string{"url"},
httptest.NewRequest("GET", weirdLongURL, nil),
0x6c7360d1c2978e84, // full url
))
t.Run("default page01.html", runner(
nil,
httptest.NewRequest("GET", "http://127.0.0.1:2017/page01.html", nil),
0xe7fcc1b160b213c2,
))
t.Run("default page02.html", runner(
nil,
httptest.NewRequest("GET", "http://127.0.0.1:2017/page02.html", nil),
0xb5a0ad5009522352,
))
}
var benchmarkPageID uint64
func BenchmarkPageID(b *testing.B) {
r := httptest.NewRequest("GET", "/catalog/product/id/42342342/price/134.231/stock/1/camera.html", nil)
pc := NewPathConfig()
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchmarkPageID = pc.pageID(r)
}
}
func BenchmarkPageID_FullURL(b *testing.B) {
r := httptest.NewRequest("GET", weirdLongURL, nil)
pc := NewPathConfig()
pc.PageIDSource = []string{"url"}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchmarkPageID = pc.pageID(r)
}
}
func BenchmarkPageID_Cookie(b *testing.B) {
r := httptest.NewRequest("GET", weirdLongURL, nil)
r.AddCookie(&http.Cookie{Name: "xtestKeks", Value: "xVal"})
pc := NewPathConfig()
pc.PageIDSource = []string{"cookie-xtestKeks"}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchmarkPageID = pc.pageID(r)
}
}
func TestPathConfig_isRequestAllowed(t *testing.T) {
t.Parallel()
runner := func(allowedMethods []string, r *http.Request, want bool) func(*testing.T) {
return func(t *testing.T) {
t.Parallel()
pc := NewPathConfig()
pc.AllowedMethods = allowedMethods
assert.Exactly(t, want, pc.IsRequestAllowed(r))
}
}
t.Run("Default GET benchIsResponseAllowed", runner(
nil,
httptest.NewRequest("GET", "/test", nil),
true,
))
t.Run("DELETE not benchIsResponseAllowed", runner(
nil,
httptest.NewRequest("DELETE", "/test", nil),
false,
))
t.Run("POST benchIsResponseAllowed", runner(
[]string{"POST"},
httptest.NewRequest("POST", "/test", nil),
true,
))
t.Run("GET benchIsResponseAllowed but only POSt benchIsResponseAllowed", runner(
[]string{"POST"},
httptest.NewRequest("GET", "/test", nil),
false,
))
}
func TestPathConfigs_ConfigForPath(t *testing.T) {
t.Parallel()
runner := func(pc PathConfigs, r *http.Request, want string) func(*testing.T) {
return func(t *testing.T) {
t.Parallel()
c := pc.ConfigForPath(r)
if want == "" {
assert.Nil(t, c)
return
}
if c == nil {
t.Errorf("c should not be nil! Request Path %q; want %q", r.URL.Path, want)
} else {
assert.Exactly(t, want, c.Scope)
}
}
}
t.Run("/catalog/product config found", runner(
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
},
&PathConfig{
Scope: "/checkout/cart",
},
},
httptest.NewRequest("GET", "/catalog/product", nil),
"/catalog/product",
))
t.Run("/catalog/product/view?a=b config found", runner(
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
},
&PathConfig{
Scope: "/checkout/cart",
},
},
httptest.NewRequest("GET", "/catalog/product/view?a=b", nil),
"/catalog/product",
))
t.Run("/checkout/cart config found", runner(
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
},
&PathConfig{
Scope: "/checkout/cart",
},
},
httptest.NewRequest("GET", "/checkout/cart", nil),
"/checkout/cart",
))
t.Run("/ no ESI config found, path does not match", runner(
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
},
&PathConfig{
Scope: "/checkout/cart",
},
},
httptest.NewRequest("GET", "/", nil),
"",
))
t.Run("/ config found in /", runner(
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
},
&PathConfig{
Scope: "/",
},
},
httptest.NewRequest("GET", "/checkout/cart", nil),
"/",
))
t.Run("/checkout/cart config found in /", runner(
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
},
&PathConfig{
Scope: "/",
},
},
httptest.NewRequest("GET", "/checkout/cart", nil),
"/",
))
}
func TestIsResponseAllowed(t *testing.T) {
t.Run("HTML benchIsResponseAllowed", func(t *testing.T) {
assert.True(t, isResponseAllowed([]byte("\r\n<html>...")))
})
t.Run("MP4 not benchIsResponseAllowed", func(t *testing.T) {
assert.False(t, isResponseAllowed([]byte("\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat")))
})
t.Run("GIF not benchIsResponseAllowed", func(t *testing.T) {
assert.False(t, isResponseAllowed([]byte("GIF89a")))
})
t.Run("XML benchIsResponseAllowed", func(t *testing.T) {
assert.True(t, isResponseAllowed([]byte("\n<?xml!")))
})
}
var benchIsResponseAllowed bool
// BenchmarkIsResponseAllowed/Detect_binary-4 3000000 462 ns/op 0 B/op 0 allocs/op
// BenchmarkIsResponseAllowed/Detect_html-4 50000000 37.3 ns/op 0 B/op 0 allocs/op
func BenchmarkIsResponseAllowed(b *testing.B) {
mp4 := []byte("\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat")
html := []byte(`<!DOCTYPE html> <html lang="en-US"><head> <title>Caddy Tag Tag Demo</title> </head><body> <h1> Caddy Tag Tag Demo Page</h1> <table> <tbody> <tr> <th> Name</th> <th>Output</th> </tr> <tr> <td>Should now a cart from ms_cart_tiny.html</td> <td> <esi:include src="http://127.0.0.1:3017/ms_cart_tiny.html"/> </td> </tr> </tbody></table></body></html>`)
b.Run("Detect binary", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchIsResponseAllowed = isResponseAllowed(mp4)
}
if benchIsResponseAllowed {
b.Fatal("MP4 not benchIsResponseAllowed but received true")
}
})
b.Run("Detect html", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
benchIsResponseAllowed = isResponseAllowed(html)
}
if !benchIsResponseAllowed {
b.Fatal("HTML benchIsResponseAllowed but received false")
}
})
}