forked from agravelot/traefik-image-optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_optimizer_test.go
301 lines (270 loc) · 8.01 KB
/
image_optimizer_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
package imageopti
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/agravelot/imageopti/config"
)
func TestImageOptimizer_ServeHTTP(t *testing.T) {
type args struct {
config config.Config
}
tests := []struct {
name string
args args
wantedContentType string
wantedCacheStatus string
wantedSecondCacheStatus string
remoteResponseContentType string
remoteResponseContent []byte
want bool
wantErr bool
}{
{
name: "should pass with processor",
args: args{
config: config.Config{
Processor: "imaginary",
Imaginary: config.ImaginaryProcessorConfig{URL: "http://localhost"},
Cache: "none",
Redis: config.RedisCacheConfig{URL: ""},
File: config.FileCacheConfig{Path: ""},
},
},
want: false,
wantErr: false,
wantedCacheStatus: "",
wantedSecondCacheStatus: "",
wantedContentType: "text/html",
remoteResponseContentType: "text/html",
remoteResponseContent: []byte("dummy response"),
},
{
name: "should not pass without processor and cache and return no cache status header",
args: args{
config: config.Config{
Processor: "",
Cache: "",
Redis: config.RedisCacheConfig{URL: ""},
File: config.FileCacheConfig{Path: ""},
Imaginary: config.ImaginaryProcessorConfig{URL: ""},
},
},
want: false,
wantedCacheStatus: "",
wantedSecondCacheStatus: "",
wantErr: true,
wantedContentType: "text/html",
remoteResponseContentType: "text/html",
remoteResponseContent: []byte("dummy response"),
},
// TODO Require to save response headers in cache
// {
// name: "should not modify image with none driver and cache file driver with cache status",
// args: args{config: config.Config{Processor: "none", Cache: "memory"}},
// want: false,
// wantErr: false,
// wantedCacheStatus: "miss",
// wantedSecondCacheStatus: "hit",
// wantedContentType: "image/jpeg",
// remoteResponseContentType: "image/jpeg",
// remoteResponseContent: []byte("dummy image"),
// },
{
name: "should not modify image with none driver and cache file driver with cache status",
args: args{
config: config.Config{
Processor: "local",
Cache: "memory",
Redis: config.RedisCacheConfig{URL: ""},
File: config.FileCacheConfig{Path: ""},
Imaginary: config.ImaginaryProcessorConfig{URL: ""},
},
},
want: false,
wantErr: false,
wantedCacheStatus: "miss",
wantedSecondCacheStatus: "hit",
wantedContentType: "image/webp",
remoteResponseContentType: "image/jpeg",
remoteResponseContent: []byte("dummy image"),
},
{
name: "should return original response if not image request and return no cache status header",
args: args{
config: config.Config{
Processor: "none",
Cache: "",
Redis: config.RedisCacheConfig{URL: ""},
File: config.FileCacheConfig{Path: ""},
Imaginary: config.ImaginaryProcessorConfig{URL: ""},
},
},
want: false,
wantErr: false,
wantedCacheStatus: "",
wantedSecondCacheStatus: "",
wantedContentType: "text/html",
remoteResponseContentType: "text/html",
remoteResponseContent: []byte("dummy response"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := CreateConfig()
cfg.Processor = tt.args.config.Processor
cfg.Imaginary = tt.args.config.Imaginary
cfg.Cache = tt.args.config.Cache
cfg.File = tt.args.config.File
ctx := context.Background()
next := http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Header().Add("content-type", tt.remoteResponseContentType)
_, err := rw.Write(tt.remoteResponseContent)
if err != nil {
t.Fatal(err)
}
})
handler, err := New(ctx, next, cfg, "demo-plugin")
if (err != nil) != tt.wantErr {
t.Fatalf("New() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost", nil)
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if !bytes.Equal(recorder.Body.Bytes(), tt.remoteResponseContent) {
t.Fatal("response are not equals")
}
if recorder.Header().Get("content-type") != tt.wantedContentType {
t.Fatalf("response content-type expected: %v got: %v", tt.wantedContentType, recorder.Header().Get("content-type"))
}
if recorder.Header().Get("cache-status") != tt.wantedCacheStatus {
t.Fatalf("response cache-status expected: %v got: %v", tt.wantedCacheStatus, recorder.Header().Get("cache-status"))
}
recorder = httptest.NewRecorder()
handler.ServeHTTP(recorder, req)
if !bytes.Equal(recorder.Body.Bytes(), tt.remoteResponseContent) {
t.Fatal("response are not equals")
}
if recorder.Header().Get("content-type") != tt.wantedContentType {
t.Fatalf("response content-type expected: %v got: %v", tt.wantedContentType, recorder.Header().Get("content-type"))
}
if recorder.Header().Get("cache-status") != tt.wantedSecondCacheStatus {
t.Fatalf(
"response cache-status expected: %v got: %v",
tt.wantedSecondCacheStatus,
recorder.Header().Get("cache-status"),
)
}
})
}
}
func TestIsImageResponse(t *testing.T) {
type args struct {
contentType string
}
tests := []struct {
name string
args args
want bool
wantErr bool
}{
{
name: "should return false with empty string",
args: args{contentType: ""},
want: false,
wantErr: false,
},
{
name: "should return true with jpeg content type",
args: args{contentType: "image/jpeg"},
want: true,
wantErr: false,
},
{
name: "should return true with webp content type",
args: args{contentType: "image/webp"},
want: true,
wantErr: false,
},
{
name: "should return false with json content type",
args: args{contentType: "application/json"},
want: false,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
recorder := httptest.NewRecorder()
recorder.Header().Add("Content-Type", tt.args.contentType)
got := isImageResponse(recorder)
if got != tt.want {
t.Errorf("IsImageResponse() = %v, want %v", got, tt.want)
}
})
}
}
func TestImageWidthRequest(t *testing.T) {
type args struct{ url string }
tests := []struct {
name string
args args
want int
wantErr bool
}{
{
name: "should return error with positive width",
args: args{url: "http://localhost?w=124"},
want: 124,
wantErr: false,
},
{
name: "should return error with negative width",
args: args{url: "http://localhost?w=-124"},
want: 0,
wantErr: true,
},
{
name: "should return error with text as width",
args: args{url: "http://localhost?w=azeaze"},
want: 0,
wantErr: true,
},
{
name: "should return error with empty width",
args: args{url: "http://localhost?w="},
want: 0,
wantErr: false,
},
{
name: "should return error with no width",
args: args{url: "http://localhost"},
want: 0,
wantErr: false,
},
}
for _, tt := range tests {
ctx := context.Background()
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, tt.args.url, nil)
if err != nil {
t.Fatal(err)
}
got, err := imageWidthRequest(req)
if (err != nil) != tt.wantErr {
t.Fatalf("imageWidthRequest() error = %v, wantErr %v", err, tt.wantErr)
}
if got != tt.want {
t.Errorf("ImageWidthRequest() = %v, want %v", got, tt.want)
}
})
}
}