-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache_test.go
179 lines (168 loc) · 4.41 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package k
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
)
const (
DATA_LENGTH = 8 * (1 << 10)
)
type Response struct {
Header map[string]string
ReadAtMost int // Optional
BodyLength int
}
type TableTest struct {
Handler http.Handler
Responses []Response
RequestCount int // Optional
}
var (
tests = map[string]TableTest{
"Caching": TableTest{
Handler: NewDummyDataHandler(DATA_LENGTH, DATA_LENGTH, 200),
RequestCount: 1,
Responses: []Response{
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DATA_LENGTH),
},
BodyLength: DATA_LENGTH,
},
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DATA_LENGTH),
},
BodyLength: DATA_LENGTH,
},
},
},
"ClientReadAbort": TableTest{
Handler: NewDummyDataHandler(DATA_LENGTH, DATA_LENGTH, 200),
Responses: []Response{
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DATA_LENGTH),
},
ReadAtMost: 4,
BodyLength: 4,
},
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DATA_LENGTH),
},
BodyLength: DATA_LENGTH,
},
},
},
"ServerWriteAbort": TableTest{
Handler: NewDummyDataHandler(DATA_LENGTH, DATA_LENGTH/2, 200),
RequestCount: 2,
Responses: []Response{
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DATA_LENGTH),
},
BodyLength: DATA_LENGTH / 2,
},
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DATA_LENGTH),
},
BodyLength: DATA_LENGTH / 2,
},
},
},
"CacheOnly200": TableTest{
Handler: NewDummyDataHandler(DATA_LENGTH, DATA_LENGTH, 201),
RequestCount: 2,
Responses: []Response{
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DATA_LENGTH),
},
BodyLength: DATA_LENGTH,
},
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DATA_LENGTH),
},
BodyLength: DATA_LENGTH,
},
},
},
"AboveCacheLimit": TableTest{
Handler: NewDummyDataHandler(DEFAULT_MAX_CACHEABLE_SIZE+1, DEFAULT_MAX_CACHEABLE_SIZE+1, 200),
RequestCount: 2,
Responses: []Response{
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DEFAULT_MAX_CACHEABLE_SIZE+1),
},
BodyLength: DEFAULT_MAX_CACHEABLE_SIZE + 1,
},
{
Header: map[string]string{
"Content-Length": fmt.Sprintf("%d", DEFAULT_MAX_CACHEABLE_SIZE+1),
},
BodyLength: DEFAULT_MAX_CACHEABLE_SIZE + 1,
},
},
},
}
)
type RequestCounterHandler struct {
Handler http.Handler
Counter int
}
func (rc *RequestCounterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rc.Counter++
rc.Handler.ServeHTTP(w, r)
}
func TestCache_table(t *testing.T) {
for name, test := range tests {
t.Logf("Running %s...", name)
rc := &RequestCounterHandler{Handler: test.Handler}
server := httptest.NewServer(NewCache(1*time.Minute, rc))
for _, expectedresp := range test.Responses {
http.DefaultTransport.(*http.Transport).CloseIdleConnections()
resp, err := http.Get(server.URL)
if err != nil {
t.Fatalf("%s: Could not send request: %s", name, err)
}
for header, expectedv := range expectedresp.Header {
if v := resp.Header.Get(header); v != expectedv {
t.Fatalf("%s: Header field %s has unexpected value. Expected \"%s\", got \"%s\"", name, header, expectedv, v)
}
}
var buf []byte
if expectedresp.ReadAtMost != 0 {
buf = make([]byte, expectedresp.ReadAtMost)
_, err = resp.Body.Read(buf)
} else {
buf, err = ioutil.ReadAll(resp.Body)
}
if err != nil {
t.Fatalf("%s: Reading from body failed: %s", name, err)
}
if len(buf) != expectedresp.BodyLength {
t.Fatalf("%s: Unexpected body length. Expected %d, got %d", name, expectedresp.BodyLength, len(buf))
}
}
server.Close()
if test.RequestCount != 0 && rc.Counter != test.RequestCount {
t.Fatalf("%s: Unexpected request count. Expected %d, got %d", name, test.RequestCount, rc.Counter)
}
}
}
func NewDummyDataHandler(announcedContentLength, actualContentLength, errorCode int) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Length", fmt.Sprintf("%d", announcedContentLength))
w.WriteHeader(errorCode)
buf := make([]byte, actualContentLength)
w.Write(buf)
})
}