-
Notifications
You must be signed in to change notification settings - Fork 8
/
setup_test.go
344 lines (309 loc) · 8.87 KB
/
setup_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
// 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 (
"bytes"
"io/ioutil"
"os"
"testing"
"time"
"github.com/corestoreio/caddy-esi/esicache"
"github.com/corestoreio/caddy-esi/esitag"
"github.com/corestoreio/caddy-esi/esitesting"
"github.com/corestoreio/errors"
"github.com/corestoreio/log"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
"github.com/stretchr/testify/assert"
)
func testPluginSetup(config string, wantPC PathConfigs, cacheCount int, requestFuncs []string, wantErrBhf errors.Kind) func(*testing.T) {
return func(t *testing.T) {
defer esicache.MainRegistry.Clear()
c := caddy.NewTestController("http", config)
if err := PluginSetup(c); wantErrBhf > 0 {
assert.True(t, wantErrBhf.Match(err), "(%s):\n%+v", t.Name(), err)
return
} else if err != nil {
t.Fatalf("Expected no errors, got: %+v", err)
}
mids := httpserver.GetConfig(c).Middleware()
if len(mids) != 1 {
t.Fatalf("Expected one middleware, got %d instead", len(mids))
}
handler := mids[0](httpserver.EmptyNext)
myHandler, ok := handler.(*Middleware)
if !ok {
t.Fatalf("Expected handler to be type Tag, got: %#v", handler)
}
assert.Exactly(t, len(wantPC), len(myHandler.PathConfigs))
for j, wantC := range wantPC {
haveC := myHandler.PathConfigs[j]
assert.Exactly(t, wantC.Scope, haveC.Scope, "Scope (Path) %s", t.Name())
assert.Exactly(t, wantC.Timeout, haveC.Timeout, "Timeout %s", t.Name())
assert.Exactly(t, wantC.TTL, haveC.TTL, "TTL %s", t.Name())
assert.Exactly(t, wantC.PageIDSource, haveC.PageIDSource, "PageIDSource %s", t.Name())
assert.Exactly(t, wantC.AllowedMethods, haveC.AllowedMethods, "AllowedMethods %s", t.Name())
assert.Exactly(t, wantC.LogFile, haveC.LogFile, "LogFile %s", t.Name())
assert.Exactly(t, wantC.LogLevel, haveC.LogLevel, "LogLevel %s", t.Name())
if len(wantC.OnError) > 0 {
assert.Exactly(t, string(wantC.OnError), string(haveC.OnError), "OnError %s", t.Name())
}
assert.Exactly(t, cacheCount, esicache.MainRegistry.Len(haveC.Scope), "Mismatch esicache.MainRegistry.Len")
}
for _, kvName := range requestFuncs {
rf, ok := esitag.LookupResourceHandler(kvName)
assert.True(t, ok, "Should have been registered %q", kvName)
assert.NotNil(t, rf, "Should have a non-nil func %q", kvName)
}
}
}
func TestPluginSetup(t *testing.T) {
t.Parallel()
t.Run("Default Config", testPluginSetup(
`esi`,
PathConfigs{
&PathConfig{
Scope: "/",
Timeout: DefaultTimeOut,
TTL: 0,
},
},
0, // cache length
nil, // kv services []string
errors.NoKind,
))
t.Run("config with allowed_methods", testPluginSetup(
`esi {
allowed_methods "GET,pUT , POsT"
}`,
PathConfigs{
&PathConfig{
Scope: "/",
Timeout: DefaultTimeOut,
AllowedMethods: []string{"GET", "PUT", "POST"},
},
},
0, // cache length
nil, // kv services []string
errors.NoKind,
))
t.Run("config with cmd_header_name", testPluginSetup(
`esi {
cmd_header_name X-Esi-CMD
}`,
PathConfigs{
&PathConfig{
Scope: "/",
Timeout: DefaultTimeOut,
CmdHeaderName: `X-Esi-Cmd`,
},
},
0, // cache length
nil, // kv services []string
errors.NoKind,
))
t.Run("config with cmd_header_name but value not provided", testPluginSetup(
`esi {
cmd_header_name
}`,
nil,
0, // cache length
nil, // kv services []string
errors.NotValid,
))
t.Run("config with page_id_source", testPluginSetup(
`esi {
page_id_source "pAth,host , IP, header-X-GitHub-Request-Id, header-Server, cookie-__Host-user_session_same_site"
}`,
PathConfigs{
&PathConfig{
Scope: "/",
Timeout: DefaultTimeOut,
PageIDSource: []string{"pAth", "host", "IP", "header-X-GitHub-Request-Id", "header-Server", "cookie-__Host-user_session_same_site"},
},
},
0, // cache length
nil, // kv services []string
errors.NoKind,
))
t.Run("config with page_id_source but errors", testPluginSetup(
`esi {
page_id_source
}`,
nil,
0, // cache length
nil, // kv services []string
errors.NotValid,
))
t.Run("Parse timeout fails", testPluginSetup(
`esi {
timeout Dms
}`,
nil,
0, // cache length
nil, // kv services []string
errors.NotValid,
))
t.Run("esi with path to /blog and /guestbook", testPluginSetup(
`esi /blog
esi /guestbook`,
PathConfigs{
&PathConfig{
Scope: "/blog",
Timeout: DefaultTimeOut,
TTL: 0,
},
&PathConfig{
Scope: "/guestbook",
Timeout: DefaultTimeOut,
TTL: 0,
},
},
0, // cache length
nil, // kv services []string
errors.NoKind,
))
t.Run("Log level info and file stderr", testPluginSetup(
`esi /catalog/product {
log_file stderr
log_level INFO
}`,
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
Timeout: 20 * time.Second,
LogFile: "stderr",
LogLevel: "info",
},
},
0, // cache length
nil, // kv services []string
errors.NoKind,
))
t.Run("OnError String", testPluginSetup(
`esi /catalog/product {
on_error "Resource content unavailable"
}`,
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
Timeout: 20 * time.Second,
OnError: []byte("Resource content unavailable"),
},
},
0, // cache length
nil, // kv services []string
errors.NoKind,
))
t.Run("OnError File", testPluginSetup(
`esi /catalog/product {
on_error "testdata/on_error.txt"
}`,
PathConfigs{
&PathConfig{
Scope: "/catalog/product",
Timeout: 20 * time.Second,
OnError: []byte("Output on a backend connection error\n"),
},
},
0, // cache length
nil, // kv services []string
errors.NoKind,
))
t.Run("OnError File not found", testPluginSetup(
`esi / {
on_error "testdataXX/on_error.txt"
}`,
nil,
0, // cache length
nil, // kv services []string
errors.Fatal,
))
}
func TestSetupLogger(t *testing.T) {
buf := new(bytes.Buffer)
osStdOut = buf
osStdErr = buf
defer func() {
osStdOut = os.Stdout
osStdErr = os.Stderr
}()
runner := func(pc *PathConfig, wantErrBhf errors.Kind) func(*testing.T) {
return func(t *testing.T) {
haveErr := setupLogger(pc)
if wantErrBhf > 0 {
assert.True(t, wantErrBhf.Match(haveErr), "%+v", haveErr)
} else {
assert.NoError(t, haveErr, "%+v", haveErr)
}
}
}
pc := &PathConfig{
LogLevel: "debug",
LogFile: "stderr",
}
t.Run("Debug Stderr", runner(pc, errors.NoKind))
assert.True(t, pc.Log.IsDebug())
pc.Log.Debug("DebugStdErr", log.String("debug01", "stderr01"))
assert.Contains(t, buf.String(), `"msg":"DebugStdErr","debug01":"stderr01"`)
pc = &PathConfig{
LogLevel: "info",
LogFile: "stdout",
}
t.Run("Info Stdout", runner(pc, errors.NoKind))
assert.True(t, pc.Log.IsInfo(), "Loglevel should be info")
pc.Log.Info("InfoStdOut", log.String("info01", "stdout01"))
assert.Contains(t, buf.String(), `InfoStdOut","info01":"stdout01"`)
pc = &PathConfig{
LogLevel: "",
LogFile: "stdout",
}
t.Run("No Log Level, Blackhole", runner(pc, errors.NoKind))
assert.False(t, pc.Log.IsInfo())
assert.False(t, pc.Log.IsDebug())
pc.Log.Info("NoLogLevelStdOut", log.String("info01", "noLogLevelstdout01"))
assert.NotContains(t, buf.String(), `NoLogLevelStdOut`)
pc = &PathConfig{
LogLevel: "debug",
LogFile: "",
}
t.Run("No Log File, Blackhole", runner(pc, errors.NoKind))
assert.False(t, pc.Log.IsInfo())
assert.False(t, pc.Log.IsDebug())
pc.Log.Info("NoLogFileStdOut", log.String("info01", "noLogFilestdout01"))
assert.NotContains(t, buf.String(), `NoLogFileStdOut`)
pc = &PathConfig{
LogLevel: "debug",
LogFile: "/root",
}
t.Run("Log File open fails", runner(pc, errors.Fatal))
assert.Exactly(t, pc.Log.(log.BlackHole), log.BlackHole{})
tmpFile, clean := esitesting.Tempfile(t)
defer clean()
pc = &PathConfig{
LogLevel: "debug",
LogFile: tmpFile,
}
t.Run("Log File open success write", runner(pc, errors.NoKind))
assert.True(t, pc.Log.IsInfo())
assert.True(t, pc.Log.IsDebug())
pc.Log.Info("InfoWriteToTempFile", log.Int("info03", 2412))
pc.Log.Debug("DebugWriteToTempFile", log.Int("debugo04", 2512))
tmpFileContent, err := ioutil.ReadFile(tmpFile)
if err != nil {
t.Fatal(err)
}
assert.Contains(t, string(tmpFileContent), `"msg":"InfoWriteToTempFile","info03":2412`)
assert.Contains(t, string(tmpFileContent), `"msg":"DebugWriteToTempFile","debugo04":2512`)
}