Skip to content
This repository was archived by the owner on Apr 20, 2023. It is now read-only.
Closed
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
14 changes: 12 additions & 2 deletions httpcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,8 @@ type cachingReadCloser struct {
// Underlying ReadCloser.
R io.ReadCloser
// OnEOF is called with a copy of the content of R when EOF is reached.
OnEOF func(io.Reader)
OnEOF func(io.Reader)
eofOnce sync.Once

buf bytes.Buffer // buf stores a copy of the content of R.
}
Expand All @@ -534,12 +535,21 @@ func (r *cachingReadCloser) Read(p []byte) (n int, err error) {
n, err = r.R.Read(p)
r.buf.Write(p[:n])
if err == io.EOF {
r.OnEOF(bytes.NewReader(r.buf.Bytes()))
r.eofOnce.Do(func() {
r.OnEOF(bytes.NewReader(r.buf.Bytes()))
})
}
return n, err
}

var dummyBuf = make([]byte, 1)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you help me be confident this is safe to be shared?


func (r *cachingReadCloser) Close() error {
r.eofOnce.Do(func() {
if n, err := r.R.Read(dummyBuf); n == 0 && err == io.EOF {
r.OnEOF(bytes.NewReader(r.buf.Bytes()))
}
})
return r.R.Close()
}

Expand Down
118 changes: 116 additions & 2 deletions httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package httpcache

import (
"bytes"
"compress/gzip"
"encoding/json"
"errors"
"flag"
"io"
Expand All @@ -10,6 +12,7 @@ import (
"net/http/httptest"
"os"
"strconv"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -56,6 +59,30 @@ func setup() {
w.Write([]byte(r.Method))
}))

var compressedJsonCounter = 0
mux.HandleFunc("/compressedJson", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("accept-encoding"), "gzip") {
w.WriteHeader(http.StatusBadRequest)
return
}

compressedJsonCounter++
w.Header().Set("X-Counter", strconv.Itoa(compressedJsonCounter))

etag := "124567"
w.Header().Set("etag", etag)
if r.Header.Get("if-none-match") == etag {
w.WriteHeader(http.StatusNotModified)
return
}

w.Header().Set("Content-Encoding", "gzip")

gzw := gzip.NewWriter(w)
defer gzw.Close()
gzw.Write([]byte(`{"some": "json"}`))
}))

mux.HandleFunc("/range", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lm := "Fri, 14 Dec 2010 01:01:50 GMT"
if r.Header.Get("if-modified-since") == lm {
Expand Down Expand Up @@ -365,10 +392,97 @@ func TestDontStorePartialRangeInCache(t *testing.T) {
}
}

func TestRevalidateCompressedJSONResponses(t *testing.T) {
type some struct{ Some string }
readJsonResponse := func(rc io.ReadCloser) (some, error) {
defer rc.Close()
var got some
err := json.NewDecoder(rc).Decode(&got)
return got, err
}
{
req, err := http.NewRequest("GET", s.server.URL+"/compressedJson", nil)
if err != nil {
t.Fatal(err)
}
resp, err := s.client.Do(req)
if err != nil {
t.Fatal(err)
}
got, err := readJsonResponse(resp.Body)
if err != nil {
t.Fatal(err)
}
want := some{"json"}
if got != want {
t.Errorf("got %q, want %q", got, want)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("response status code isn't 200 OK: %v", resp.StatusCode)
}
if resp.Header.Get(XFromCache) != "" {
t.Error("XFromCache header isn't blank")
}
if resp.Header.Get("x-counter") != "1" {
t.Error("X-Counter header is not 1")
}
if resp.Header.Get("etag") == "" {
t.Error("ETag is blank")
}
}
{
req, err := http.NewRequest("GET", s.server.URL+"/compressedJson", nil)
if err != nil {
t.Fatal(err)
}
resp, err := s.client.Do(req)
if err != nil {
t.Fatal(err)
}
_, err = readJsonResponse(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("response status code isn't 200 OK: %v", resp.StatusCode)
}
if resp.Header.Get(XFromCache) != "1" {
t.Errorf(`XFromCache header isn't "1": %v`, resp.Header.Get(XFromCache))
}
if resp.Header.Get("x-counter") != "2" {
t.Error("X-Counter header is not 2")
}
}
{
req, err := http.NewRequest("GET", s.server.URL+"/compressedJson", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("cache-control", "only-if-cached")
resp, err := s.client.Do(req)
if err != nil {
t.Fatal(err)
}
_, err = readJsonResponse(resp.Body)
if err != nil {
t.Fatal(err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("response status code isn't 200 OK: %v", resp.StatusCode)
}
if resp.Header.Get(XFromCache) != "1" {
t.Errorf(`XFromCache header isn't "1": %v`, resp.Header.Get(XFromCache))
}
if resp.Header.Get("x-counter") != "2" {
t.Error("X-Counter was not updated on revalidation")
}
}
}

func TestCacheOnlyIfBodyRead(t *testing.T) {
resetTest()
{
req, err := http.NewRequest("GET", s.server.URL, nil)
req, err := http.NewRequest("GET", s.server.URL+"/method", nil)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this part changing?

if err != nil {
t.Fatal(err)
}
Expand All @@ -383,7 +497,7 @@ func TestCacheOnlyIfBodyRead(t *testing.T) {
resp.Body.Close()
}
{
req, err := http.NewRequest("GET", s.server.URL, nil)
req, err := http.NewRequest("GET", s.server.URL+"/method", nil)
if err != nil {
t.Fatal(err)
}
Expand Down