forked from traefik/plugin-simplecache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
150 lines (120 loc) · 3.8 KB
/
cache_test.go
File metadata and controls
150 lines (120 loc) · 3.8 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
package traefik_plugin_simplecache
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
func TestNew(t *testing.T) {
tests := []struct {
name string
cfg *Config
wantErr bool
}{
{
name: "should not error if path is not valid",
cfg: &Config{Path: fmt.Sprintf("%s/foo_%d", os.TempDir(), time.Now().Unix()), MaxExpiry: 300, Cleanup: 600},
wantErr: false,
},
{
name: "should error if maxExpiry <= 1",
cfg: &Config{Path: os.TempDir(), MaxExpiry: 1, Cleanup: 600},
wantErr: true,
},
{
name: "should error if cleanup <= 1",
cfg: &Config{Path: os.TempDir(), MaxExpiry: 300, Cleanup: 1},
wantErr: true,
},
{
name: "should be valid",
cfg: &Config{Path: os.TempDir(), MaxExpiry: 300, Cleanup: 600},
wantErr: false,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, err := New(context.Background(), nil, test.cfg, "simplecache")
if test.wantErr && err == nil {
t.Fatal("expected error on bad regexp format")
}
})
}
}
func TestCache_ServeHTTP(t *testing.T) {
dir := createTempDir(t)
next := func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Cache-Control", "max-age=20")
rw.WriteHeader(http.StatusOK)
}
cfg := &Config{Path: dir, MaxExpiry: 10, Cleanup: 20, AddStatusHeader: true, ConsiderUrlQuery: false}
c, err := New(context.Background(), http.HandlerFunc(next), cfg, "simplecache")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "http://localhost/some/path", nil)
rw := httptest.NewRecorder()
c.ServeHTTP(rw, req)
if state := rw.Header().Get("Cache-Status"); state != "miss" {
t.Errorf("unexpected cache state: want \"miss\", got: %q", state)
}
rw = httptest.NewRecorder()
c.ServeHTTP(rw, req)
if state := rw.Header().Get("Cache-Status"); state != "hit" {
t.Errorf("unexpected cache state: want \"hit\", got: %q", state)
}
rw = httptest.NewRecorder()
// Check that the same request with a different URL query hits the same cache entry
c.ServeHTTP(rw, httptest.NewRequest(http.MethodGet, "http://localhost/some/path?queryParam=1", nil))
if state := rw.Header().Get("Cache-Status"); state != "hit" {
t.Errorf("unexpected cache state: want \"hit\", got: %q", state)
}
}
func TestCache_ServeHTTP_ConsiderUrlQuery(t *testing.T) {
dir := createTempDir(t)
next := func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Set("Cache-Control", "max-age=20")
rw.WriteHeader(http.StatusOK)
}
cfg := &Config{Path: dir, MaxExpiry: 10, Cleanup: 20, AddStatusHeader: true, ConsiderUrlQuery: true}
c, err := New(context.Background(), http.HandlerFunc(next), cfg, "simplecache")
if err != nil {
t.Fatal(err)
}
testUrl := "http://localhost/some/path"
req := httptest.NewRequest(http.MethodGet, testUrl, nil)
rw := httptest.NewRecorder()
c.ServeHTTP(rw, req) // Add response to the cache
rw = httptest.NewRecorder()
c.ServeHTTP(rw, req)
if state := rw.Header().Get("Cache-Status"); state != "hit" {
t.Errorf("unexpected cache state: want \"hit\", got: %q", state)
}
rw = httptest.NewRecorder()
c.ServeHTTP(rw, httptest.NewRequest(http.MethodGet, testUrl+"?queryParam=1", nil))
if state := rw.Header().Get("Cache-Status"); state != "miss" {
t.Errorf("unexpected cache state: want \"miss\", got: %q", state)
}
rw = httptest.NewRecorder()
c.ServeHTTP(rw, httptest.NewRequest(http.MethodGet, testUrl+"?queryParam=2", nil))
if state := rw.Header().Get("Cache-Status"); state != "miss" {
t.Errorf("unexpected cache state: want \"miss\", got: %q", state)
}
}
func createTempDir(tb testing.TB) string {
tb.Helper()
dir, err := ioutil.TempDir("./", "example")
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() {
if err = os.RemoveAll(dir); err != nil {
tb.Fatal(err)
}
})
return dir
}