mirror of
https://github.com/willnorris/imageproxy.git
synced 2026-04-25 04:46:24 +02:00
small cleanup of check304 and add more tests
Remove unused ResponseWriter parameter from check304, add function docs, and add TODO for alternate Etag header values that we should handle. Add tests for Proxy.allowed and check304.
This commit is contained in:
parent
e0a9588607
commit
5a4e602373
2 changed files with 123 additions and 3 deletions
114
imageproxy_test.go
Normal file
114
imageproxy_test.go
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
package imageproxy
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestAllowed(t *testing.T) {
|
||||
p := &Proxy{
|
||||
Whitelist: []string{"a.test", "*.b.test", "*c.test"},
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
url string
|
||||
allowed bool
|
||||
}{
|
||||
{"http://a.test/image", true},
|
||||
{"http://x.a.test/image", false},
|
||||
|
||||
{"http://b.test/image", true},
|
||||
{"http://x.b.test/image", true},
|
||||
{"http://x.y.b.test/image", true},
|
||||
|
||||
{"http://c.test/image", false},
|
||||
{"http://xc.test/image", false},
|
||||
{"/image", false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
u, err := url.Parse(tt.url)
|
||||
if err != nil {
|
||||
t.Errorf("error parsing url %q: %v", tt.url, err)
|
||||
}
|
||||
if got, want := p.allowed(u), tt.allowed; got != want {
|
||||
t.Errorf("allowed(%q) returned %v, want %v", u, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheck304(t *testing.T) {
|
||||
tests := []struct {
|
||||
req, resp string
|
||||
is304 bool
|
||||
}{
|
||||
{ // etag match
|
||||
"GET / HTTP/1.1\nIf-None-Match: \"v\"\n\n",
|
||||
"HTTP/1.1 200 OK\nEtag: \"v\"\n\n",
|
||||
true,
|
||||
},
|
||||
{ // last-modified match
|
||||
"GET / HTTP/1.1\nIf-Modified-Since: Sun, 02 Jan 2000 00:00:00 GMT\n\n",
|
||||
"HTTP/1.1 200 OK\nLast-Modified: Sat, 01 Jan 2000 00:00:00 GMT\n\n",
|
||||
true,
|
||||
},
|
||||
|
||||
// mismatches
|
||||
{
|
||||
"GET / HTTP/1.1\n\n",
|
||||
"HTTP/1.1 200 OK\n\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"GET / HTTP/1.1\n\n",
|
||||
"HTTP/1.1 200 OK\nEtag: \"v\"\n\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"GET / HTTP/1.1\nIf-None-Match: \"v\"\n\n",
|
||||
"HTTP/1.1 200 OK\n\n",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"GET / HTTP/1.1\nIf-None-Match: \"a\"\n\n",
|
||||
"HTTP/1.1 200 OK\nEtag: \"b\"\n\n",
|
||||
false,
|
||||
},
|
||||
{ // last-modified match
|
||||
"GET / HTTP/1.1\n\n",
|
||||
"HTTP/1.1 200 OK\nLast-Modified: Sat, 01 Jan 2000 00:00:00 GMT\n\n",
|
||||
false,
|
||||
},
|
||||
{ // last-modified match
|
||||
"GET / HTTP/1.1\nIf-Modified-Since: Sun, 02 Jan 2000 00:00:00 GMT\n\n",
|
||||
"HTTP/1.1 200 OK\n\n",
|
||||
false,
|
||||
},
|
||||
{ // last-modified match
|
||||
"GET / HTTP/1.1\nIf-Modified-Since: Fri, 31 Dec 1999 00:00:00 GMT\n\n",
|
||||
"HTTP/1.1 200 OK\nLast-Modified: Sat, 01 Jan 2000 00:00:00 GMT\n\n",
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
buf := bufio.NewReader(strings.NewReader(tt.req))
|
||||
req, err := http.ReadRequest(buf)
|
||||
if err != nil {
|
||||
t.Errorf("http.ReadRequest(%q) returned error: %v", tt.req, err)
|
||||
}
|
||||
|
||||
buf = bufio.NewReader(strings.NewReader(tt.resp))
|
||||
resp, err := http.ReadResponse(buf, req)
|
||||
if err != nil {
|
||||
t.Errorf("http.ReadResponse(%q) returned error: %v", tt.resp, err)
|
||||
}
|
||||
|
||||
if got, want := check304(req, resp), tt.is304; got != want {
|
||||
t.Errorf("check304(%q, %q) returned: %v, want %v", tt.req, tt.resp, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue