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

Commit

Permalink
Add failing test case for storing uncacheable method.
Browse files Browse the repository at this point in the history
This test is expected to fail due to the bug. Next commit will resolve
the issue and fix the failing test.
  • Loading branch information
dmitshur committed May 18, 2016
1 parent cd554ea commit ed02b2f
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"flag"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -48,6 +49,11 @@ func setup() {
w.Header().Set("Cache-Control", "max-age=3600")
}))

mux.HandleFunc("/method", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=3600")
w.Write([]byte(r.Method))
}))

mux.HandleFunc("/nostore", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
}))
Expand Down Expand Up @@ -119,6 +125,65 @@ func resetTest() {
clock = &realClock{}
}

// TestCacheableMethod ensures that uncacheable method does not get stored
// in cache and get incorrectly used for a following cacheable method request.
func TestCacheableMethod(t *testing.T) {
resetTest()
{
req, err := http.NewRequest("POST", s.server.URL+"/method", nil)
if err != nil {
t.Fatal(err)
}
resp, err := s.client.Do(req)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, resp.Body)
if err != nil {
t.Fatal(err)
}
err = resp.Body.Close()
if err != nil {
t.Fatal(err)
}
if got, want := buf.String(), "POST"; 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)
}
}
{
req, err := http.NewRequest("GET", s.server.URL+"/method", nil)
if err != nil {
t.Fatal(err)
}
resp, err := s.client.Do(req)
if err != nil {
t.Fatal(err)
}
var buf bytes.Buffer
_, err = io.Copy(&buf, resp.Body)
if err != nil {
t.Fatal(err)
}
err = resp.Body.Close()
if err != nil {
t.Fatal(err)
}
if got, want := buf.String(), "GET"; got != want {
t.Errorf("got wrong body %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.Errorf("XFromCache header isn't blank")
}
}
}

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

0 comments on commit ed02b2f

Please sign in to comment.