Skip to content
Merged
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
47 changes: 47 additions & 0 deletions internal/cache_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -165,6 +166,52 @@ func TestCacheHandler_vary_header(t *testing.T) {
assert.Equal(t, "hit", resp.Header().Get("X-Cache"))
}

func TestCacheHandler_different_hosts(t *testing.T) {
cache := newTestCache()
handler := NewCacheHandler(cache, 1024, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
host := r.Header.Get("Host")
w.Header().Set("Cache-Control", "public, max-age=600")
w.Write([]byte(host))
}))

doReq := func(url string) *httptest.ResponseRecorder {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", url, nil)
host := strings.Split(url, "://")[1]
r.Header.Set("Host", host)
handler.ServeHTTP(w, r)
return w
}

resp := doReq("https://example.com")
assert.Equal(t, "example.com", resp.Body.String())
assert.Equal(t, "miss", resp.Header().Get("X-Cache"))

resp = doReq("https://example.com")
assert.Equal(t, "example.com", resp.Body.String())
assert.Equal(t, "hit", resp.Header().Get("X-Cache"))

resp = doReq("https://another.com")
assert.Equal(t, "another.com", resp.Body.String())
assert.Equal(t, "miss", resp.Header().Get("X-Cache"))

resp = doReq("https://another.com")
assert.Equal(t, "another.com", resp.Body.String())
assert.Equal(t, "hit", resp.Header().Get("X-Cache"))

resp = doReq("https://example.com/test")
assert.Equal(t, "example.com/test", resp.Body.String())
assert.Equal(t, "miss", resp.Header().Get("X-Cache"))

resp = doReq("https://another.com/test")
assert.Equal(t, "another.com/test", resp.Body.String())
assert.Equal(t, "miss", resp.Header().Get("X-Cache"))

resp = doReq("https://another.com/test")
assert.Equal(t, "another.com/test", resp.Body.String())
assert.Equal(t, "hit", resp.Header().Get("X-Cache"))
}

func TestCacheHandler_range_requests_are_not_cached(t *testing.T) {
cache := newTestCache()

Expand Down
1 change: 1 addition & 0 deletions internal/variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (v *Variant) CacheKey() CacheKey {
hash.Write([]byte(v.r.Method))
hash.Write([]byte(v.r.URL.Path))
hash.Write([]byte(v.r.URL.Query().Encode()))
hash.Write([]byte(v.r.Host))

for _, name := range v.headerNames {
hash.Write([]byte(name + "=" + v.r.Header.Get(name)))
Expand Down