From a7a896628929c5778d6cb0c66b2b1261709c6238 Mon Sep 17 00:00:00 2001 From: Will Norris Date: Tue, 11 Jun 2019 14:02:44 -0700 Subject: [PATCH] add miscellaneous tests also fix minor bug in detecting content type for content less than 512 bytes. --- data_test.go | 4 ++-- imageproxy.go | 2 +- imageproxy_test.go | 18 ++++++++++++++++++ transform_test.go | 12 ++++++++++++ 4 files changed, 33 insertions(+), 3 deletions(-) diff --git a/data_test.go b/data_test.go index 9e839c7cf..6e4cda9e8 100644 --- a/data_test.go +++ b/data_test.go @@ -44,8 +44,8 @@ func TestOptions_String(t *testing.T) { "0.15x1.3,cx100,cy200,q95,r45,sc0ffee", }, { - Options{0.15, 1.3, false, 45, false, false, 95, "c0ffee", false, "png", 100, 200, 300, 400, false}, - "0.15x1.3,ch400,cw300,cx100,cy200,png,q95,r45,sc0ffee", + Options{0.15, 1.3, false, 45, false, false, 95, "c0ffee", true, "png", 100, 200, 300, 400, true}, + "0.15x1.3,ch400,cw300,cx100,cy200,png,q95,r45,sc,sc0ffee,scaleUp", }, } diff --git a/imageproxy.go b/imageproxy.go index 476763698..1b9a528fe 100644 --- a/imageproxy.go +++ b/imageproxy.go @@ -215,7 +215,7 @@ func (p *Proxy) serveImage(w http.ResponseWriter, r *http.Request) { // the content type. Returns empty string if error occurs. func peekContentType(p *bufio.Reader) string { byt, err := p.Peek(512) - if err != nil && err != bufio.ErrBufferFull { + if err != nil && err != bufio.ErrBufferFull && err != io.EOF { return "" } return http.DetectContentType(byt) diff --git a/imageproxy_test.go b/imageproxy_test.go index de31ffb85..4405b2876 100644 --- a/imageproxy_test.go +++ b/imageproxy_test.go @@ -17,6 +17,7 @@ package imageproxy import ( "bufio" "bytes" + "encoding/base64" "errors" "fmt" "image" @@ -31,6 +32,21 @@ import ( "testing" ) +func TestPeekContentType(t *testing.T) { + // 1 pixel png image, base64 encoded + b, _ := base64.StdEncoding.DecodeString("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAEUlEQVR4nGJiYGBgAAQAAP//AA8AA/6P688AAAAASUVORK5CYII=") + got := peekContentType(bufio.NewReader(bytes.NewReader(b))) + if want := "image/png"; got != want { + t.Errorf("peekContentType returned %v, want %v", got, want) + } + + // single zero byte + got = peekContentType(bufio.NewReader(bytes.NewReader([]byte{0x0}))) + if want := "application/octet-stream"; got != want { + t.Errorf("peekContentType returned %v, want %v", got, want) + } +} + func TestCopyHeader(t *testing.T) { tests := []struct { dst, src http.Header @@ -234,6 +250,8 @@ func TestValidSignature(t *testing.T) { {"http://test/image", Options{Signature: "NDx5zZHx7QfE8E-ijowRreq6CJJBZjwiRfOVk_mkfQQ", Rotate: 90}, true}, // signature calculated from url plus options {"http://test/image", Options{Signature: "ZGTzEm32o4iZ7qcChls3EVYaWyrDd9u0etySo0-WkF8=", Rotate: 90}, true}, + // invalid base64 encoded signature + {"http://test/image", Options{Signature: "!!"}, false}, } for _, tt := range tests { diff --git a/transform_test.go b/transform_test.go index f7555a9a5..8f6beeb95 100644 --- a/transform_test.go +++ b/transform_test.go @@ -91,6 +91,7 @@ func TestCropParams(t *testing.T) { {Options{CropX: 50, CropY: 100, CropWidth: 100, CropHeight: 150}, 50, 100, 64, 128}, {Options{CropX: -50, CropY: -50}, 14, 78, 64, 128}, {Options{CropY: 0.5, CropWidth: 0.5}, 0, 64, 32, 128}, + {Options{Width: 10, Height: 10, SmartCrop: true}, 0, 0, 64, 64}, } for _, tt := range tests { want := image.Rect(tt.x0, tt.y0, tt.x1, tt.y1) @@ -148,6 +149,17 @@ func TestTransform(t *testing.T) { } } +func TestTransform_InvalidFormat(t *testing.T) { + src := newImage(2, 2, red, green, blue, yellow) + buf := new(bytes.Buffer) + png.Encode(buf, src) + + _, err := Transform(buf.Bytes(), Options{Format: "invalid"}) + if err == nil { + t.Errorf("Transform with invalid format did not return expected error") + } +} + // Test that each of the eight EXIF orientations is applied to the transformed // image appropriately. func TestTransform_EXIF(t *testing.T) {