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

Commit ed02b2f

Browse files
committed
Add failing test case for storing uncacheable method.
This test is expected to fail due to the bug. Next commit will resolve the issue and fix the failing test.
1 parent cd554ea commit ed02b2f

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

‎httpcache_test.go‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"flag"
7+
"io"
78
"io/ioutil"
89
"net/http"
910
"net/http/httptest"
@@ -48,6 +49,11 @@ func setup() {
4849
w.Header().Set("Cache-Control", "max-age=3600")
4950
}))
5051

52+
mux.HandleFunc("/method", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
53+
w.Header().Set("Cache-Control", "max-age=3600")
54+
w.Write([]byte(r.Method))
55+
}))
56+
5157
mux.HandleFunc("/nostore", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5258
w.Header().Set("Cache-Control", "no-store")
5359
}))
@@ -119,6 +125,65 @@ func resetTest() {
119125
clock = &realClock{}
120126
}
121127

128+
// TestCacheableMethod ensures that uncacheable method does not get stored
129+
// in cache and get incorrectly used for a following cacheable method request.
130+
func TestCacheableMethod(t *testing.T) {
131+
resetTest()
132+
{
133+
req, err := http.NewRequest("POST", s.server.URL+"/method", nil)
134+
if err != nil {
135+
t.Fatal(err)
136+
}
137+
resp, err := s.client.Do(req)
138+
if err != nil {
139+
t.Fatal(err)
140+
}
141+
var buf bytes.Buffer
142+
_, err = io.Copy(&buf, resp.Body)
143+
if err != nil {
144+
t.Fatal(err)
145+
}
146+
err = resp.Body.Close()
147+
if err != nil {
148+
t.Fatal(err)
149+
}
150+
if got, want := buf.String(), "POST"; got != want {
151+
t.Errorf("got %q, want %q", got, want)
152+
}
153+
if resp.StatusCode != http.StatusOK {
154+
t.Errorf("response status code isn't 200 OK: %v", resp.StatusCode)
155+
}
156+
}
157+
{
158+
req, err := http.NewRequest("GET", s.server.URL+"/method", nil)
159+
if err != nil {
160+
t.Fatal(err)
161+
}
162+
resp, err := s.client.Do(req)
163+
if err != nil {
164+
t.Fatal(err)
165+
}
166+
var buf bytes.Buffer
167+
_, err = io.Copy(&buf, resp.Body)
168+
if err != nil {
169+
t.Fatal(err)
170+
}
171+
err = resp.Body.Close()
172+
if err != nil {
173+
t.Fatal(err)
174+
}
175+
if got, want := buf.String(), "GET"; got != want {
176+
t.Errorf("got wrong body %q, want %q", got, want)
177+
}
178+
if resp.StatusCode != http.StatusOK {
179+
t.Errorf("response status code isn't 200 OK: %v", resp.StatusCode)
180+
}
181+
if resp.Header.Get(XFromCache) != "" {
182+
t.Errorf("XFromCache header isn't blank")
183+
}
184+
}
185+
}
186+
122187
func TestGetOnlyIfCachedHit(t *testing.T) {
123188
resetTest()
124189
{

0 commit comments

Comments
 (0)