-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalways-cache_test.go
More file actions
238 lines (213 loc) · 7.33 KB
/
always-cache_test.go
File metadata and controls
238 lines (213 loc) · 7.33 KB
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
package alwayscache
import (
"context"
"fmt"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"github.com/always-cache/always-cache/cache"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
func init() {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}
func startTestServer(handler *http.ServeMux, port int) (AlwaysCache, *http.Server) {
// start server
server := http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: handler,
}
go func() {
err := server.ListenAndServe()
if err != http.ErrServerClosed {
panic(err)
}
}()
// start set up acache
url, _ := url.Parse(fmt.Sprintf("http://localhost:%d", port))
acache := CreateCache(Config{
Cache: cache.NewSQLiteCache(""),
OriginURL: *url,
})
// wait a small while to ensure server is up
time.Sleep(time.Millisecond * 200)
return *acache, &server
}
func TestCacheUpdate(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("cache-update", "/count")
w.Write([]byte("Hello world"))
})
var handleCount int
mux.HandleFunc("/count", func(w http.ResponseWriter, r *http.Request) {
handleCount++
w.Header().Add("Cache-Control", "max-age=60")
w.Write([]byte(fmt.Sprintf("Called %d times", handleCount)))
})
req, _ := http.NewRequest("POST", "/update", nil)
countReq, _ := http.NewRequest("GET", "/count", nil)
acache, server := startTestServer(mux, 9001)
rr := httptest.NewRecorder()
acache.ServeHTTP(httptest.NewRecorder(), countReq)
acache.ServeHTTP(httptest.NewRecorder(), req)
// need to sleep, since updates are async
time.Sleep(time.Second)
acache.ServeHTTP(rr, countReq)
if body, err := io.ReadAll(rr.Result().Body); err != nil || fmt.Sprintf("%s", body) != "Called 2 times" {
t.Fatalf("Body is %s", body)
}
server.Shutdown(context.Background())
}
func TestUpdateOnPost(t *testing.T) {
handleCount := 0
assertCount := func(count int) {
if count != handleCount {
t.Fatalf("Handler called %d times, expected %d", handleCount, count)
}
}
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
handleCount++
w.Header().Add("Cache-Control", "max-age=60")
w.Write([]byte(fmt.Sprintf("So you wanted to %s?", r.Method)))
})
get, _ := http.NewRequest("GET", "/", nil)
post, _ := http.NewRequest("POST", "/", nil)
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
mw, server := startTestServer(mux, 9002)
mw.ServeHTTP(httptest.NewRecorder(), get)
assertCount(1)
// need to sleep, since updates are async
time.Sleep(time.Millisecond * 100)
mw.ServeHTTP(httptest.NewRecorder(), get)
assertCount(1)
mw.ServeHTTP(httptest.NewRecorder(), post)
// need to sleep, since updates are async
time.Sleep(time.Second)
// post will first do the actual post and then refresh with a get
assertCount(3)
mw.ServeHTTP(httptest.NewRecorder(), get)
assertCount(3)
server.Shutdown(context.Background())
}
func TestUpdateBeforeRespondingOnRedirect(t *testing.T) {
listCount := 0
mux := http.NewServeMux()
mux.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Second)
w.Header().Add("Cache-Control", "max-age=60")
w.Write([]byte(fmt.Sprintf("%d elements", listCount)))
})
mux.HandleFunc("/add", func(w http.ResponseWriter, r *http.Request) {
// only add if post request
if r.Method == "POST" {
listCount++
w.Header().Add("cache-update", "/list")
w.WriteHeader(http.StatusSeeOther)
w.Write([]byte("done"))
} else {
w.Write([]byte("nothing to do on get"))
}
})
mw, server := startTestServer(mux, 9003)
rr := httptest.NewRecorder()
mw.ServeHTTP(rr, httptest.NewRequest("GET", "/list", nil))
if body := rr.Body.String(); body != "0 elements" {
t.Fatalf("body is %s", body)
}
// create post, which will update the cache, and return when the response is done
mw.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("POST", "/add", nil))
rr = httptest.NewRecorder()
mw.ServeHTTP(rr, httptest.NewRequest("GET", "/list", nil))
if body := rr.Body.String(); body != "1 elements" {
t.Fatalf("body is %s", body)
}
server.Shutdown(context.Background())
}
// TestMaxAgeUpdate tests that the cache is updated when the max-age is reached.
// This is done by setting the max-age to 1 second and the update timeout to 0.5 seconds.
//
// This is what we will do and what we expect to happen:
// 1. Request the resource, which will be cached for 1 second.
// 2. Change the response to something new.
// 2. Sleep for 1 second, and in the meantime the cached resource should be updated.
// 3. Turn off the handler, so we know the next request will be served from the cache.
// 4. Request the resource again, which should be the updated resource served from the cache.
func TestMaxAgeUpdate(t *testing.T) {
response := "Hello world"
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if response == "" {
w.WriteHeader(http.StatusNotFound)
return
}
w.Header().Add("cache-control", "max-age=2")
w.Write([]byte(response))
})
mux := http.NewServeMux()
mux.HandleFunc("/", handler)
mw, server := startTestServer(mux, 9004)
req, _ := http.NewRequest("GET", "/", nil)
mw.ServeHTTP(httptest.NewRecorder(), req)
response = "Hello world 2"
time.Sleep(time.Second * 3)
response = ""
rr := httptest.NewRecorder()
mw.ServeHTTP(rr, req)
if body := rr.Body.String(); body != "Hello world 2" {
t.Fatalf("body is %s", body)
}
server.Shutdown(context.Background())
}
// TestUpdateDelay tests that the `delay` directive works as expected.
// It should delay the update of the cache by the specified amount of time.
//
// This is what we will do and what we expect to happen:
// 1. Request the resource, with a default max-age of 60 seconds.
// 2. POST an update to the resource, with a delay of 1 second.
// 3. Sleep for 100 ms just in case.
// 4. Update the response to something new.
// 5. Sleep for 1 second, and in the meantime the cached resource should be updated.
// 6. Turn off the handler, so we know the next request will be served from the cache.
// 7. Request the resource again, which should be the updated resource served from the cache.
func TestUpdateDelay(t *testing.T) {
response := "Hello world"
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Cache-Control", "max-age=60")
if response == "" {
w.WriteHeader(http.StatusNotFound)
return
}
w.Write([]byte(response))
})
mux.HandleFunc("/update", func(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
w.Header().Add("cache-update", "/; delay=1")
w.Write([]byte("done"))
} else {
http.Error(w, "nothing to do on get", http.StatusMethodNotAllowed)
}
})
mw, server := startTestServer(mux, 9005)
get, _ := http.NewRequest("GET", "/", nil)
post, _ := http.NewRequest("POST", "/update", nil)
rr := httptest.NewRecorder()
time.Sleep(time.Millisecond * 100)
mw.ServeHTTP(httptest.NewRecorder(), get) // 1.
mw.ServeHTTP(httptest.NewRecorder(), post) // 2.
time.Sleep(time.Millisecond * 100) // 3.
response = "Hello world 2" // 4.
time.Sleep(time.Second) // 5.
response = "" // 6.
mw.ServeHTTP(rr, get) // 7.
if body := rr.Body.String(); body != "Hello world 2" {
t.Fatalf("body is %s", body)
}
server.Shutdown(context.Background())
}