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

Use method as cache key prefix for non-GET requests #75

Merged
merged 2 commits into from
Sep 20, 2017
Merged
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
6 changes: 5 additions & 1 deletion httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ type Cache interface {

// cacheKey returns the cache key for req.
func cacheKey(req *http.Request) string {
return req.URL.String()
if req.Method == http.MethodGet {
return req.URL.String()
} else {
return req.Method + " " + req.URL.String()
}
}

// CachedResponse returns the cached http.Response for req if present, and nil
Expand Down
24 changes: 24 additions & 0 deletions httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,30 @@ func TestCacheableMethod(t *testing.T) {
}
}

func TestDontServeHeadResponseToGetRequest(t *testing.T) {
resetTest()
url := s.server.URL + "/"
req, err := http.NewRequest(http.MethodHead, url, nil)
if err != nil {
t.Fatal(err)
}
_, err = s.client.Do(req)
if err != nil {
t.Fatal(err)
}
req, err = http.NewRequest(http.MethodGet, url, nil)
if err != nil {
t.Fatal(err)
}
resp, err := s.client.Do(req)
if err != nil {
t.Fatal(err)
}
if resp.Header.Get(XFromCache) != "" {
t.Errorf("Cache should not match")
}
}

func TestDontStorePartialRangeInCache(t *testing.T) {
resetTest()
{
Expand Down