Repository navigation
Expand file tree
/
Copy pathmultivalue_header_test.go
More file actions
104 lines (91 loc) · 2.76 KB
/
Copy pathmultivalue_header_test.go
File metadata and controls
104 lines (91 loc) · 2.76 KB
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
package cache
import (
"net/http"
"net/http/httptest"
"reflect"
"sync"
"testing"
"time"
)
type releaseCountingAdapter struct {
sync.Mutex
store map[uint64][]byte
releaseCalls int
}
func (a *releaseCountingAdapter) Get(key uint64) ([]byte, bool) {
a.Lock()
defer a.Unlock()
v, ok := a.store[key]
return v, ok
}
func (a *releaseCountingAdapter) Set(key uint64, b []byte, _ time.Time) {
a.Lock()
defer a.Unlock()
a.store[key] = b
}
func (a *releaseCountingAdapter) Release(key uint64) {
a.Lock()
defer a.Unlock()
a.releaseCalls++
delete(a.store, key)
}
func TestMiddlewareDoesNotRefreshWhenRefreshKeyEmpty(t *testing.T) {
adapter := &releaseCountingAdapter{store: map[uint64][]byte{}}
client, err := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Minute),
// no ClientWithRefreshKey -> refreshKey == ""
)
if err != nil {
t.Fatal(err)
}
calls := 0
handler := client.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
calls++
w.Write([]byte("v"))
}))
// Warm the cache for /foo.
handler.ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet, "http://x/foo", nil))
// Send /foo?=junk: with refreshKey == "", params[""] would match and
// release the cache entry for /foo. After the fix, no release happens.
handler.ServeHTTP(httptest.NewRecorder(),
httptest.NewRequest(http.MethodGet, "http://x/foo?=junk", nil))
if adapter.releaseCalls != 0 {
t.Fatalf("release was called %d time(s); empty refreshKey must never trigger refresh", adapter.releaseCalls)
}
_ = calls
}
func TestMiddlewarePreservesMultiValuedHeaders(t *testing.T) {
adapter := &adapterMock{store: map[uint64][]byte{}}
client, err := NewClient(
ClientWithAdapter(adapter),
ClientWithTTL(1*time.Minute),
)
if err != nil {
t.Fatal(err)
}
handler := client.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Set-Cookie", "a=1; Path=/")
w.Header().Add("Set-Cookie", "b=2; Path=/")
w.Header().Add("Link", "</one>; rel=preload")
w.Header().Add("Link", "</two>; rel=preload")
w.Write([]byte("ok"))
}))
wantCookies := []string{"a=1; Path=/", "b=2; Path=/"}
wantLinks := []string{"</one>; rel=preload", "</two>; rel=preload"}
for i, label := range []string{"first", "cached"} {
r := httptest.NewRequest(http.MethodGet, "http://x/cookies", nil)
w := httptest.NewRecorder()
handler.ServeHTTP(w, r)
gotCookies := w.Result().Header.Values("Set-Cookie")
if !reflect.DeepEqual(gotCookies, wantCookies) {
t.Errorf("%s response Set-Cookie = %#v, want %#v", label, gotCookies, wantCookies)
}
gotLinks := w.Result().Header.Values("Link")
if !reflect.DeepEqual(gotLinks, wantLinks) {
t.Errorf("%s response Link = %#v, want %#v", label, gotLinks, wantLinks)
}
_ = i
}
}