/
cachehandler_test.go
124 lines (107 loc) · 2.87 KB
/
cachehandler_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
package cachehandler_test
import (
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/johejo/cachehandler"
)
func Test(t *testing.T) {
mux := http.NewServeMux()
m := cachehandler.NewMiddleware(1000, 1*time.Hour, cachehandler.BasicKeyFunc())
var called int
mux.Handle("/", m.Wrap(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Add("X-Test", "test-value")
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte("abcdefg"))
called++
})))
ts := httptest.NewServer(mux)
defer ts.Close()
testRequest := func(t *testing.T, mux http.Handler, method, url string, reader io.Reader) {
t.Helper()
req, err := http.NewRequest(method, ts.URL+url, reader)
if err != nil {
t.Fatal(err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
gotBody := string(body)
const wantBody = "abcdefg"
if wantBody != gotBody {
t.Errorf("body should be %s but got %s", wantBody, gotBody)
}
gotHeader := resp.Header.Get("X-Test")
const wantHeader = "test-value"
if gotHeader != wantHeader {
t.Errorf("header should be %s but got %s", wantHeader, gotHeader)
}
if resp.StatusCode != http.StatusInternalServerError {
t.Errorf("status should be %d but got %d", http.StatusInternalServerError, resp.StatusCode)
}
}
for i := 0; i < 5; i++ {
t.Run("GET /foo", func(t *testing.T) {
testRequest(t, mux, http.MethodGet, "/foo", nil)
if called != 1 {
t.Errorf("called should be 1 but called %d times", called)
}
})
}
t.Run("GET /foo/bar", func(t *testing.T) {
testRequest(t, mux, http.MethodGet, "/foo/var", nil)
if called != 2 {
t.Errorf("called should be 2 but called %d times", called)
}
})
t.Run("DELETE /foo/bar", func(t *testing.T) {
testRequest(t, mux, http.MethodDelete, "/foo/var", nil)
if called != 3 {
t.Errorf("called should be 3 but called %d times", called)
}
})
t.Run("POST /foo/bar", func(t *testing.T) {
testRequest(t, mux, http.MethodPost, "/foo/var", strings.NewReader("test-body"))
if called != 4 {
t.Errorf("called should be 4 but called %d times", called)
}
})
}
func TestGroup(t *testing.T) {
mux := http.NewServeMux()
m := cachehandler.NewMiddleware(1000, 1*time.Hour, cachehandler.BasicKeyFunc())
var called int
mux.Handle("/", m.Wrap(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
time.Sleep(1 * time.Second)
called++
})))
ts := httptest.NewServer(mux)
defer ts.Close()
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
resp, err := http.Get(ts.URL + "/foo")
if err != nil {
panic(err)
}
defer resp.Body.Close()
}()
}
wg.Wait()
if called != 1 {
t.Errorf("should be called once but called %d-times", called)
}
}