Skip to content

Commit

Permalink
Adding WithMethod option
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Nov 3, 2024
1 parent 95a5bda commit 22d0a15
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 31 deletions.
13 changes: 11 additions & 2 deletions diskcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func (c *Cache) EvictKey(key string) error {
}

// Fetch retrieves the key from the cache based on the policy TTL. When forced,
// or if the cached response is stale the request will be executed and cached.
// or if the cached response is stale the request will be executed and the
// response cached.
func (c *Cache) Fetch(key string, p Policy, req *http.Request, force bool) (bool, time.Time, *http.Response, error) {
// check stale
stale, mod, err := c.Stale(req.Context(), key, p.TTL)
Expand Down Expand Up @@ -283,7 +284,15 @@ func (c *Cache) Exec(key string, p Policy, req *http.Request) (*http.Response, e
buf = t.HeaderTransform(buf)
}
// apply body transforms
buf, err = transformAndAppend(buf, res.Body, req.URL.String(), res.StatusCode, res.Header.Get("Content-Type"), p.BodyTransformers...)
buf, err = transformAndAppend(
buf,
res.Body,
req.URL.String(),
res.StatusCode,
res.Header.Get("Content-Type"),
req.Method != "HEAD",
p.BodyTransformers...,
)
if err != nil {
return nil, err
}
Expand Down
51 changes: 51 additions & 0 deletions diskcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"net/http/httputil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -61,6 +62,56 @@ func TestWithContextTTL(t *testing.T) {
}
}

func TestWithMethod(t *testing.T) {
// set up simple test server for demonstration
var count uint64
const size = 20000
s := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.Method == "HEAD" {
res.Header().Add("Content-Length", strconv.Itoa(size))
} else {
fmt.Fprintf(res, "%d\n", atomic.AddUint64(&count, 1))
}
}))
defer s.Close()
baseDir := setupDir(t, "test-with-method")
// create disk cache
c, err := New(
WithBasePathFs(baseDir),
WithMethod("GET", "HEAD"),
WithErrorTruncator(),
WithTTL(1*time.Hour),
)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
cl := &http.Client{
Transport: c,
}
ctx := context.Background()
for i := range 5 {
req, err := http.NewRequestWithContext(ctx, "HEAD", s.URL, nil)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
res, err := cl.Do(req)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
buf, err := httputil.DumpResponse(res, true)
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
t.Logf("%d:\n%s\n---", i, string(buf))
if res.ContentLength != size {
t.Errorf("response %d expected size %d, got: %d", i, size, res.ContentLength)
}
}
if count != 0 {
t.Errorf("expected count %d, got: %d", 0, count)
}
}

func doReq(ctx context.Context, cl *http.Client, urlstr string) (int, error) {
req, err := http.NewRequestWithContext(ctx, "GET", urlstr, nil)
if err != nil {
Expand Down
Loading

0 comments on commit 22d0a15

Please sign in to comment.