forked from darkweak/souin
/
mock.go
454 lines (422 loc) · 9.25 KB
/
mock.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package tests
import (
"fmt"
"log"
"net/http"
"os"
"regexp"
"github.com/darkweak/souin/cache/types"
"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/plugins/souin/configuration"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// DOMAIN is the domain constant
const DOMAIN = "domain.com"
// PATH is the path constant
const PATH = "/testing"
// BaseConfiguration is the legacy configuration
func BaseConfiguration() string {
return `
api:
basepath: /souin-api
security:
secret: your_secret_key
enable: true
users:
- username: user1
password: test
souin:
enable: true
default_cache:
headers:
- Authorization
port:
web: 80
tls: 443
regex:
exclude: 'ARegexHere'
ttl: 1000s
reverse_proxy_url: 'http://domain.com:81'
urls:
'domain.com/':
ttl: 1000s
headers:
- Authorization
'mysubdomain.domain.com':
ttl: 50s
headers:
- Authorization
- 'Content-Type'
ykeys:
The_First_Test:
headers:
Authorization: '.+'
Content-Type: '.+'
The_Second_Test:
url: 'the/second/.+'
The_Third_Test:
`
}
// CDNConfiguration is the CDN configuration
func CDNConfiguration() string {
return `
api:
basepath: /souin-api
souin:
enable: true
default_cache:
ttl: 1000s
cdn:
dynamic: true
strategy: hard
`
}
// BadgerConfiguration simulate the configuration for the Badger storage
func BadgerConfiguration() string {
return `
api:
basepath: /souin-api
security:
secret: your_secret_key
enable: true
users:
- username: user1
password: test
souin:
enable: true
default_cache:
badger:
configuration:
syncWrites: true
readOnly: false
inMemory: false
metricsEnabled: true
distributed: true
headers:
- Authorization
port:
web: 80
tls: 443
regex:
exclude: 'ARegexHere'
ttl: 1000s
reverse_proxy_url: 'http://domain.com:81'
ssl_providers:
- traefik
urls:
'domain.com/':
ttl: 1000s
headers:
- Authorization
'mysubdomain.domain.com':
ttl: 50s
headers:
- Authorization
- 'Content-Type'
`
}
// NutsConfiguration simulate the configuration for the Nuts storage
func NutsConfiguration() string {
return `
api:
basepath: /souin-api
security:
secret: your_secret_key
enable: true
users:
- username: user1
password: test
souin:
enable: true
default_cache:
nuts:
path: "/tmp/nuts"
headers:
- Authorization
port:
web: 80
tls: 443
regex:
exclude: 'ARegexHere'
ttl: 1000s
reverse_proxy_url: 'http://domain.com:81'
ssl_providers:
- traefik
urls:
'domain.com/':
ttl: 1000s
headers:
- Authorization
'mysubdomain.domain.com':
ttl: 50s
headers:
- Authorization
- 'Content-Type'
`
}
// EtcdConfiguration simulate the configuration for the Nuts storage
func EtcdConfiguration() string {
return `
api:
basepath: /souin-api
security:
secret: your_secret_key
enable: true
users:
- username: user1
password: test
souin:
enable: true
default_cache:
etcd:
configuration:
endpoints:
- http://etcd:2379
distributed: true
headers:
- Authorization
port:
web: 80
tls: 443
regex:
exclude: 'ARegexHere'
ttl: 1000s
reverse_proxy_url: 'http://domain.com:81'
ssl_providers:
- traefik
urls:
'domain.com/':
ttl: 1000s
headers:
- Authorization
'mysubdomain.domain.com':
ttl: 50s
headers:
- Authorization
- 'Content-Type'
`
}
// RedisConfiguration simulate the configuration for the Nuts storage
func RedisConfiguration() string {
return `
api:
basepath: /souin-api
security:
secret: your_secret_key
enable: true
users:
- username: user1
password: test
souin:
enable: true
default_cache:
redis:
url: redis:6379
distributed: true
headers:
- Authorization
port:
web: 80
tls: 443
regex:
exclude: 'ARegexHere'
ttl: 1000s
reverse_proxy_url: 'http://domain.com:81'
ssl_providers:
- traefik
urls:
'domain.com/':
ttl: 1000s
headers:
- Authorization
'mysubdomain.domain.com':
ttl: 50s
headers:
- Authorization
- 'Content-Type'
`
}
func baseEmbeddedOlricConfiguration(path string) string {
return fmt.Sprintf(`
api:
basepath: /souin-api
security:
secret: your_secret_key
enable: true
users:
- username: user1
password: test
souin:
enable: true
default_cache:
distributed: true
headers:
- Authorization
olric:
%s
port:
web: 80
tls: 443
regex:
exclude: 'ARegexHere'
ttl: 1000s
reverse_proxy_url: 'http://domain.com:81'
ssl_providers:
- traefik
urls:
'domain.com/':
ttl: 1000s
headers:
- Authorization
'mysubdomain.domain.com':
ttl: 50s
headers:
- Authorization
- 'Content-Type'
`, path)
}
// OlricConfiguration is the olric included configuration
func OlricConfiguration() string {
return baseEmbeddedOlricConfiguration(fmt.Sprintf("url: '%s'", "olric:3320"))
}
// EmbeddedOlricPlainConfigurationWithoutAdditionalYAML simulate the configuration for the embedded Olric storage
func EmbeddedOlricPlainConfigurationWithoutAdditionalYAML() string {
return baseEmbeddedOlricConfiguration(`
configuration:
olricd:
bindAddr: "0.0.0.0"
bindPort: 3320
serializer: "msgpack"
keepAlivePeriod: "20s"
bootstrapTimeout: "5s"
partitionCount: 271
replicaCount: 2
writeQuorum: 1
readQuorum: 1
readRepair: false
replicationMode: 1 # sync mode. for async, set 1
tableSize: 1048576 # 1MB in bytes
memberCountQuorum: 1
logging:
verbosity: 6
level: "DEBUG"
output: "stderr"
memberlist:
environment: "local"
bindAddr: "0.0.0.0"
bindPort: 3322
enableCompression: false
joinRetryInterval: "10s"
maxJoinAttempts: 2
storageEngines:
config:
kvstore:
tableSize: 4096
`)
}
// EmbeddedOlricConfiguration is the olric included configuration
func EmbeddedOlricConfiguration() string {
path := "/tmp/olric.yml"
_ = os.WriteFile(
path,
[]byte(
`
olricd:
bindAddr: "0.0.0.0"
bindPort: 3320
serializer: "msgpack"
keepAlivePeriod: "300s"
bootstrapTimeout: "5s"
partitionCount: 271
replicaCount: 2
writeQuorum: 1
readQuorum: 1
readRepair: false
replicationMode: 1 # sync mode. for async, set 1
tableSize: 1048576 # 1MB in bytes
memberCountQuorum: 1
logging:
verbosity: 6
level: "DEBUG"
output: "stderr"
memberlist:
environment: "local"
bindAddr: "0.0.0.0"
bindPort: 3322
enableCompression: false
joinRetryInterval: "1s"
maxJoinAttempts: 10
storageEngines:
config:
kvstore:
tableSize: 4096
`),
0600,
)
return baseEmbeddedOlricConfiguration(fmt.Sprintf("path: '%s'", path))
}
// MockConfiguration is an helper to mock the configuration
func MockConfiguration(configurationToLoad func() string) *configuration.Configuration {
var config configuration.Configuration
e := config.Parse([]byte(configurationToLoad()))
if e != nil {
log.Fatal(e)
}
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
logger, _ := cfg.Build()
config.SetLogger(logger)
return &config
}
// MockInitializeRegexp is an helper to mock the regexp initialization
func MockInitializeRegexp(configurationInstance configurationtypes.AbstractConfigurationInterface) regexp.Regexp {
u := ""
for k := range configurationInstance.GetUrls() {
if "" != u {
u += "|"
}
u += "(" + k + ")"
}
return *regexp.MustCompile(u)
}
// GetTokenName returns the token name
func GetTokenName() string {
return "souin-authorization-token"
}
// GetValidToken returns a valid token
func GetValidToken() *http.Cookie {
return &http.Cookie{
Name: GetTokenName(),
Value: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIxIiwiZXhwIjoxNjE0MTI0Nzk5OX0.7blW8hKWls2UgHLU8KOzwTG13uNoJR3UhLgoVdyCzx0",
Path: "/",
}
}
// GetCacheProviderClientAndMatchedURL will work as a factory to build providers from configuration and get the URL from the key passed in parameter
func GetCacheProviderClientAndMatchedURL(key string, configurationMocker func() configurationtypes.AbstractConfigurationInterface, factory func(configurationInterface configurationtypes.AbstractConfigurationInterface) (types.AbstractProviderInterface, error)) (types.AbstractProviderInterface, configurationtypes.URL) {
config := configurationMocker()
client, _ := factory(config)
regexpUrls := MockInitializeRegexp(config)
regexpURL := regexpUrls.FindString(key)
matchedURL := configurationtypes.URL{
TTL: configurationtypes.Duration{Duration: config.GetDefaultCache().GetTTL()},
Headers: config.GetDefaultCache().GetHeaders(),
}
if "" != regexpURL {
matchedURL = config.GetUrls()[regexpURL]
}
return client, matchedURL
}