Skip to content

Commit

Permalink
Make sure both the x-etags are always set
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed May 21, 2024
1 parent 6762e99 commit 6aca6f7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
if t.EnableETagPair {
if etag := resp.Header.Get("etag"); etag != "" {
resp.Header.Set(XETag1, etag)
resp.Header.Set(XETag2, cachedXEtag)
etag2 := cachedXEtag
if etag2 == "" {
etag2 = etag
}
resp.Header.Set(XETag2, etag2)
} else {
etagHash = md5.New()
r = struct {
Expand All @@ -261,8 +265,11 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error
if etagHash != nil {
md5Str := hex.EncodeToString(etagHash.Sum(nil))
resp.Header.Set(XETag1, md5Str)
resp.Header.Set(XETag2, cachedXEtag)

etag2 := cachedXEtag
if etag2 == "" {
etag2 = md5Str
}
resp.Header.Set(XETag2, etag2)
}
resp := *resp
resp.Body = io.NopCloser(r)
Expand Down
4 changes: 2 additions & 2 deletions httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func TestEnableETagPair(t *testing.T) {
_, resp := doMethod(t, "GET", "/etag", nil)
c.Assert(resp.StatusCode, qt.Equals, http.StatusOK)
c.Assert(resp.Header.Get(XETag1), qt.Equals, "124567")
c.Assert(resp.Header.Get(XETag2), qt.Equals, "")
c.Assert(resp.Header.Get(XETag2), qt.Equals, "124567")
}
{
_, resp := doMethod(t, "GET", "/etag", nil)
Expand All @@ -238,7 +238,7 @@ func TestEnableETagPair(t *testing.T) {
_, resp := doMethod(t, "GET", "/helloheaderasbody", map[string]string{"Hello": "world1"})
c.Assert(resp.StatusCode, qt.Equals, http.StatusOK)
c.Assert(resp.Header.Get(XETag1), qt.Equals, "48b21a691481958c34cc165011bdb9bc")
c.Assert(resp.Header.Get(XETag2), qt.Equals, "")
c.Assert(resp.Header.Get(XETag2), qt.Equals, "48b21a691481958c34cc165011bdb9bc")
}
{
_, resp := doMethod(t, "GET", "/helloheaderasbody", map[string]string{"Hello": "world2"})
Expand Down

0 comments on commit 6aca6f7

Please sign in to comment.