-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler_test.go
106 lines (83 loc) · 1.92 KB
/
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
package main
import (
"image"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
var testUrl1 = "http://localhost/foo/sdf/sdflkj/abc.png"
var testUrl2 = "http://localhost/foo/bar/Picture2.jpg"
func TestHandlePOST(t *testing.T) {
picture1, err := os.Open(filepath.Join(filepath.Dir(ConfigFileUrl), "testdata", "picture1.png"))
if err != nil {
log.Panicln(err)
}
req, err := http.NewRequest("POST", testUrl1, picture1)
if err != nil {
log.Fatal(err)
}
w := httptest.NewRecorder()
hander := &ImgHandler{}
hander.ServeHTTP(w, req)
}
func TestHandleGET(t *testing.T) {
req, err := http.NewRequest("GET", testUrl1, nil)
if err != nil {
log.Fatal(err)
}
w := httptest.NewRecorder()
hander := &ImgHandler{}
hander.ServeHTTP(w, req)
if w.Body.Len() == 0 {
t.Fail()
}
img, _, err := image.Decode(w.Body)
if err != nil {
log.Fatalln(err)
}
if img.Bounds().Dx() != 655 {
t.Fail()
}
}
func TestUploadJPG(t *testing.T) {
picture2, err := os.Open(filepath.Join(filepath.Dir(ConfigFileUrl), "testdata", "picture2.jpg"))
if err != nil {
log.Panicln(err)
}
req, err := http.NewRequest("POST", testUrl2, picture2)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "image/jpeg")
w := httptest.NewRecorder()
hander := &ImgHandler{}
hander.ServeHTTP(w, req)
}
func TestGetJPG(t *testing.T) {
ext := strings.ToLower(filepath.Ext(testUrl2))
basename := strings.TrimSuffix(testUrl2, ext)
req, err := http.NewRequest("GET", basename+"__345x123"+ext, nil)
if err != nil {
log.Fatal(err)
}
w := httptest.NewRecorder()
hander := &ImgHandler{}
hander.ServeHTTP(w, req)
if w.Body.Len() == 0 {
t.Fail()
}
img, _, err := image.Decode(w.Body)
if err != nil {
log.Fatalln(err)
}
if img.Bounds().Dx() != 345 {
t.Errorf("expected %v got %v", 345, img.Bounds().Dx())
}
if img.Bounds().Dy() != 123 {
t.Errorf("expected %v got %v", 123, img.Bounds().Dy())
}
}