Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.

Commit 135302a

Browse files
committed
Make sure both the x-etags are always set
1 parent 6762e99 commit 135302a

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

‎httpcache.go‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
242242
if t.EnableETagPair {
243243
if etag := resp.Header.Get("etag"); etag != "" {
244244
resp.Header.Set(XETag1, etag)
245-
resp.Header.Set(XETag2, cachedXEtag)
245+
etag2 := cachedXEtag
246+
if etag2 == "" {
247+
etag2 = etag
248+
}
249+
resp.Header.Set(XETag2, etag2)
246250
} else {
247251
etagHash = md5.New()
248252
r = struct {
@@ -261,8 +265,11 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
261265
if etagHash != nil {
262266
md5Str := hex.EncodeToString(etagHash.Sum(nil))
263267
resp.Header.Set(XETag1, md5Str)
264-
resp.Header.Set(XETag2, cachedXEtag)
265-
268+
etag2 := cachedXEtag
269+
if etag2 == "" {
270+
etag2 = md5Str
271+
}
272+
resp.Header.Set(XETag2, etag2)
266273
}
267274
resp := *resp
268275
resp.Body = io.NopCloser(r)

‎httpcache_test.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func TestEnableETagPair(t *testing.T) {
224224
_, resp := doMethod(t, "GET", "/etag", nil)
225225
c.Assert(resp.StatusCode, qt.Equals, http.StatusOK)
226226
c.Assert(resp.Header.Get(XETag1), qt.Equals, "124567")
227-
c.Assert(resp.Header.Get(XETag2), qt.Equals, "")
227+
c.Assert(resp.Header.Get(XETag2), qt.Equals, "124567")
228228
}
229229
{
230230
_, resp := doMethod(t, "GET", "/etag", nil)
@@ -238,7 +238,7 @@ func TestEnableETagPair(t *testing.T) {
238238
_, resp := doMethod(t, "GET", "/helloheaderasbody", map[string]string{"Hello": "world1"})
239239
c.Assert(resp.StatusCode, qt.Equals, http.StatusOK)
240240
c.Assert(resp.Header.Get(XETag1), qt.Equals, "48b21a691481958c34cc165011bdb9bc")
241-
c.Assert(resp.Header.Get(XETag2), qt.Equals, "")
241+
c.Assert(resp.Header.Get(XETag2), qt.Equals, "48b21a691481958c34cc165011bdb9bc")
242242
}
243243
{
244244
_, resp := doMethod(t, "GET", "/helloheaderasbody", map[string]string{"Hello": "world2"})

0 commit comments

Comments
 (0)