-
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathrouter_test.go
395 lines (335 loc) · 13.8 KB
/
router_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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package handler
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"
"time"
"webp_server_go/config"
"webp_server_go/helper"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/etag"
"github.com/patrickmn/go-cache"
"github.com/stretchr/testify/assert"
)
var (
chromeUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"
safariUA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15"
safari17UA = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Safari/605.1.15" // <- Mac with Safari 17
curlUA = "curl/7.64.1"
acceptWebP = "image/webp,image/apng,image/*,*/*;q=0.8"
acceptAvif = "image/avif,image/*,*/*;q=0.8"
acceptLegacy = "image/jpeg,image/png"
)
func setupParam() {
// setup parameters here...
config.Config.ImgPath = "../pics"
config.Config.ExhaustPath = "../exhaust_test"
config.Config.AllowedTypes = []string{"jpg", "png", "jpeg", "bmp", "heic", "avif"}
config.Config.MetadataPath = "../metadata"
config.Config.RemoteRawPath = "../remote-raw"
config.ProxyMode = false
config.Config.EnableWebP = true
config.Config.EnableAVIF = false
config.Config.Quality = 80
config.Config.ImageMap = map[string]string{}
config.RemoteCache = cache.New(cache.NoExpiration, 10*time.Minute)
}
func requestToServer(reqUrl string, app *fiber.App, ua, accept string) (*http.Response, []byte) {
parsedUrl, _ := url.Parse(reqUrl)
req := httptest.NewRequest("GET", parsedUrl.EscapedPath(), nil)
req.Header.Set("User-Agent", ua)
req.Header.Set("Accept", accept)
req.Header.Set("Host", parsedUrl.Host)
req.Host = parsedUrl.Host
resp, err := app.Test(req, 120000)
if err != nil {
return nil, nil
}
data, _ := io.ReadAll(resp.Body)
return resp, data
}
func TestServerHeaders(t *testing.T) {
setupParam()
var app = fiber.New()
app.Use(etag.New(etag.Config{
Weak: true,
}))
app.Get("/*", Convert)
url := "http://127.0.0.1:3333/webp_server.bmp"
// test for chrome
response, _ := requestToServer(url, app, chromeUA, acceptWebP)
defer response.Body.Close()
ratio := response.Header.Get("X-Compression-Rate")
etag := response.Header.Get("Etag")
assert.NotEqual(t, "", ratio)
assert.NotEqual(t, "", etag)
// test for safari
response, _ = requestToServer(url, app, safariUA, acceptLegacy)
defer response.Body.Close()
// ratio = response.Header.Get("X-Compression-Rate")
etag = response.Header.Get("Etag")
assert.NotEqual(t, "", etag)
}
func TestConvertDuplicates(t *testing.T) {
setupParam()
N := 3
var testLink = map[string]string{
"http://127.0.0.1:3333/webp_server.jpg": "image/webp",
"http://127.0.0.1:3333/webp_server.bmp": "image/webp",
"http://127.0.0.1:3333/webp_server.png": "image/webp",
"http://127.0.0.1:3333/empty.jpg": "",
"http://127.0.0.1:3333/png.jpg": "image/webp",
"http://127.0.0.1:3333/12314.jpg": "",
"http://127.0.0.1:3333/dir1/inside.jpg": "image/webp",
"http://127.0.0.1:3333/%e5%a4%aa%e7%a5%9e%e5%95%a6.png": "image/webp",
"http://127.0.0.1:3333/太神啦.png": "image/webp",
}
var app = fiber.New()
app.Get("/*", Convert)
// test Chrome
for url, respType := range testLink {
for range N {
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
contentType := helper.GetContentType(data)
assert.Equal(t, respType, contentType)
}
}
}
func TestConvert(t *testing.T) {
setupParam()
// TODO: old-style test, better update it with accept headers
var testChromeLink = map[string]string{
"http://127.0.0.1:3333/webp_server.jpg": "image/webp",
"http://127.0.0.1:3333/webp_server.bmp": "image/webp",
"http://127.0.0.1:3333/webp_server.png": "image/webp",
"http://127.0.0.1:3333/empty.jpg": "",
"http://127.0.0.1:3333/png.jpg": "image/webp",
"http://127.0.0.1:3333/12314.jpg": "",
"http://127.0.0.1:3333/dir1/inside.jpg": "image/webp",
"http://127.0.0.1:3333/%e5%a4%aa%e7%a5%9e%e5%95%a6.png": "image/webp",
"http://127.0.0.1:3333/太神啦.png": "image/webp",
// Source: https://filesamples.com/formats/heic
"http://127.0.0.1:3333/sample3.heic": "image/webp", // webp because browser does not support heic
// Source: https://raw.githubusercontent.com/link-u/avif-sample-images/refs/heads/master/kimono.avif
"http://127.0.0.1:3333/kimono.avif": "image/webp", // webp because browser does not support avif
}
var testChromeAvifLink = map[string]string{
"http://127.0.0.1:3333/webp_server.jpg": "image/avif",
"http://127.0.0.1:3333/webp_server.bmp": "image/avif",
"http://127.0.0.1:3333/webp_server.png": "image/avif",
"http://127.0.0.1:3333/empty.jpg": "",
"http://127.0.0.1:3333/png.jpg": "image/avif",
"http://127.0.0.1:3333/12314.jpg": "",
"http://127.0.0.1:3333/dir1/inside.jpg": "image/avif",
"http://127.0.0.1:3333/%e5%a4%aa%e7%a5%9e%e5%95%a6.png": "image/avif",
"http://127.0.0.1:3333/太神啦.png": "image/avif",
}
var testSafariLink = map[string]string{
"http://127.0.0.1:3333/webp_server.jpg": "image/jpeg",
"http://127.0.0.1:3333/webp_server.bmp": "image/png", // png instead oft bmp because ResizeItself() uses ExportNative()
"http://127.0.0.1:3333/webp_server.png": "image/png",
"http://127.0.0.1:3333/empty.jpg": "",
"http://127.0.0.1:3333/png.jpg": "image/png",
"http://127.0.0.1:3333/12314.jpg": "",
"http://127.0.0.1:3333/dir1/inside.jpg": "image/jpeg",
}
var app = fiber.New()
app.Get("/*", Convert)
// // test Chrome
for url, respType := range testChromeLink {
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
contentType := helper.GetContentType(data)
assert.Equal(t, respType, contentType)
}
// test Safari
for url, respType := range testSafariLink {
resp, data := requestToServer(url, app, safariUA, acceptLegacy)
defer resp.Body.Close()
contentType := helper.GetContentType(data)
assert.Equal(t, respType, contentType)
}
// test Avif is processed in proxy mode
config.Config.EnableAVIF = true
for url, respType := range testChromeAvifLink {
resp, data := requestToServer(url, app, chromeUA, acceptAvif)
defer resp.Body.Close()
contentType := helper.GetContentType(data)
assert.NotNil(t, respType)
assert.Equal(t, respType, contentType)
}
}
func TestConvertNotAllowed(t *testing.T) {
setupParam()
config.Config.AllowedTypes = []string{"jpg", "png", "jpeg"}
var app = fiber.New()
app.Get("/*", Convert)
// not allowed, but we have the file, this should return File extension not allowed
url := "http://127.0.0.1:3333/webp_server.bmp"
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Contains(t, string(data), "File extension not allowed")
// not allowed, but we have the file, this should return File extension not allowed
url = "http://127.0.0.1:3333/config.json"
resp, data = requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Contains(t, string(data), "File extension not allowed")
// not allowed, random file
url = url + "hagdgd"
resp, data = requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Contains(t, string(data), "File extension not allowed")
}
func TestConvertPassThrough(t *testing.T) {
setupParam()
config.Config.AllowedTypes = []string{"*"}
config.AllowAllExtensions = true
var app = fiber.New()
app.Get("/*", Convert)
// Since AllowedTypes is *, we should be able to access config.json
url := "http://127.0.0.1:3333/config.json"
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
assert.Contains(t, string(data), "HOST")
}
func TestConvertPassThroughWithRemoteBackend(t *testing.T) {
setupParam()
config.Config.AllowedTypes = []string{"*"}
config.Config.ImgPath = "https://docs.webp.sh"
config.ProxyMode = true
config.AllowAllExtensions = true
var app = fiber.New()
app.Get("/*", Convert)
// This should send request to https://docs.webp.sh/sw.js and render this file
url := "http://127.0.0.1:3333/sw.js"
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
// TODO: No idea why this is not working, it shows text/html instead of application/javascript
// assert.Equal(t, "application/javascript", resp.Header.Get("Content-Type"))
assert.Contains(t, string(data), "addEventListener")
}
func TestConvertProxyModeBad(t *testing.T) {
setupParam()
config.ProxyMode = true
var app = fiber.New()
app.Get("/*", Convert)
// this is local random image, should be 404
url := "http://127.0.0.1:3333/webp_8888server.bmp"
resp, _ := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
// this is local random image, test using cURL, should be 404, ref: https://github.com/webp-sh/webp_server_go/issues/197
resp1, _ := requestToServer(url, app, curlUA, acceptWebP)
defer resp1.Body.Close()
assert.Equal(t, http.StatusNotFound, resp1.StatusCode)
}
func TestConvertProxyModeWork(t *testing.T) {
setupParam()
config.ProxyMode = true
config.Config.ImgPath = "https://docs.webp.sh"
var app = fiber.New()
app.Get("/*", Convert)
url := "http://127.0.0.1:3333/images/webp_server.jpg"
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "image/webp", helper.GetContentType(data))
// test proxyMode with Safari
resp, data = requestToServer(url, app, safariUA, acceptLegacy)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, "image/jpeg", helper.GetContentType(data))
}
func TestConvertProxyImgMap(t *testing.T) {
setupParam()
config.ProxyMode = false
config.Config.ImageMap = map[string]string{
"/2": "../pics/dir1",
"/3": "../pics3", // Invalid path, does not exists
"www.invalid-path.com": "https://docs.webp.sh", // Invalid, it does not start with '/'
"/www.weird-path.com": "https://docs.webp.sh",
"/www.even-more-werid-path.com": "https://docs.webp.sh/images",
"http://example.com": "https://docs.webp.sh",
}
var app = fiber.New()
app.Get("/*", Convert)
var testUrls = map[string]string{
"http://127.0.0.1:3333/webp_server.jpg": "image/webp",
"http://127.0.0.1:3333/2/inside.jpg": "image/webp",
"http://127.0.0.1:3333/www.weird-path.com/images/webp_server.jpg": "image/webp",
"http://127.0.0.1:3333/www.even-more-werid-path.com/webp_server.jpg": "image/webp",
"http://example.com//images/webp_server.jpg": "image/webp",
}
var testUrlsLegacy = map[string]string{
"http://127.0.0.1:3333/webp_server.jpg": "image/jpeg",
"http://127.0.0.1:3333/2/inside.jpg": "image/jpeg",
"http://example.com/images/webp_server.jpg": "image/jpeg",
}
var testUrlsInvalid = map[string]string{
"http://127.0.0.1:3333/3/does-not-exist.jpg": "", // Dir mapped does not exist
"http://127.0.0.1:3333/www.weird-path.com/cover.jpg": "", // Host mapped, final URI invalid
}
for url, respType := range testUrls {
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, respType, helper.GetContentType(data))
}
// tests with Safari
for url, respType := range testUrlsLegacy {
resp, data := requestToServer(url, app, safariUA, acceptLegacy)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, respType, helper.GetContentType(data))
}
for url, respType := range testUrlsInvalid {
resp, data := requestToServer(url, app, safariUA, acceptLegacy)
defer resp.Body.Close()
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
assert.Equal(t, respType, helper.GetContentType(data))
}
}
func TestConvertProxyImgMapCWD(t *testing.T) {
setupParam()
config.ProxyMode = false
config.Config.ImgPath = ".." // equivalent to "" when not testing
config.Config.ImageMap = map[string]string{
"/1": "../pics/dir1",
"/2": "../pics",
"/3": "../pics", // Invalid path, does not exists
"http://www.example.com": "https://docs.webp.sh",
}
var app = fiber.New()
app.Get("/*", Convert)
var testUrls = map[string]string{
"http://127.0.0.1:3333/1/inside.jpg": "image/webp",
"http://127.0.0.1:3333/2/webp_server.jpg": "image/webp",
"http://127.0.0.1:3333/3/webp_server.jpg": "image/webp",
"http://www.example.com/images/webp_server.jpg": "image/webp",
}
for url, respType := range testUrls {
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, respType, helper.GetContentType(data))
}
}
func TestConvertBigger(t *testing.T) {
setupParam()
config.Config.Quality = 100
var app = fiber.New()
app.Get("/*", Convert)
url := "http://127.0.0.1:3333/big.jpg"
resp, data := requestToServer(url, app, chromeUA, acceptWebP)
defer resp.Body.Close()
assert.Equal(t, "image/jpeg", resp.Header.Get("content-type"))
assert.Equal(t, "image/jpeg", helper.GetContentType(data))
_ = os.RemoveAll(config.Config.ExhaustPath)
}