@@ -15,58 +15,47 @@ import (
15
15
func TestRC (t * testing.T ) {
16
16
tests := []struct {
17
17
name string
18
- cachers [] rc.Cacher
18
+ cacher rc.Cacher
19
19
requests []* http.Request
20
20
wantResponses []* http.Response
21
- wantHits [] int
21
+ wantHit int
22
22
}{
23
- {"all cache" , []rc. Cacher { testutil .NewAllCache (t )} , []* http.Request {
23
+ {"all cache" , testutil .NewAllCache (t ), []* http.Request {
24
24
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
25
25
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
26
26
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
27
27
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/2" )},
28
28
}, []* http.Response {
29
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":1}` )},
30
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":1}` )},
31
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":1}` )},
32
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":2}` )},
33
- }, [] int { 2 } },
34
- {"all cache 2" , []rc. Cacher { testutil .NewAllCache (t )} , []* http.Request {
29
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : [] string { "max-age=60" }, " Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":1}` )},
30
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : [] string { "max-age=60" }, " Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":1}` )},
31
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : [] string { "max-age=60" }, " Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":1}` )},
32
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : [] string { "max-age=60" }, " Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":2}` )},
33
+ }, 2 },
34
+ {"all cache 2" , testutil .NewAllCache (t ), []* http.Request {
35
35
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
36
36
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/2" )},
37
37
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
38
38
}, []* http.Response {
39
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":1}` )},
40
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":2}` )},
41
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":1}` )},
42
- }, [] int { 1 } },
43
- {"get only" , []rc. Cacher { testutil .NewGetOnlyCache (t )} , []* http.Request {
39
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : [] string { "max-age=60" }, " Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":1}` )},
40
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : [] string { "max-age=60" }, " Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":2}` )},
41
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : [] string { "max-age=60" }, " Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":1}` )},
42
+ }, 1 },
43
+ {"get only" , testutil .NewGetOnlyCache (t ), []* http.Request {
44
44
{Method : http .MethodPost , URL : testutil .MustParseURL ("http://example.com/1" )},
45
45
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
46
46
{Method : http .MethodDelete , URL : testutil .MustParseURL ("http://example.com/1" )},
47
47
{Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
48
48
}, []* http.Response {
49
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":1}` )},
50
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":2}` )},
51
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":3}` )},
52
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":2}` )},
53
- }, []int {1 }},
54
- {"multi cache" , []rc.Cacher {testutil .NewGetOnlyCache (t ), testutil .NewAllCache (t )}, []* http.Request {
55
- {Method : http .MethodPost , URL : testutil .MustParseURL ("http://example.com/1" )},
56
- {Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
57
- {Method : http .MethodPost , URL : testutil .MustParseURL ("http://example.com/1" )},
58
- {Method : http .MethodGet , URL : testutil .MustParseURL ("http://example.com/1" )},
59
- }, []* http.Response {
60
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":1}` )},
61
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":2}` )},
62
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":1}` )},
63
- {StatusCode : http .StatusOK , Header : http.Header {"Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":2}` )},
64
- }, []int {1 , 1 }},
49
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : []string {"max-age=60" }, "Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":1}` )},
50
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : []string {"max-age=60" }, "Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":2}` )},
51
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : []string {"max-age=60" }, "Content-Type" : []string {"application/json" }}, Body : testutil .NewBody (`{"count":3}` )},
52
+ {StatusCode : http .StatusOK , Header : http.Header {"Cache-Control" : []string {"max-age=60" }, "Content-Type" : []string {"application/json" }, "X-Cache" : []string {"HIT" }}, Body : testutil .NewBody (`{"count":2}` )},
53
+ }, 1 },
65
54
}
66
55
for _ , tt := range tests {
67
56
t .Run (tt .name , func (t * testing.T ) {
68
57
tr := testutil .NewHTTPRouter (t )
69
- m := rc .New (tt .cachers ... )
58
+ m := rc .New (tt .cacher )
70
59
ts := httptest .NewServer (m (tr ))
71
60
tu := testutil .MustParseURL (ts .URL )
72
61
t .Cleanup (ts .Close )
@@ -77,6 +66,7 @@ func TestRC(t *testing.T) {
77
66
got , err := tc .Do (req )
78
67
if err != nil {
79
68
t .Fatal (err )
69
+ return
80
70
}
81
71
opts := []cmp.Option {
82
72
cmpopts .IgnoreFields (http.Response {}, "Status" , "Proto" , "ProtoMajor" , "ProtoMinor" , "ContentLength" , "TransferEncoding" , "Uncompressed" , "Trailer" , "Request" , "Close" , "Body" ),
@@ -110,11 +100,9 @@ func TestRC(t *testing.T) {
110
100
}
111
101
}
112
102
113
- for i , want := range tt .wantHits {
114
- got := tt .cachers [i ].(testutil.Cacher ).Hit ()
115
- if got != want {
116
- t .Errorf ("got %v want %v" , got , want )
117
- }
103
+ got := tt .cacher .(testutil.Cacher ).Hit ()
104
+ if got != tt .wantHit {
105
+ t .Errorf ("got %v want %v" , got , tt .wantHit )
118
106
}
119
107
})
120
108
}
0 commit comments