|
4 | 4 | "fmt"
|
5 | 5 | "net/http"
|
6 | 6 | "net/http/httptest"
|
| 7 | + "strings" |
7 | 8 | "testing"
|
8 | 9 | "time"
|
9 | 10 |
|
@@ -165,6 +166,52 @@ func TestCacheHandler_vary_header(t *testing.T) {
|
165 | 166 | assert.Equal(t, "hit", resp.Header().Get("X-Cache"))
|
166 | 167 | }
|
167 | 168 |
|
| 169 | +func TestCacheHandler_different_hosts(t *testing.T) { |
| 170 | + cache := newTestCache() |
| 171 | + handler := NewCacheHandler(cache, 1024, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 172 | + host := r.Header.Get("Host") |
| 173 | + w.Header().Set("Cache-Control", "public, max-age=600") |
| 174 | + w.Write([]byte(host)) |
| 175 | + })) |
| 176 | + |
| 177 | + doReq := func(url string) *httptest.ResponseRecorder { |
| 178 | + w := httptest.NewRecorder() |
| 179 | + r := httptest.NewRequest("GET", url, nil) |
| 180 | + host := strings.Split(url, "://")[1] |
| 181 | + r.Header.Set("Host", host) |
| 182 | + handler.ServeHTTP(w, r) |
| 183 | + return w |
| 184 | + } |
| 185 | + |
| 186 | + resp := doReq("https://example.com") |
| 187 | + assert.Equal(t, "example.com", resp.Body.String()) |
| 188 | + assert.Equal(t, "miss", resp.Header().Get("X-Cache")) |
| 189 | + |
| 190 | + resp = doReq("https://example.com") |
| 191 | + assert.Equal(t, "example.com", resp.Body.String()) |
| 192 | + assert.Equal(t, "hit", resp.Header().Get("X-Cache")) |
| 193 | + |
| 194 | + resp = doReq("https://another.com") |
| 195 | + assert.Equal(t, "another.com", resp.Body.String()) |
| 196 | + assert.Equal(t, "miss", resp.Header().Get("X-Cache")) |
| 197 | + |
| 198 | + resp = doReq("https://another.com") |
| 199 | + assert.Equal(t, "another.com", resp.Body.String()) |
| 200 | + assert.Equal(t, "hit", resp.Header().Get("X-Cache")) |
| 201 | + |
| 202 | + resp = doReq("https://example.com/test") |
| 203 | + assert.Equal(t, "example.com/test", resp.Body.String()) |
| 204 | + assert.Equal(t, "miss", resp.Header().Get("X-Cache")) |
| 205 | + |
| 206 | + resp = doReq("https://another.com/test") |
| 207 | + assert.Equal(t, "another.com/test", resp.Body.String()) |
| 208 | + assert.Equal(t, "miss", resp.Header().Get("X-Cache")) |
| 209 | + |
| 210 | + resp = doReq("https://another.com/test") |
| 211 | + assert.Equal(t, "another.com/test", resp.Body.String()) |
| 212 | + assert.Equal(t, "hit", resp.Header().Get("X-Cache")) |
| 213 | +} |
| 214 | + |
168 | 215 | func TestCacheHandler_range_requests_are_not_cached(t *testing.T) {
|
169 | 216 | cache := newTestCache()
|
170 | 217 |
|
|
0 commit comments