Skip to content

Commit

Permalink
add x-cache header
Browse files Browse the repository at this point in the history
  • Loading branch information
acoshift committed Jul 30, 2017
1 parent d3bd9ab commit 597e0ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cachestatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,12 @@ func New(c Config) func(http.Handler) http.Handler {
}
}

w.Header().Set("X-Cache", "HIT")
io.Copy(w, bytes.NewReader(ci.data))
return
}
l.RUnlock()
w.Header().Set("X-Cache", "MISS")
cw := &responseWriter{
ResponseWriter: w,
cache: &bytes.Buffer{},
Expand Down
9 changes: 9 additions & 0 deletions cachestatic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,19 @@ func TestCachestatic(t *testing.T) {
ts := httptest.NewServer(New(DefaultConfig)(createTestHandler()))
defer ts.Close()

hit := false

verify := func(resp *http.Response, err error) {
if err != nil {
t.Fatalf("expected error to be nil; got %v", err)
}
xCache := resp.Header.Get("X-Cache")
if !hit && xCache != "MISS" {
t.Fatalf("expected X-Cache to be MISS; got %s", xCache)
} else if hit && xCache != "HIT" {
t.Fatalf("expected X-Cache to be HIT; got %s", xCache)
}
hit = true
if resp.Header.Get(header.ContentType) != "text/plain; charset=utf-8" {
t.Fatalf("invalid Content-Type; got %v", resp.Header.Get(header.ContentType))
}
Expand Down

0 comments on commit 597e0ba

Please sign in to comment.