Skip to content

Commit f364d10

Browse files
committed
Additional testing and backwards compatibility note.
1 parent 1068045 commit f364d10

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ Content-Type: text/plain; charset=utf-8
4444
4545
Hello, World!
4646
```
47+
48+
Backwards compatibility
49+
-----------------------
50+
51+
If you need backwards compatibility with `CacheOptions` configuration, you can
52+
wrap it in a `Config` option with...
53+
54+
```go
55+
http.Handle("/", cw.Cached(http.HandlerFunc(helloWorld), cw.Config(cw.CacheOptions{MaxAge: time.Hour * 24 * 13, NoTransform: true})))
56+
```

cache.go

+7
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@ func SharedMaxAge(d time.Duration) optionFunc {
9898
}
9999
}
100100

101+
// Config takes a CacheOptions value and replaces options.
102+
func Config(co CacheOptions) optionFunc {
103+
return func(o *CacheOptions) {
104+
*o = co
105+
}
106+
}
107+
101108
// These set the relevant pragmas in the response per
102109
// http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html
103110
type CacheOptions struct {

cache_test.go

+23-3
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ import (
1010
func TestCacheControl(t *testing.T) {
1111
w := httptest.NewRecorder()
1212
r, _ := http.NewRequest("POST", "http://example.com/foo", nil)
13-
cached := Cached(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}), MaxAge(time.Hour*24*13), NoTransform())
13+
handler := func(w http.ResponseWriter, r *http.Request) {}
14+
opts := []optionFunc{MaxAge(time.Hour * 24 * 13), NoTransform()}
15+
cached := Cached(http.HandlerFunc(handler), opts...)
16+
1417
cached.ServeHTTP(w, r)
1518

16-
if "no-transform, max-age=1123200" != w.Header().Get("Cache-Control") {
17-
t.Fatalf("Cache-Control header: got %s, wanted 'no-transform, max-age=1123200'", w.Header().Get("Cache-Control"))
19+
wanted := "no-transform, max-age=1123200"
20+
if pragmas := w.Header().Get("Cache-Control"); pragmas != wanted {
21+
t.Fatalf("Cache-Control header: got %s, wanted '%s'", pragmas, wanted)
1822
}
1923
}
2024

@@ -53,6 +57,7 @@ var cacheOptionFuncTests = []struct {
5357
{MustRevalidate(), "must-revalidate"},
5458
{ProxyRevalidate(), "proxy-revalidate"},
5559
{SharedMaxAge(time.Hour * 13), "s-maxage=46800"},
60+
{Config(CacheOptions{MaxAge: time.Hour * 24 * 13, NoTransform: true}), "no-transform, max-age=1123200"},
5661
}
5762

5863
func TestOptionFuncs(t *testing.T) {
@@ -64,3 +69,18 @@ func TestOptionFuncs(t *testing.T) {
6469
}
6570
}
6671
}
72+
73+
func TestMaintainsExistingCacheOptions(t *testing.T) {
74+
w := httptest.NewRecorder()
75+
r, _ := http.NewRequest("POST", "http://example.com/foo", nil)
76+
handler := func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "must-revalidate") }
77+
opts := []optionFunc{MaxAge(time.Hour * 24 * 13), NoTransform()}
78+
cached := Cached(http.HandlerFunc(handler), opts...)
79+
80+
cached.ServeHTTP(w, r)
81+
82+
wanted := "must-revalidate"
83+
if pragmas := w.Header().Get("Cache-Control"); pragmas != wanted {
84+
t.Fatalf("Cache-Control header: got %s, wanted '%s'", pragmas, wanted)
85+
}
86+
}

0 commit comments

Comments
 (0)