From 0791f8ceecfd26843439682085cd7fea5919c843 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 22 Mar 2019 08:46:34 +0000 Subject: [PATCH] add more tests: mostly trivial checks --- cache_test.go | 4 ++++ data_test.go | 19 ++++++++++++++++++- imageproxy_test.go | 26 ++++++++++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/cache_test.go b/cache_test.go index 9b9ac3f..51515f3 100644 --- a/cache_test.go +++ b/cache_test.go @@ -24,4 +24,8 @@ func TestNopCache(t *testing.T) { if ok != false { t.Errorf("NopCache.Get returned ok = true, should always be false.") } + + // nothing to test on these methods other than to verify they exist + NopCache.Set("", []byte{}) + NopCache.Delete("") } diff --git a/data_test.go b/data_test.go index 035dca6..1416613 100644 --- a/data_test.go +++ b/data_test.go @@ -16,6 +16,7 @@ package imageproxy import ( "net/http" + "net/url" "testing" ) @@ -98,7 +99,7 @@ func TestParseOptions(t *testing.T) { {"r90,fh,sc0ffee,png,q90,1x2,fv,fit", Options{1, 2, true, 90, true, true, 90, "c0ffee", false, "png", 0, 0, 0, 0, false}}, // all flags, in different orders with crop - {"q70,cx100,cw300,1x2,fit,cy200,r90,fv,ch400,fh,sc0ffee,png", Options{1, 2, true, 90, true, true, 70, "c0ffee", false, "png", 100, 200, 300, 400, false}}, + {"q70,cx100,cw300,1x2,fit,cy200,r90,fv,ch400,fh,sc0ffee,png,sc,scaleUp", Options{1, 2, true, 90, true, true, 70, "c0ffee", true, "png", 100, 200, 300, 400, true}}, {"ch400,r90,cw300,fh,sc0ffee,png,cx100,q90,cy200,1x2,fv,fit", Options{1, 2, true, 90, true, true, 90, "c0ffee", false, "png", 100, 200, 300, 400, false}}, // all flags, in different orders with crop & different resizes @@ -204,3 +205,19 @@ func TestNewRequest(t *testing.T) { } } } + +func TestNewRequest_BaseURL(t *testing.T) { + req, _ := http.NewRequest("GET", "/x/path", nil) + base, _ := url.Parse("https://example.com/") + + r, err := NewRequest(req, base) + if err != nil { + t.Errorf("NewRequest(%v, %v) returned unexpected error: %v", req, base, err) + } + + want := "https://example.com/path#0x0" + if got := r.String(); got != want { + t.Errorf("NewRequest(%v, %v) returned %q, want %q", req, base, got, want) + } + +} diff --git a/imageproxy_test.go b/imageproxy_test.go index 46fad73..a10a92f 100644 --- a/imageproxy_test.go +++ b/imageproxy_test.go @@ -195,6 +195,28 @@ func TestHostMatches(t *testing.T) { } } +func TestReferrerMatches(t *testing.T) { + hosts := []string{"a.test"} + + tests := []struct { + referrer string + valid bool + }{ + {"", false}, + {"%", false}, + {"http://a.test/", true}, + {"http://b.test/", false}, + } + + for _, tt := range tests { + r, _ := http.NewRequest("GET", "/", nil) + r.Header.Set("Referer", tt.referrer) + if got, want := referrerMatches(hosts, r), tt.valid; got != want { + t.Errorf("referrerMatches(%v, %v) returned %v, want %v", hosts, r, got, want) + } + } +} + func TestValidSignature(t *testing.T) { key := []byte("c0ffee") @@ -348,6 +370,10 @@ func TestProxy_ServeHTTP(t *testing.T) { {"/http://good.test/nocontent", http.StatusNoContent}, // non-OK response {"/100/http://good.test/png", http.StatusOK}, {"/100/http://good.test/plain", http.StatusForbidden}, // non-image response + + // health-check URLs + {"/", http.StatusOK}, + {"/health-check", http.StatusOK}, } for _, tt := range tests {