-
Notifications
You must be signed in to change notification settings - Fork 15
/
image-handler_test.go
132 lines (104 loc) · 3.71 KB
/
image-handler_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"flag"
"fmt"
"github.com/die-net/fotomat/imager"
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"net"
"net/http"
"runtime"
"testing"
)
var localhost string
func init() {
// Initialize flags with default values, enable local serving.
flag.Parse()
*localImageDirectory = "."
poolInit(1)
runtime.GOMAXPROCS(2)
// Listen on an ephemeral localhost port.
listen, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
log.Fatal("Listen: ", err)
}
// Record that address.
localhost = listen.Addr().String()
go http.Serve(listen, nil)
}
func TestSuccess(t *testing.T) {
// Load a 2x3 pixel image.
assert.Nil(t, isSize("2px.png=s2048x2048", imager.Png, 2, 3))
// Crop JPEG to 200x100.
assert.Nil(t, isSize("watermelon.jpg=c200x100", imager.Jpeg, 200, 100))
// Scale preview JPEG.
assert.Nil(t, isSize("watermelon.jpg=ps100x100", imager.Jpeg, 75, 100))
// Crop 3000x2000 PNG to a small preview JPEG.
assert.Nil(t, isSize("3000px.png=pc16x16", imager.Jpeg, 16, 16))
}
func TestResponseErrors(t *testing.T) {
// Return StatusNotFound on a textfile that doesn't exist.
assert.Equal(t, status("notfound.txt=s16x16"), http.StatusNotFound)
// Return StatusUnsupportedMediaType on a text file.
assert.Equal(t, status("notimage.txt=s16x16"), http.StatusUnsupportedMediaType)
// Return StatusUnsupportedMediaType on a truncated image.
assert.Equal(t, status("bad.jpg=s16x16"), http.StatusUnsupportedMediaType)
// Return StatusUnsupportedMediaType on a 1x1 pixel image.
assert.Equal(t, status("1px.png=s16x16"), http.StatusUnsupportedMediaType)
// Return StatusRequestEntityTooLarge on a 34000px image.
assert.Equal(t, status("34000px.png=s16x16"), http.StatusRequestEntityTooLarge)
}
func TestParameterValidation(t *testing.T) {
// Test missing parameters.
assert.Equal(t, status("watermelon.jpg"), http.StatusBadRequest)
// Test bad operation.
assert.Equal(t, status("watermelon.jpg=z16x16"), http.StatusBadRequest)
// Require preview flag to be a prefix.
assert.Equal(t, status("watermelon.jpg=sp16x16"), http.StatusBadRequest)
// Test that both scale and crop refuse a 0px width or height.
assert.Equal(t, status("watermelon.jpg=s0x10"), http.StatusBadRequest)
assert.Equal(t, status("watermelon.jpg=s10x0"), http.StatusBadRequest)
assert.Equal(t, status("watermelon.jpg=c0x10"), http.StatusBadRequest)
assert.Equal(t, status("watermelon.jpg=c10x0"), http.StatusBadRequest)
// Test that both scale and crop refuse a 2049px width or height.
assert.Equal(t, status("watermelon.jpg=s2049x16"), http.StatusBadRequest)
assert.Equal(t, status("watermelon.jpg=s16x2049"), http.StatusBadRequest)
assert.Equal(t, status("watermelon.jpg=c2049x16"), http.StatusBadRequest)
assert.Equal(t, status("watermelon.jpg=c16x2049"), http.StatusBadRequest)
// Refuse repeated scale parameters.
assert.Equal(t, status("watermelon.jpg=s16x16=s16x16"), http.StatusBadRequest)
}
func isSize(filename string, format imager.Format, width, height int) error {
image, code := fetch(filename)
if code != 200 {
return fmt.Errorf("HTTP error %d", code)
}
m, err := imager.MetadataBytes(image)
if err != nil {
return err
}
if m.Width != width || m.Height != height {
return fmt.Errorf("Width %d!=%d or Height %d!=%d", m.Width, width, m.Height, height)
}
if m.Format != format {
return fmt.Errorf("Format %s!=%s", m.Format, format)
}
return nil
}
func status(filename string) int {
_, code := fetch(filename)
return code
}
func fetch(filename string) ([]byte, int) {
resp, err := http.Get("http://" + localhost + "/imager/testdata/" + filename)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return body, resp.StatusCode
}