This repository has been archived by the owner on Nov 22, 2018. It is now read-only.
forked from nicolasazrak/caddy-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_test.go
98 lines (92 loc) · 3.4 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
package cache
import (
"strconv"
"testing"
"time"
"github.com/mholt/caddy"
"github.com/stretchr/testify/require"
)
func TestParsingConfig(t *testing.T) {
tests := []struct {
input string
shouldErr bool
expect Config
}{
{"cache", false, Config{
StatusHeader: defaultStatusHeader,
LockTimeout: defaultLockTimeout,
DefaultMaxAge: defaultMaxAge,
CacheRules: []CacheRule{},
}},
{"cache {\n match_path /assets \n} }", false, Config{
StatusHeader: defaultStatusHeader,
LockTimeout: defaultLockTimeout,
DefaultMaxAge: defaultMaxAge,
CacheRules: []CacheRule{&PathCacheRule{Path: "/assets"}},
}},
{"cache {\n match_path /assets \n match_path /api \n} \n}", false, Config{
StatusHeader: defaultStatusHeader,
LockTimeout: defaultLockTimeout,
DefaultMaxAge: defaultMaxAge,
CacheRules: []CacheRule{
&PathCacheRule{Path: "/assets"},
&PathCacheRule{Path: "/api"},
},
}},
{"cache {\n match_header Content-Type image/png image/gif \n match_path /assets \n}", false, Config{
StatusHeader: defaultStatusHeader,
LockTimeout: defaultLockTimeout,
DefaultMaxAge: defaultMaxAge,
CacheRules: []CacheRule{
&HeaderCacheRule{Header: "Content-Type", Value: []string{"image/png", "image/gif"}},
&PathCacheRule{Path: "/assets"},
},
}},
{"cache {\n status_header X-Custom-Header \n}", false, Config{
StatusHeader: "X-Custom-Header",
LockTimeout: defaultLockTimeout,
DefaultMaxAge: defaultMaxAge,
CacheRules: []CacheRule{},
}},
{"cache {\n path /tmp/caddy \n}", false, Config{
StatusHeader: defaultStatusHeader,
LockTimeout: defaultLockTimeout,
DefaultMaxAge: defaultMaxAge,
CacheRules: []CacheRule{},
Path: "/tmp/caddy",
}},
{"cache {\n lock_timeout 1s \n}", false, Config{
StatusHeader: defaultStatusHeader,
LockTimeout: time.Duration(1) * time.Second,
DefaultMaxAge: defaultMaxAge,
CacheRules: []CacheRule{},
}},
{"cache {\n default_max_age 1h \n}", false, Config{
StatusHeader: defaultStatusHeader,
LockTimeout: defaultLockTimeout,
DefaultMaxAge: time.Duration(1) * time.Hour,
CacheRules: []CacheRule{},
}},
{"cache {\n match_header aheader \n}", true, Config{}}, // match_header without value
{"cache {\n lock_timeout aheader \n}", true, Config{}}, // lock_timeout with invalid duration
{"cache {\n lock_timeout \n}", true, Config{}}, // lock_timeout has no arguments
{"cache {\n default_max_age somevalue \n}", true, Config{}}, // lock_timeout has invalid duration
{"cache {\n default_max_age \n}", true, Config{}}, // default_max_age has no arguments
{"cache {\n status_header aheader another \n}", true, Config{}}, // status_header with invalid number of parameters
{"cache {\n match_path / ea \n}", true, Config{}}, // Invalid number of parameters in match
{"cache {\n invalid / ea \n}", true, Config{}}, // Invalid directive
{"cache {\n path \n}", true, Config{}}, // Path without arguments
}
for i, test := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
c := caddy.NewTestController("http", test.input)
actual, err := cacheParse(c)
if test.shouldErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, test.expect, *actual, "Invalid config parsed in test "+strconv.Itoa(i+1))
}
})
}
}