forked from sillygod/cdp-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
caddyfile_test.go
94 lines (82 loc) · 2.16 KB
/
caddyfile_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
package httpcache
import (
"testing"
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/stretchr/testify/suite"
)
type CaddyfileTestSuite struct {
suite.Suite
}
func (suite *CaddyfileTestSuite) TestSettingFileCacheType() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
cache_type file
match_path /assets
lock_timeout 10m
path /tmp/cache
default_max_age 5m
status_header "X-Cache-Status"
cache_key "{http.request.method} {http.request.host}{http.request.uri.path} {http.request.uri.query}"
cache_bucket_num 1024
match_header Content-Type image/png
match_header X-Forwarded-For 144.30.20.10
}
`),
}
handler, err := parseCaddyfile(h)
suite.Nil(err)
mh, ok := handler.(*Handler)
suite.True(ok, "the caddyhttp middlewareHandler should be castable to Handler")
suite.Equal(file, mh.Config.Type)
suite.Equal(3, len(mh.Config.RuleMatchersRaws))
}
func (suite *CaddyfileTestSuite) TestErrorSetMultipleCacheType() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
cache_type file in_memory
}
`),
}
_, err := parseCaddyfile(h)
suite.Error(err, "it should raise the invalid usage of cache_type")
}
func (suite *CaddyfileTestSuite) TestInvalidCacheStatusHeader() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
status_header X-Cache-Status A-Status
}`),
}
_, err := parseCaddyfile(h)
suite.Error(err, "it should raise the invalid usage of status_header")
}
func (suite *CaddyfileTestSuite) TestInvalidParameter() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
whocares haha
}
`),
}
_, err := parseCaddyfile(h)
suite.Error(err, "invalid parameter")
}
func (suite *CaddyfileTestSuite) TestDistributedCacheConfig() {
h := httpcaddyfile.Helper{
Dispenser: caddyfile.NewTestDispenser(`
http_cache {
distributed consul {
service_name "cache_server"
}
}
`),
}
_, err := parseCaddyfile(h)
suite.Nil(err)
}
func TestCaddyfileTestSuite(t *testing.T) {
suite.Run(t, new(CaddyfileTestSuite))
}