0
Fork 0
mirror of https://github.com/willnorris/imageproxy.git synced 2024-12-30 22:34:18 -05:00

add miscellaneous tests

also fix minor bug in detecting content type for content less than 512
bytes.
This commit is contained in:
Will Norris 2019-06-11 14:02:44 -07:00
parent 6975320eb4
commit a7a8966289
4 changed files with 33 additions and 3 deletions

View file

@ -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",
},
}

View file

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

View file

@ -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 {

View file

@ -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) {