-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpbox_test.go
230 lines (185 loc) · 5.81 KB
/
httpbox_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"github.com/coocood/freecache"
"github.com/stretchr/testify/assert"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
const (
uniqImageSize = minSize + 2 // for avoiding caching during the tests, all other requests use minSize+1
)
var (
goodImageURL string
brokenImageURL string
)
// Copypasted main() for CLI parsing, init cache and
// webserver. This is setup for blackbox-like testing for the
// handlers.
func TestMain(m *testing.M) {
flag.StringVar(&hostPort, "listen-at", "localhost:8080", "listen for HTTP requests at host:port")
flag.Parse()
goodImageURL = "http://" + hostPort + "/static/l_hires.jpg"
brokenImageURL = "http://" + hostPort + "/static/nonjpeg.jpg"
cache = freecache.NewCache(cacheSize)
// Testing only handler with pictures samples:
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("testdata"))))
// Handlers that should be tested:
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
handleRootRequest(w, r)
})
http.HandleFunc("/resize", func(w http.ResponseWriter, r *http.Request) {
handleResizeRequest(w, r)
})
go func() { http.ListenAndServe(hostPort, nil) }()
time.Sleep(100 * time.Millisecond)
ret := m.Run()
os.Exit(ret)
}
func TestSampleJpegImageExists(t *testing.T) {
resp, err := http.Get(goodImageURL)
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestSampleNonJpegImageExists(t *testing.T) {
resp, err := http.Get(brokenImageURL)
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestGetNotExistedHandler(t *testing.T) {
resp, err := http.Get("http://" + hostPort + "/should-be-not-found")
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
}
func TestGetRoot(t *testing.T) {
resp, err := http.Get("http://" + hostPort)
if err != nil {
t.Error(err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Error(err)
}
assert.Contains(t, string(data), fmt.Sprintf("Sample Image Resizer ver. %s", version))
}
func TestPutResize_WrongMethod(t *testing.T) {
client := &http.Client{}
req, err := http.NewRequest("PUT", "http://"+hostPort+"/resize", nil)
if err != nil {
t.Error(err)
}
resp, err := client.Do(req)
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
}
func TestGetResize_NoArgs(t *testing.T) {
resp, err := http.Get("http://" + hostPort + "/resize")
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}
func TestGetResize_TotallyWrongArgs(t *testing.T) {
resp, err := http.Get("http://" + hostPort + "/resize?username=xxx&password=yyy")
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}
func TestGetResize_NoSizes(t *testing.T) {
resp, err := http.Get("http://" + hostPort + "/resize?url=xxx.jpeg")
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}
func TestGetResize_NoHeight(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=%s&width=%d", hostPort, goodImageURL, minSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}
func TestGetResize_NoWidth(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=%s&height=%d", hostPort, goodImageURL, minSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}
func TestGetResize_SourceImageDoesntExists(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=notexisted.jpeg&height=%d&width=%d", hostPort, minSize+1, minSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusFailedDependency, resp.StatusCode)
}
func TestGetResize_Ok(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=%s&width=%d&height=%d", hostPort, goodImageURL, minSize+1, minSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestGetResize_NonJpeg(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=%s&width=%d&height=%d", hostPort, brokenImageURL, minSize+1, minSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusFailedDependency, resp.StatusCode)
}
func TestGetResize_ExceedWidthLimit(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=%s&height=%d&width=%d", hostPort, goodImageURL, minSize+1, maxSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}
func TestGetResize_ExceedHeightLimit(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=%s&height=%d&width=%d", hostPort, goodImageURL, maxSize+1, minSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
}
func TestGetResize_KeepRatioByWidth(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=%s&width=0&height=%d", hostPort, goodImageURL, minSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestGetResize_KeepRatioByHeight(t *testing.T) {
resp, err := http.Get(fmt.Sprintf("http://%s/resize?url=%s&width=%d&height=0", hostPort, goodImageURL, minSize+1))
if err != nil {
t.Error(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestGetResize_CheckForEtag(t *testing.T) {
fullreq := fmt.Sprintf("http://%s/resize?url=%s&width=%d&height=0", hostPort, goodImageURL, uniqImageSize)
resp, err := http.Get(fullreq)
if err != nil {
t.Error(err)
}
w := httptest.NewRecorder()
ok := useClientCache(w, resp.Request)
writtenResp := w.Result()
assert.False(t, ok)
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, resp.Header.Get("Etag"), writtenResp.Header.Get("Etag"))
}