-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcache_backend_test.go
94 lines (69 loc) · 1.8 KB
/
cache_backend_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
package templar
import (
"io/ioutil"
"net/http"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vektra/neko"
)
func TestCache(t *testing.T) {
n := neko.Start(t)
var (
cache *Cache
)
n.Setup(func() {
cache = NewMemoryCache(30 * time.Second)
})
n.It("can store and retrieve responses", func() {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)
upstream := &http.Response{
Request: req,
StatusCode: 304,
Status: "304 Too Funky",
Header: make(http.Header),
}
cache.Set(req, upstream)
out, ok := cache.Get(req)
require.True(t, ok)
assert.Equal(t, upstream.StatusCode, out.StatusCode)
assert.Equal(t, upstream.Header, out.Header)
})
n.It("makes the response body readable", func() {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)
funky := "waaay too funky"
upstream := &http.Response{
Request: req,
StatusCode: 304,
Status: "304 Too Funky",
Body: ioutil.NopCloser(strings.NewReader(funky)),
}
cache.Set(req, upstream)
_, err = ioutil.ReadAll(upstream.Body)
require.NoError(t, err)
out, ok := cache.Get(req)
require.True(t, ok)
bytes, err := ioutil.ReadAll(out.Body)
require.NoError(t, err)
assert.Equal(t, funky, string(bytes))
})
n.It("honors cache time requested in header", func() {
req, err := http.NewRequest("GET", "http://google.com/foo/bar", nil)
require.NoError(t, err)
upstream := &http.Response{
Request: req,
StatusCode: 304,
Status: "304 Too Funky",
}
req.Header.Set(CacheTimeHeader, "1s")
cache.Set(req, upstream)
time.Sleep(1 * time.Second)
_, ok := cache.Get(req)
require.False(t, ok)
})
n.Meow()
}