forked from sillygod/cdp-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint_test.go
187 lines (147 loc) · 4.6 KB
/
endpoint_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
package httpcache
import (
"bytes"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"testing"
"github.com/caddyserver/caddy/v2/caddytest"
"github.com/stretchr/testify/suite"
)
type CacheEndpointTestSuite struct {
suite.Suite
caddyTester *caddytest.Tester
url string
admin_url string
}
func (suite *CacheEndpointTestSuite) assertKeyNotIn(key string, keys []string, msgAndArgs ...interface{}) {
exists := false
for _, k := range keys {
if k == key {
exists = true
}
}
suite.False(exists, msgAndArgs)
}
func (suite *CacheEndpointTestSuite) assertKeyIn(key string, keys []string, msgAndArgs ...interface{}) {
exists := false
for _, k := range keys {
if k == key {
exists = true
}
}
suite.True(exists, msgAndArgs)
}
func (suite *CacheEndpointTestSuite) SetupSuite() {
suite.caddyTester = caddytest.NewTester(suite.T())
suite.url = "http://localhost:9898/hello"
caddytest.Default.AdminPort = 2019
// HACK: I don't know why caddytest will re-spwan a server which admin's port is 2019 different from the origin port in the caddytest
// This way, the test can run smoothly.
suite.admin_url = fmt.Sprintf("http://localhost:%d", caddytest.Default.AdminPort)
suite.caddyTester.InitServer(`
{
order http_cache before reverse_proxy
}
:9898 {
reverse_proxy {
to localhost:9988
}
http_cache {
cache_type in_memory
}
}
:9988 {
respond /hello 200 {
body "hope anything will be good"
}
}`, "caddyfile")
}
func (suite *CacheEndpointTestSuite) TestListCacheKeys() {
r, err := http.NewRequest("GET", suite.url, nil)
suite.Assert().NoError(err)
_, err = suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)
// create the cache first
r, err = http.NewRequest("GET", suite.admin_url+"/caches", nil)
suite.Assert().NoError(err)
res, err := suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)
result, err := io.ReadAll(res.Body)
res.Body.Close()
suite.Assert().NoError(err)
suite.True(strings.Contains(string(result), "GET localhost/hello?"))
}
func (suite *CacheEndpointTestSuite) TestHealthCheck() {
r, err := http.NewRequest("GET", suite.admin_url+"/health", nil)
suite.Assert().NoError(err)
_, err = suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)
}
func (suite *CacheEndpointTestSuite) TestShowCache() {
r, err := http.NewRequest("GET", suite.url, nil)
suite.Assert().NoError(err)
_, err = suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)
// create the cache first
url := fmt.Sprintf("%s/caches/%s", suite.admin_url, url.PathEscape("GET localhost/hello?"))
r, err = http.NewRequest("GET", url, nil)
suite.Assert().NoError(err)
res, err := suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)
result, err := io.ReadAll(res.Body)
res.Body.Close()
suite.Assert().NoError(err)
suite.True(strings.Contains(string(result), "hope anything will be good"), fmt.Sprintf("result: %s", string(result)))
}
func (suite *CacheEndpointTestSuite) TestPurgeCache() {
testdata := []struct {
uri string
host string
body []byte
cacheKey string
}{
{
host: "http://localhost:9898/",
uri: "hello",
body: []byte(`{"method": "GET", "host": "http://localhost", "uri": "hello"}`),
cacheKey: "GET localhost/hello?",
},
{
host: "http://localhost:9898/",
uri: "hello?abc.txt",
body: []byte(`{"host": "http://localhost", "uri": "hello?abc.txt"}`), // default method is GET
cacheKey: "GET localhost/hello?abc.txt",
},
{
host: "http://localhost:9898/",
uri: "hello",
body: []byte(`{"host": "http://localhost/", "uri": "hello"}`), // host with trailing forward slash is also ok
cacheKey: "GET localhost/hello?",
},
}
for _, data := range testdata {
r, err := http.NewRequest("GET", data.host+data.uri, nil)
suite.Assert().NoError(err)
_, err = suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)
// create the cache first
r, err = http.NewRequest("DELETE", suite.admin_url+"/caches/purge", bytes.NewBuffer(data.body))
suite.Assert().NoError(err)
r.Header.Set("Content-Type", "application/json")
cache = getHandlerCache()
keys := cache.Keys()
suite.assertKeyIn(data.cacheKey, keys, fmt.Sprintf("%s should be in keys: %s", data.cacheKey, keys))
res, err := suite.caddyTester.Client.Do(r)
suite.Assert().NoError(err)
suite.Equal(200, res.StatusCode)
// now, the cache is deleted so the key is not existed.
keys = cache.Keys()
suite.assertKeyNotIn(data.cacheKey, keys, fmt.Sprintf("%s should not be in keys: %s", data.cacheKey, keys))
}
}
func TestCacheEndpotingTestSuite(t *testing.T) {
suite.Run(t, new(CacheEndpointTestSuite))
}