-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimage_test.go
55 lines (40 loc) · 1.24 KB
/
image_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
package main
import (
"image"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWrite(t *testing.T) {
assert := assert.New(t)
w := httptest.NewRecorder()
file, _ := os.Open("test/images/gopher.png")
img, _ := newImg(file)
img.write(w)
assert.Equal(w.Code, 200, "status should be ok")
assert.Equal(w.HeaderMap.Get("Content-Type"), "image/jpeg")
file, _ = os.Open("test/images/gopher.png")
fileImage, _, _ := image.Decode(file)
bodyImage, _, _ := image.Decode(w.Body)
assert.Equal(bodyImage.Bounds(), fileImage.Bounds(), "images should be same size")
}
func TestThumbnail(t *testing.T) {
assert := assert.New(t)
file, _ := os.Open("test/images/gopher.png")
img, _ := newImg(file)
img.resize(100, 50)
bounds := img.image.Bounds()
assert.Equal(bounds.Dx(), 100, "width should be 100")
assert.Equal(bounds.Dy(), 50, "height should be 50")
}
func TestResize(t *testing.T) {
assert := assert.New(t)
file, _ := os.Open("test/images/gopher.png") // 250 x 340px
img, _ := newImg(file)
// Resize to half width. Height should become 170px
img.resize(125, 0)
bounds := img.image.Bounds()
assert.Equal(bounds.Dx(), 125, "width should be 125")
assert.Equal(bounds.Dy(), 170, "height should be 170")
}