@@ -12,7 +12,6 @@ import (
1212 "github.com/stretchr/testify/require"
1313)
1414
15- // roundTripFunc is a helper to create http.RoundTripper from a function.
1615type roundTripFunc func (* http.Request ) (* http.Response , error )
1716
1817func (f roundTripFunc ) RoundTrip (r * http.Request ) (* http.Response , error ) {
@@ -31,7 +30,6 @@ func makeResponse(statusCode int, body string, headers map[string]string) *http.
3130 return resp
3231}
3332
34- // TestIsETagTargetPath verifies which URL paths are considered ETag-cache targets.
3533func TestIsETagTargetPath (t * testing.T ) {
3634 t .Parallel ()
3735
@@ -126,7 +124,6 @@ func TestIsETagTargetPath(t *testing.T) {
126124 }
127125}
128126
129- // TestNewETagCacheTransport verifies the constructor.
130127func TestNewETagCacheTransport (t * testing.T ) {
131128 t .Parallel ()
132129
@@ -138,20 +135,19 @@ func TestNewETagCacheTransport(t *testing.T) {
138135
139136 t .Run ("non-nil base is used" , func (t * testing.T ) {
140137 t .Parallel ()
141- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
138+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
142139 return nil , nil
143140 })
144141 transport := newETagCacheTransport (base )
145142 require .NotNil (t , transport )
146143 })
147144}
148145
149- // TestETagCacheTransport_RoundTrip_NonGET verifies that non-GET requests bypass caching entirely.
150146func TestETagCacheTransport_RoundTrip_NonGET (t * testing.T ) {
151147 t .Parallel ()
152148
153149 called := 0
154- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
150+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
155151 called ++
156152 return makeResponse (http .StatusOK , "ok" , nil ), nil
157153 })
@@ -170,14 +166,12 @@ func TestETagCacheTransport_RoundTrip_NonGET(t *testing.T) {
170166 assert .Equal (t , len (methods ), called , "base should be called once per non-GET method" )
171167}
172168
173- // TestETagCacheTransport_RoundTrip_NonCacheablePath verifies requests to non-ETag paths are not cached.
174169func TestETagCacheTransport_RoundTrip_NonCacheablePath (t * testing.T ) {
175170 t .Parallel ()
176171
177172 callCount := 0
178- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
173+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
179174 callCount ++
180- // Never returns ETag header
181175 return makeResponse (http .StatusOK , "data" , nil ), nil
182176 })
183177
@@ -188,16 +182,13 @@ func TestETagCacheTransport_RoundTrip_NonCacheablePath(t *testing.T) {
188182 require .NoError (t , err )
189183 assert .Equal (t , http .StatusOK , resp .StatusCode )
190184
191- // Second call: path not cacheable so no If-None-Match is sent
192185 req2 := httptest .NewRequest (http .MethodGet , "http://example.com/api/v3/messages" , nil )
193186 resp2 , err := transport .RoundTrip (req2 )
194187 require .NoError (t , err )
195188 assert .Equal (t , http .StatusOK , resp2 .StatusCode )
196189 assert .Equal (t , 2 , callCount )
197190}
198191
199- // TestETagCacheTransport_RoundTrip_FirstRequestCachesETag verifies that a 200 response
200- // with an ETag header is stored in the cache.
201192func TestETagCacheTransport_RoundTrip_FirstRequestCachesETag (t * testing.T ) {
202193 t .Parallel ()
203194
@@ -221,29 +212,24 @@ func TestETagCacheTransport_RoundTrip_FirstRequestCachesETag(t *testing.T) {
221212 assert .Equal (t , 1 , callCount )
222213 assert .Empty (t , receivedIfNoneMatch , "first request must not send If-None-Match" )
223214
224- // Second request must send If-None-Match with the stored ETag
225215 req2 := httptest .NewRequest (http .MethodGet , "http://example.com/api/v3/users" , nil )
226216 _ , err = transport .RoundTrip (req2 )
227217 require .NoError (t , err )
228218 assert .Equal (t , etag , receivedIfNoneMatch , "second request must send stored ETag as If-None-Match" )
229219}
230220
231- // TestETagCacheTransport_RoundTrip_304UsesCachedBody verifies that a 304 Not Modified response
232- // causes the transport to return the previously cached body as 200.
233221func TestETagCacheTransport_RoundTrip_304UsesCachedBody (t * testing.T ) {
234222 t .Parallel ()
235223
236224 const etag = `"v1"`
237225 const cachedBody = `["user1","user2"]`
238226
239227 callNum := 0
240- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
228+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
241229 callNum ++
242230 if callNum == 1 {
243- // First call: return 200 with ETag
244231 return makeResponse (http .StatusOK , cachedBody , map [string ]string {"ETag" : etag }), nil
245232 }
246- // Second call: return 304 Not Modified (empty body)
247233 return makeResponse (http .StatusNotModified , "" , nil ), nil
248234 })
249235
@@ -264,33 +250,28 @@ func TestETagCacheTransport_RoundTrip_304UsesCachedBody(t *testing.T) {
264250 assert .Equal (t , cachedBody , string (bodyBytes ))
265251}
266252
267- // TestETagCacheTransport_RoundTrip_304NoCacheFallthrough verifies that a 304 without a cached entry
268- // is returned as-is (edge/error case).
269253func TestETagCacheTransport_RoundTrip_304NoCacheFallthrough (t * testing.T ) {
270254 t .Parallel ()
271255
272- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
256+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
273257 return makeResponse (http .StatusNotModified , "" , nil ), nil
274258 })
275259
276260 transport := newETagCacheTransport (base ).(* etagCacheTransport )
277261 req := httptest .NewRequest (http .MethodGet , "http://example.com/api/v3/users" , nil )
278262 resp , err := transport .RoundTrip (req )
279263 require .NoError (t , err )
280- // No cached entry, so the raw 304 is returned
281264 assert .Equal (t , http .StatusNotModified , resp .StatusCode )
282265}
283266
284- // TestETagCacheTransport_RoundTrip_5xxFallback verifies that a 5xx response falls back to
285- // cached data if available.
286267func TestETagCacheTransport_RoundTrip_5xxFallback (t * testing.T ) {
287268 t .Parallel ()
288269
289270 const etag = `"v2"`
290271 const cachedBody = `["stamp1"]`
291272
292273 callNum := 0
293- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
274+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
294275 callNum ++
295276 if callNum == 1 {
296277 return makeResponse (http .StatusOK , cachedBody , map [string ]string {"ETag" : etag }), nil
@@ -315,12 +296,10 @@ func TestETagCacheTransport_RoundTrip_5xxFallback(t *testing.T) {
315296 assert .Equal (t , cachedBody , string (bodyBytes ))
316297}
317298
318- // TestETagCacheTransport_RoundTrip_5xxNoCacheFallthrough verifies that a 5xx without a cached
319- // entry is returned as-is.
320299func TestETagCacheTransport_RoundTrip_5xxNoCacheFallthrough (t * testing.T ) {
321300 t .Parallel ()
322301
323- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
302+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
324303 return makeResponse (http .StatusServiceUnavailable , "unavailable" , nil ), nil
325304 })
326305
@@ -331,8 +310,6 @@ func TestETagCacheTransport_RoundTrip_5xxNoCacheFallthrough(t *testing.T) {
331310 assert .Equal (t , http .StatusServiceUnavailable , resp .StatusCode )
332311}
333312
334- // TestETagCacheTransport_RoundTrip_NetworkErrorWithCache verifies that a network error falls back
335- // to cached data if available.
336313func TestETagCacheTransport_RoundTrip_NetworkErrorWithCache (t * testing.T ) {
337314 t .Parallel ()
338315
@@ -341,7 +318,7 @@ func TestETagCacheTransport_RoundTrip_NetworkErrorWithCache(t *testing.T) {
341318 networkErr := errors .New ("connection refused" )
342319
343320 callNum := 0
344- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
321+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
345322 callNum ++
346323 if callNum == 1 {
347324 return makeResponse (http .StatusOK , cachedBody , map [string ]string {"ETag" : etag }), nil
@@ -366,13 +343,11 @@ func TestETagCacheTransport_RoundTrip_NetworkErrorWithCache(t *testing.T) {
366343 assert .Equal (t , cachedBody , string (bodyBytes ))
367344}
368345
369- // TestETagCacheTransport_RoundTrip_NetworkErrorNoCache verifies that a network error without
370- // cached data propagates the error.
371346func TestETagCacheTransport_RoundTrip_NetworkErrorNoCache (t * testing.T ) {
372347 t .Parallel ()
373348
374349 networkErr := errors .New ("connection refused" )
375- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
350+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
376351 return nil , networkErr
377352 })
378353
@@ -383,12 +358,10 @@ func TestETagCacheTransport_RoundTrip_NetworkErrorNoCache(t *testing.T) {
383358 assert .ErrorIs (t , err , networkErr )
384359}
385360
386- // TestETagCacheTransport_RoundTrip_NonOKNonSpecialStatus verifies that non-200/304/5xx status
387- // codes (e.g. 401, 404) are returned as-is without caching.
388361func TestETagCacheTransport_RoundTrip_NonOKNonSpecialStatus (t * testing.T ) {
389362 t .Parallel ()
390363
391- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
364+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
392365 return makeResponse (http .StatusUnauthorized , "unauthorized" , nil ), nil
393366 })
394367
@@ -399,8 +372,6 @@ func TestETagCacheTransport_RoundTrip_NonOKNonSpecialStatus(t *testing.T) {
399372 assert .Equal (t , http .StatusUnauthorized , resp .StatusCode )
400373}
401374
402- // TestETagCacheTransport_RoundTrip_200WithoutETagNotCached verifies that 200 responses without
403- // an ETag header are not cached (no If-None-Match on subsequent calls).
404375func TestETagCacheTransport_RoundTrip_200WithoutETagNotCached (t * testing.T ) {
405376 t .Parallel ()
406377
@@ -409,7 +380,6 @@ func TestETagCacheTransport_RoundTrip_200WithoutETagNotCached(t *testing.T) {
409380 base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
410381 callCount ++
411382 receivedIfNoneMatch = r .Header .Get ("If-None-Match" )
412- // No ETag in response
413383 return makeResponse (http .StatusOK , "data" , nil ), nil
414384 })
415385
@@ -425,15 +395,13 @@ func TestETagCacheTransport_RoundTrip_200WithoutETagNotCached(t *testing.T) {
425395 assert .Equal (t , 2 , callCount )
426396}
427397
428- // TestETagCacheTransport_RoundTrip_CacheHitHeader verifies that responses served from cache
429- // include the X-AnkeTo-Cache: hit header.
430398func TestETagCacheTransport_RoundTrip_CacheHitHeader (t * testing.T ) {
431399 t .Parallel ()
432400
433401 const etag = `"hdr-test"`
434402
435403 callNum := 0
436- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
404+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
437405 callNum ++
438406 if callNum == 1 {
439407 return makeResponse (http .StatusOK , "body" , map [string ]string {"ETag" : etag }), nil
@@ -452,16 +420,13 @@ func TestETagCacheTransport_RoundTrip_CacheHitHeader(t *testing.T) {
452420 assert .Equal (t , "hit" , resp .Header .Get ("X-AnkeTo-Cache" ))
453421}
454422
455- // TestETagCacheTransport_ConcurrentAccess verifies that the transport is safe under concurrent load.
456423func TestETagCacheTransport_ConcurrentAccess (t * testing.T ) {
457424 t .Parallel ()
458425
459426 const etag = `"concurrent"`
460427 const body = `["a","b"]`
461428
462- callNum := 0
463- base := roundTripFunc (func (r * http.Request ) (* http.Response , error ) {
464- callNum ++
429+ base := roundTripFunc (func (_ * http.Request ) (* http.Response , error ) {
465430 return makeResponse (http .StatusOK , body , map [string ]string {"ETag" : etag }), nil
466431 })
467432
@@ -482,5 +447,4 @@ func TestETagCacheTransport_ConcurrentAccess(t *testing.T) {
482447 for i := 0 ; i < goroutines ; i ++ {
483448 <- done
484449 }
485- // No race detector failure is the assertion here.
486- }
450+ }
0 commit comments