From 538477a59da5272b3237eeecd8a92e09923ba68d Mon Sep 17 00:00:00 2001 From: Alvaro Aleman Date: Fri, 18 Jun 2021 16:17:27 -0400 Subject: [PATCH] Allow recognizing responses that got re-validated When MarkCacheResponse is set, this change will add a header if a response got revalidated, so that consumers can detect that. --- httpcache.go | 5 +++++ httpcache_test.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/httpcache.go b/httpcache.go index b41a63d..8f21e40 100644 --- a/httpcache.go +++ b/httpcache.go @@ -25,6 +25,8 @@ const ( transparent // XFromCache is the header added to responses that are returned from the cache XFromCache = "X-From-Cache" + // XRevalidated is the header added to responses that got revalidated + XRevalidated = "X-Revalidated" ) // A Cache interface is used by the Transport to store and retrieve responses. @@ -192,6 +194,9 @@ func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error for _, header := range endToEndHeaders { cachedResp.Header[header] = resp.Header[header] } + if t.MarkCachedResponses { + cachedResp.Header[XRevalidated] = []string{"1"} + } resp = cachedResp } else if (err != nil || (cachedResp != nil && resp.StatusCode >= 500)) && req.Method == "GET" && canStaleOnError(cachedResp.Header, req.Header) { diff --git a/httpcache_test.go b/httpcache_test.go index a504641..af7342f 100644 --- a/httpcache_test.go +++ b/httpcache_test.go @@ -564,6 +564,9 @@ func TestGetWithEtag(t *testing.T) { if resp.Header.Get(XFromCache) != "1" { t.Fatalf(`XFromCache header isn't "1": %v`, resp.Header.Get(XFromCache)) } + if resp.Header.Get(XRevalidated) != "1" { + t.Fatalf(`XRevalidated isn't "1": %v`, resp.Header.Get(XRevalidated)) + } // additional assertions to verify that 304 response is converted properly if resp.StatusCode != http.StatusOK { t.Fatalf("response status code isn't 200 OK: %v", resp.StatusCode)