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) } } }