-
Notifications
You must be signed in to change notification settings - Fork 15
/
cache_test.go
109 lines (94 loc) · 2.83 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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http/httptest"
"strings"
"testing"
)
type mockFileSystem struct {
}
func (fs mockFileSystem) WriteFile(path string, content []byte) error {
return nil
}
func (fs mockFileSystem) ReadFile(path string) ([]byte, error) {
if strings.HasSuffix(path, "-error") {
return nil, fmt.Errorf("SOMETHING BROKE")
}
// Return specs when spec.json is requested (which should be always)
if strings.HasSuffix(path, "spec.json") {
specs := []Spec{
Spec{
Key: "key",
SpecResponse: SpecResponse{
StatusCode: 418,
ContentFile: "key",
Headers: map[string]string{"Content-Type": "text/plain"},
},
},
}
var specsContent bytes.Buffer
dec := json.NewEncoder(&specsContent)
_ = dec.Encode(specs)
return specsContent.Bytes(), nil
}
// Otherwise we're returning a file that has been cached
return []byte("CACHED CONTENT FILE"), nil
}
func TestDiskCacherGet(t *testing.T) {
cacher := NewDiskCacher("")
cacher.FileSystem = mockFileSystem{}
cacher.SeedCache()
response := cacher.Get("key")
if response.StatusCode != 418 {
t.Errorf("Got: `%v`; Expected: `418`", response.StatusCode)
}
if response.Headers["Content-Type"] != "text/plain" {
t.Errorf("Got: `%v`; Expected: `text/plain`", response.Headers["Content-Type"])
}
if string(response.Body) != "CACHED CONTENT FILE" {
t.Errorf("Got: `%v`; Expected: `CACHED CONTENT FILE`", string(response.Body))
}
}
func TestDiskCacherPut(t *testing.T) {
cacher := NewDiskCacher("")
cacher.FileSystem = mockFileSystem{}
cacher.SeedCache()
recorder := httptest.NewRecorder()
var body bytes.Buffer
_, _ = body.WriteString("THIS IS A NEW BODY")
recorder.Header().Set("Content-Type", "text/plain")
recorder.Code = 700
recorder.Body = &body
response := cacher.Put("new_key", recorder)
if response.StatusCode != 700 {
t.Errorf("Got: `%v`; Expected: `700`", response.StatusCode)
}
if response.Headers["Content-Type"] != "text/plain" {
t.Errorf("Got: `%v`; Expected: `text/plain`", response.Headers["Content-Type"])
}
if string(response.Body) != "THIS IS A NEW BODY" {
t.Errorf("Got: `%v`; Expected: `THIS IS A NEW BODY`", string(response.Body))
}
}
func TestDiskCacherSeedCacheNoSpecs(t *testing.T) {
cacher := NewDiskCacher("")
cacher.FileSystem = mockFileSystem{}
cacher.specPath = "-error"
cacher.SeedCache()
if len(cacher.cache) != 0 {
t.Errorf("Got: `%v`; Expected: `0`", len(cacher.cache))
}
}
func TestDiskCacherPutSkipDiskSeeded(t *testing.T) {
cacher := NewDiskCacher("")
cacher.FileSystem = mockFileSystem{}
cacher.SeedCache()
recorder := httptest.NewRecorder()
recorder.Header().Set("_chameleon-seeded-skip-disk", "true")
response := cacher.Put("new_key", recorder)
if _, ok := response.Headers["_chameleon-seeded-skip-disk"]; ok {
t.Errorf("Unexpected header `_chameleon-seeded-skip-disk`")
}
}