/
handlers_test.go
96 lines (78 loc) · 2.55 KB
/
handlers_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
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
assert "github.com/pilu/miniassert"
)
func init() {
logger.enabled = false
}
func TestMainHandler_Favicon(t *testing.T) {
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/favicon.ico", nil)
MainHandler(recorder, request)
assert.Equal(t, http.StatusNotFound, recorder.Code)
}
func TestMainHandler_OriginalFileExists(t *testing.T) {
recorder := httptest.NewRecorder()
InitSettings()
settings["PublicPath"] = "./test_fixtures"
request, _ := http.NewRequest("GET", "/test.png", nil)
MainHandler(recorder, request)
assert.Equal(t, http.StatusOK, recorder.Code)
}
func TestMainHandler_ThumbExists(t *testing.T) {
recorder := httptest.NewRecorder()
InitSettings()
settings["PublicPath"] = "./test_fixtures"
request, _ := http.NewRequest("GET", "/test-50x50.png", nil)
MainHandler(recorder, request)
assert.Equal(t, http.StatusOK, recorder.Code)
}
func TestMainHandler_GenerateThumb(t *testing.T) {
InitSettings()
fixturesPath := "./test_fixtures"
settings["PublicPath"] = fixturesPath
fileName := "test-100x100.png"
filePath := path.Join(fixturesPath, fileName)
// we don't have a thumb yet
_, err := os.Stat(filePath)
assert.NotNil(t, err)
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", fmt.Sprintf("/%s", fileName), nil)
MainHandler(recorder, request)
// check if the thumb has been generated
assert.Equal(t, http.StatusOK, recorder.Code)
assert.Equal(t, "image/jpeg", recorder.HeaderMap.Get("Content-Type"))
}
func TestMainHandler_BadGeometry(t *testing.T) {
InitSettings()
settings["PublicPath"] = "./test_fixtures"
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "test-axb.png", nil)
MainHandler(recorder, request)
assert.Equal(t, http.StatusBadRequest, recorder.Code)
}
func TestMainHandler_MainFileNotFound(t *testing.T) {
InitSettings()
settings["PublicPath"] = "./test_fixtures"
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "foo-100x100.png", nil)
MainHandler(recorder, request)
assert.Equal(t, http.StatusNotFound, recorder.Code)
}
func TestMainHandler_InvalidGeomtries(t *testing.T) {
InitSettings()
settings["PublicPath"] = "./test_fixtures"
validGeometries = map[string]bool{"200x200": true}
// only 200x200 is allowed
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "test-10x10.png", nil)
MainHandler(recorder, request)
assert.Equal(t, http.StatusBadRequest, recorder.Code)
validGeometries = map[string]bool{"*": true}
}