-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpcache_test.go
65 lines (55 loc) · 1.35 KB
/
httpcache_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
package httpcache
import (
"errors"
"fmt"
"github.com/go-chi/chi"
"net/http"
"net/http/httptest"
"net/http/httputil"
"testing"
)
type DummyCache struct {
cache map[string][]byte
}
func (d *DummyCache) ShouldCached(res *http.Response) bool {
return true
}
func (d *DummyCache) Get(req *http.Request) ([]byte, error) {
if val, ok := d.cache[req.URL.Path]; ok {
return val, nil
}
return nil, errors.New("cache miss")
}
func (d *DummyCache) Set(req *http.Request, res []byte) error {
d.cache[req.URL.Path] = res
return nil
}
func NewDummyCache() Cache {
return &DummyCache{cache: make(map[string][]byte)}
}
func TestRouter(t *testing.T) {
r := chi.NewRouter()
c := NewDummyCache()
r.Use(NewMiddleware(c))
r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Test", "hoge")
w.Write([]byte("welcome"))
})
testServer := httptest.NewServer(r)
defer testServer.Close()
req, _ := http.NewRequest("GET", testServer.URL+"/hello", nil)
req.Header.Set("Authorization", "Bearer access-token")
client := new(http.Client)
resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
result, _ := httputil.DumpResponse(resp, true)
fmt.Println("test1", string(result))
resp2, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
result2, _ := httputil.DumpResponse(resp2, true)
fmt.Println("test2", string(result2))
}