Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down