|
|
|
|
@ -1,60 +1,11 @@
|
|
|
|
|
package http |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"net/http" |
|
|
|
|
"net/url" |
|
|
|
|
"reflect" |
|
|
|
|
"strings" |
|
|
|
|
"testing" |
|
|
|
|
"time" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
func TestCacheControlMaxAgeSuccess(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
hdr string |
|
|
|
|
wantAge time.Duration |
|
|
|
|
wantOK bool |
|
|
|
|
}{ |
|
|
|
|
{"max-age=12", 12 * time.Second, true}, |
|
|
|
|
{"max-age=-12", 0, false}, |
|
|
|
|
{"max-age=0", 0, false}, |
|
|
|
|
{"public, max-age=12", 12 * time.Second, true}, |
|
|
|
|
{"public, max-age=40192, must-revalidate", 40192 * time.Second, true}, |
|
|
|
|
{"public, not-max-age=12, must-revalidate", time.Duration(0), false}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, tt := range tests { |
|
|
|
|
maxAge, ok, err := cacheControlMaxAge(tt.hdr) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Errorf("case %d: err=%v", i, err) |
|
|
|
|
} |
|
|
|
|
if tt.wantAge != maxAge { |
|
|
|
|
t.Errorf("case %d: want=%d got=%d", i, tt.wantAge, maxAge) |
|
|
|
|
} |
|
|
|
|
if tt.wantOK != ok { |
|
|
|
|
t.Errorf("case %d: incorrect ok value: want=%t got=%t", i, tt.wantOK, ok) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCacheControlMaxAgeFail(t *testing.T) { |
|
|
|
|
tests := []string{ |
|
|
|
|
"max-age=aasdf", |
|
|
|
|
"max-age=", |
|
|
|
|
"max-age", |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, tt := range tests { |
|
|
|
|
_, ok, err := cacheControlMaxAge(tt) |
|
|
|
|
if ok { |
|
|
|
|
t.Errorf("case %d: want ok=false, got true", i) |
|
|
|
|
} |
|
|
|
|
if err == nil { |
|
|
|
|
t.Errorf("case %d: want non-nil err", i) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestMergeQuery(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
u string |
|
|
|
|
@ -134,176 +85,6 @@ func TestMergeQuery(t *testing.T) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestExpiresPass(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
date string |
|
|
|
|
exp string |
|
|
|
|
wantTTL time.Duration |
|
|
|
|
wantOK bool |
|
|
|
|
}{ |
|
|
|
|
// Expires and Date properly set
|
|
|
|
|
{ |
|
|
|
|
date: "Thu, 01 Dec 1983 22:00:00 GMT", |
|
|
|
|
exp: "Fri, 02 Dec 1983 01:00:00 GMT", |
|
|
|
|
wantTTL: 10800 * time.Second, |
|
|
|
|
wantOK: true, |
|
|
|
|
}, |
|
|
|
|
// empty headers
|
|
|
|
|
{ |
|
|
|
|
date: "", |
|
|
|
|
exp: "", |
|
|
|
|
wantOK: false, |
|
|
|
|
}, |
|
|
|
|
// lack of Expirs short-ciruits Date parsing
|
|
|
|
|
{ |
|
|
|
|
date: "foo", |
|
|
|
|
exp: "", |
|
|
|
|
wantOK: false, |
|
|
|
|
}, |
|
|
|
|
// lack of Date short-ciruits Expires parsing
|
|
|
|
|
{ |
|
|
|
|
date: "", |
|
|
|
|
exp: "foo", |
|
|
|
|
wantOK: false, |
|
|
|
|
}, |
|
|
|
|
// no Date
|
|
|
|
|
{ |
|
|
|
|
exp: "Thu, 01 Dec 1983 22:00:00 GMT", |
|
|
|
|
wantTTL: 0, |
|
|
|
|
wantOK: false, |
|
|
|
|
}, |
|
|
|
|
// no Expires
|
|
|
|
|
{ |
|
|
|
|
date: "Thu, 01 Dec 1983 22:00:00 GMT", |
|
|
|
|
wantTTL: 0, |
|
|
|
|
wantOK: false, |
|
|
|
|
}, |
|
|
|
|
// Expires < Date
|
|
|
|
|
{ |
|
|
|
|
date: "Fri, 02 Dec 1983 01:00:00 GMT", |
|
|
|
|
exp: "Thu, 01 Dec 1983 22:00:00 GMT", |
|
|
|
|
wantTTL: 0, |
|
|
|
|
wantOK: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, tt := range tests { |
|
|
|
|
ttl, ok, err := expires(tt.date, tt.exp) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Errorf("case %d: err=%v", i, err) |
|
|
|
|
} |
|
|
|
|
if tt.wantTTL != ttl { |
|
|
|
|
t.Errorf("case %d: want=%d got=%d", i, tt.wantTTL, ttl) |
|
|
|
|
} |
|
|
|
|
if tt.wantOK != ok { |
|
|
|
|
t.Errorf("case %d: incorrect ok value: want=%t got=%t", i, tt.wantOK, ok) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestExpiresFail(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
date string |
|
|
|
|
exp string |
|
|
|
|
}{ |
|
|
|
|
// malformed Date header
|
|
|
|
|
{ |
|
|
|
|
date: "foo", |
|
|
|
|
exp: "Fri, 02 Dec 1983 01:00:00 GMT", |
|
|
|
|
}, |
|
|
|
|
// malformed exp header
|
|
|
|
|
{ |
|
|
|
|
date: "Fri, 02 Dec 1983 01:00:00 GMT", |
|
|
|
|
exp: "bar", |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, tt := range tests { |
|
|
|
|
_, _, err := expires(tt.date, tt.exp) |
|
|
|
|
if err == nil { |
|
|
|
|
t.Errorf("case %d: expected non-nil error", i) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCacheablePass(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
headers http.Header |
|
|
|
|
wantTTL time.Duration |
|
|
|
|
wantOK bool |
|
|
|
|
}{ |
|
|
|
|
// valid Cache-Control
|
|
|
|
|
{ |
|
|
|
|
headers: http.Header{ |
|
|
|
|
"Cache-Control": []string{"max-age=100"}, |
|
|
|
|
}, |
|
|
|
|
wantTTL: 100 * time.Second, |
|
|
|
|
wantOK: true, |
|
|
|
|
}, |
|
|
|
|
// valid Date/Expires
|
|
|
|
|
{ |
|
|
|
|
headers: http.Header{ |
|
|
|
|
"Date": []string{"Thu, 01 Dec 1983 22:00:00 GMT"}, |
|
|
|
|
"Expires": []string{"Fri, 02 Dec 1983 01:00:00 GMT"}, |
|
|
|
|
}, |
|
|
|
|
wantTTL: 10800 * time.Second, |
|
|
|
|
wantOK: true, |
|
|
|
|
}, |
|
|
|
|
// Cache-Control supersedes Date/Expires
|
|
|
|
|
{ |
|
|
|
|
headers: http.Header{ |
|
|
|
|
"Cache-Control": []string{"max-age=100"}, |
|
|
|
|
"Date": []string{"Thu, 01 Dec 1983 22:00:00 GMT"}, |
|
|
|
|
"Expires": []string{"Fri, 02 Dec 1983 01:00:00 GMT"}, |
|
|
|
|
}, |
|
|
|
|
wantTTL: 100 * time.Second, |
|
|
|
|
wantOK: true, |
|
|
|
|
}, |
|
|
|
|
// no caching headers
|
|
|
|
|
{ |
|
|
|
|
headers: http.Header{}, |
|
|
|
|
wantOK: false, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, tt := range tests { |
|
|
|
|
ttl, ok, err := Cacheable(tt.headers) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Errorf("case %d: err=%v", i, err) |
|
|
|
|
continue |
|
|
|
|
} |
|
|
|
|
if tt.wantTTL != ttl { |
|
|
|
|
t.Errorf("case %d: want=%d got=%d", i, tt.wantTTL, ttl) |
|
|
|
|
} |
|
|
|
|
if tt.wantOK != ok { |
|
|
|
|
t.Errorf("case %d: incorrect ok value: want=%t got=%t", i, tt.wantOK, ok) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCacheableFail(t *testing.T) { |
|
|
|
|
tests := []http.Header{ |
|
|
|
|
// invalid Cache-Control short-circuits
|
|
|
|
|
http.Header{ |
|
|
|
|
"Cache-Control": []string{"max-age"}, |
|
|
|
|
"Date": []string{"Thu, 01 Dec 1983 22:00:00 GMT"}, |
|
|
|
|
"Expires": []string{"Fri, 02 Dec 1983 01:00:00 GMT"}, |
|
|
|
|
}, |
|
|
|
|
// no Cache-Control, invalid Expires
|
|
|
|
|
http.Header{ |
|
|
|
|
"Date": []string{"Thu, 01 Dec 1983 22:00:00 GMT"}, |
|
|
|
|
"Expires": []string{"boo"}, |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for i, tt := range tests { |
|
|
|
|
_, _, err := Cacheable(tt) |
|
|
|
|
if err == nil { |
|
|
|
|
t.Errorf("case %d: want non-nil err", i) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestNewResourceLocation(t *testing.T) { |
|
|
|
|
tests := []struct { |
|
|
|
|
ru *url.URL |
|
|
|
|
@ -366,15 +147,3 @@ func TestNewResourceLocation(t *testing.T) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestCopyRequest(t *testing.T) { |
|
|
|
|
r1, err := http.NewRequest("GET", "http://example.com", strings.NewReader("foo")) |
|
|
|
|
if err != nil { |
|
|
|
|
t.Fatalf("Unexpected error: %v", err) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
r2 := CopyRequest(r1) |
|
|
|
|
if !reflect.DeepEqual(r1, r2) { |
|
|
|
|
t.Fatalf("Result of CopyRequest incorrect: %#v != %#v", r1, r2) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|