diff --git a/httpcache.go b/httpcache.go index 8239edc..b26e167 100644 --- a/httpcache.go +++ b/httpcache.go @@ -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 diff --git a/httpcache_test.go b/httpcache_test.go index c2eab02..f22ed83 100644 --- a/httpcache_test.go +++ b/httpcache_test.go @@ -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() {