forked from sillygod/cdp-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_test.go
160 lines (133 loc) · 3.91 KB
/
handler_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
package httpcache
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/stretchr/testify/suite"
)
type CacheKeyTemplatingTestSuite struct {
suite.Suite
requestURI string
}
func (suite *CacheKeyTemplatingTestSuite) SetupSuite() {
suite.requestURI = "https://example.com/songa?age=20&class=A"
}
func (suite *CacheKeyTemplatingTestSuite) TestKeyReplacer() {
r, err := http.NewRequest("GET", suite.requestURI, nil)
if err != nil {
suite.Error(err)
}
repl := caddyhttp.NewTestReplacer(r)
result := repl.ReplaceKnown(defaultCacheKeyTemplate, "")
suite.Equal("GET example.com/songa?age=20&class=A", result)
}
type HandlerProvisionTestSuite struct {
suite.Suite
handler *Handler
}
func (suite *HandlerProvisionTestSuite) SetupSuite() {
suite.handler = new(Handler)
if suite.handler.Config == nil {
suite.handler.Config = getDefaultConfig()
}
suite.handler.Config.RuleMatchersRaws = []RuleMatcherRawWithType{
{
Type: MatcherTypePath,
Data: []byte(`{"path": "/"}`),
},
{
Type: MatcherTypeHeader,
Data: []byte(`{"header": "Content-Type", "value": ["image/jpg"]}`),
},
}
}
func (suite *HandlerProvisionTestSuite) TearDownSuite() {
}
func (suite *HandlerProvisionTestSuite) TestProvisionRuleMatchers() {
err := suite.handler.provisionRuleMatchers()
suite.Assert().NoError(err)
}
func (suite *HandlerProvisionTestSuite) TestProvisionDistributed() {
suite.handler.DistributedRaw = []byte(`
{
"Catalog":null,
"Client":null,
"Config":{
"addr":"consul:8500",
"health_url":":7777/health",
"service_name":"cache_server"
},
"KV":null,
"ServiceIDs":null,
"distributed":"consul"}
`)
err := suite.handler.provisionDistributed(caddy.Context{})
// NOTE: I know it's weird here but I want to ensure the part of function LoadModule
// works correctly. In this case, I don't set up the consul server so it will get
// connection error like "xxx no such host"
suite.Contains(err.Error(), "http://consul:8500/v1/agent/service/register")
}
type DetermineShouldCacheTestSuite struct {
suite.Suite
Config *Config
}
func (suite *DetermineShouldCacheTestSuite) SetupSuite() {
if suite.Config == nil {
suite.Config = getDefaultConfig()
}
}
func (suite *DetermineShouldCacheTestSuite) TestWebsocketConnection() {
eligibleHeader := http.Header{
"Connection": {"Upgrade"},
"Upgrade": {"Websocket"},
}
nonEligibleHeader := http.Header{
"Connection": {"Upgrade"},
"Upgrade": {"NoWebsocket"},
}
tests := []struct {
header http.Header
shouldBeCached bool
}{
{eligibleHeader, false},
{nonEligibleHeader, true},
}
for _, test := range tests {
req := makeRequest("/", test.header)
shouldBeCached := shouldUseCache(req, suite.Config)
suite.Equal(test.shouldBeCached, shouldBeCached)
}
}
func (suite *DetermineShouldCacheTestSuite) TestNonGETOrHeadMethod() {
r := httptest.NewRequest("POST", "/", nil)
shouldBeCached := shouldUseCache(r, suite.Config)
suite.False(shouldBeCached)
}
type DetermineShouldCachePOSTOnlyTestSuite struct {
suite.Suite
Config *Config
}
func (suite *DetermineShouldCachePOSTOnlyTestSuite) SetupSuite() {
if suite.Config == nil {
suite.Config = getDefaultConfig()
suite.Config.MatchMethods = []string{"POST"}
}
}
func (suite *DetermineShouldCachePOSTOnlyTestSuite) TestPOSTMethod() {
r := httptest.NewRequest("POST", "/", nil)
shouldBeCached := shouldUseCache(r, suite.Config)
suite.True(shouldBeCached)
}
func (suite *DetermineShouldCachePOSTOnlyTestSuite) TestGETMethod() {
r := httptest.NewRequest("GET", "/", nil)
shouldBeCached := shouldUseCache(r, suite.Config)
suite.False(shouldBeCached)
}
func TestCacheKeyTemplatingTestSuite(t *testing.T) {
suite.Run(t, new(CacheKeyTemplatingTestSuite))
suite.Run(t, new(DetermineShouldCacheTestSuite))
suite.Run(t, new(DetermineShouldCachePOSTOnlyTestSuite))
suite.Run(t, new(HandlerProvisionTestSuite))
}