-
Notifications
You must be signed in to change notification settings - Fork 5
/
request_test.go
55 lines (53 loc) · 1.83 KB
/
request_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
package microcache
import (
"net/http"
"reflect"
"testing"
"time"
)
// buildRequestOpts detects response headers appropriately
func TestBuildRequestOpts(t *testing.T) {
var i = 0
r, _ := http.NewRequest("GET", "/", nil)
type tc struct {
hdr string
val string
exp RequestOpts
}
var runCases = func(m *microcache, cases []tc) {
for _, c := range cases {
res := Response{header: http.Header{}}
res.Header().Set(c.hdr, c.val)
reqOpts := buildRequestOpts(m, res, r)
reqOpts.found = false
if !reflect.DeepEqual(reqOpts, c.exp) {
t.Fatalf("Mismatch in case %d\n%#v\n%#v", i+1, reqOpts, c.exp)
}
i++
}
}
runCases(New(Config{}), []tc{
{"microcache-nocache", "1", RequestOpts{nocache: true}},
{"microcache-ttl", "10", RequestOpts{ttl: time.Duration(10 * time.Second)}},
{"microcache-stale-if-error", "10", RequestOpts{staleIfError: time.Duration(10 * time.Second)}},
{"microcache-stale-while-revalidate", "10", RequestOpts{staleWhileRevalidate: time.Duration(10 * time.Second)}},
{"microcache-collapsed-forwarding", "1", RequestOpts{collapsedForwarding: true}},
{"microcache-stale-recache", "1", RequestOpts{staleRecache: true}},
{"Microcache-Vary-Query", "a", RequestOpts{varyQuery: []string{"a"}}},
})
runCases(New(Config{Nocache: true}), []tc{
{"microcache-cache", "1", RequestOpts{nocache: false}},
})
runCases(New(Config{CollapsedForwarding: true}), []tc{
{"microcache-no-collapsed-forwarding", "1", RequestOpts{collapsedForwarding: false}},
})
runCases(New(Config{StaleRecache: true}), []tc{
{"microcache-no-stale-recache", "1", RequestOpts{staleRecache: false}},
})
runCases(New(Config{Vary: []string{"a"}}), []tc{
{"Microcache-Vary", "b", RequestOpts{vary: []string{"a", "b"}}},
})
runCases(New(Config{Vary: []string{"a"}}), []tc{
{"Vary", "b", RequestOpts{vary: []string{"a", "b"}}},
})
}