forked from lox/httpcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
51 lines (40 loc) · 1.14 KB
/
cache_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
package httpcache_test
import (
"net/http"
"strings"
"testing"
"github.com/lox/httpcache"
"github.com/stretchr/testify/require"
)
func TestSaveResource(t *testing.T) {
var body = strings.Repeat("llamas", 5000)
var cache = httpcache.NewMemoryCache()
res := httpcache.NewResourceBytes(http.StatusOK, []byte(body), http.Header{
"Llamas": []string{"true"},
})
if err := cache.Store(res, "testkey"); err != nil {
t.Fatal(err)
}
resOut, err := cache.Retrieve("testkey")
if err != nil {
t.Fatal(err)
}
require.NotNil(t, resOut)
require.Equal(t, res.Header(), resOut.Header())
require.Equal(t, body, readAllString(resOut))
}
func TestSaveResourceWithIncorrectContentLength(t *testing.T) {
var body = "llamas"
var cache = httpcache.NewMemoryCache()
res := httpcache.NewResourceBytes(http.StatusOK, []byte(body), http.Header{
"Llamas": []string{"true"},
"Content-Length": []string{"10"},
})
if err := cache.Store(res, "testkey"); err == nil {
t.Fatal("Entry should have generated an error")
}
_, err := cache.Retrieve("testkey")
if err != httpcache.ErrNotFoundInCache {
t.Fatal("Entry shouldn't have been cached")
}
}