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

Commit

Permalink
Add failing test case for storing uncacheable range request.
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 20, 2016
1 parent 37c2ad6 commit 01b2fc8
Showing 1 changed file with 127 additions and 0 deletions.
127 changes: 127 additions & 0 deletions httpcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ func setup() {
w.Write([]byte(r.Method))
}))

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 {
w.WriteHeader(http.StatusNotModified)
return
}
w.Header().Set("last-modified", lm)
if r.Header.Get("range") == "bytes=4-9" {
w.WriteHeader(http.StatusPartialContent)
w.Write([]byte(" text "))
return
}
w.Write([]byte("Some text content"))
}))

mux.HandleFunc("/nostore", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
}))
Expand Down Expand Up @@ -184,6 +199,118 @@ func TestCacheableMethod(t *testing.T) {
}
}

func TestDontStorePartialRangeInCache(t *testing.T) {
resetTest()
{
req, err := http.NewRequest("GET", s.server.URL+"/range", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("range", "bytes=4-9")
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(), " text "; got != want {
t.Errorf("got %q, want %q", got, want)
}
if resp.StatusCode != http.StatusPartialContent {
t.Errorf("response status code isn't 206 Partial Content: %v", resp.StatusCode)
}
}
{
req, err := http.NewRequest("GET", s.server.URL+"/range", 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(), "Some text content"; 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")
}
}
{
req, err := http.NewRequest("GET", s.server.URL+"/range", 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(), "Some text content"; 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) != "1" {
t.Errorf(`XFromCache header isn't "1": %v`, resp.Header.Get(XFromCache))
}
}
{
req, err := http.NewRequest("GET", s.server.URL+"/range", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("range", "bytes=4-9")
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(), " text "; got != want {
t.Errorf("got %q, want %q", got, want)
}
if resp.StatusCode != http.StatusPartialContent {
t.Errorf("response status code isn't 206 Partial Content: %v", resp.StatusCode)
}
}
}

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

0 comments on commit 01b2fc8

Please sign in to comment.