Skip to content

Commit

Permalink
add miscellaneous tests
Browse files Browse the repository at this point in the history
also fix minor bug in detecting content type for content less than 512
bytes.
  • Loading branch information
willnorris committed Jun 11, 2019
1 parent 6975320 commit a7a8966
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
}

Expand Down
2 changes: 1 addition & 1 deletion imageproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions imageproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package imageproxy
import (
"bufio"
"bytes"
"encoding/base64"
"errors"
"fmt"
"image"
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions transform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit a7a8966

Please sign in to comment.