forked from willnorris/imageproxy
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdata_test.go
More file actions
332 lines (294 loc) · 11.5 KB
/
data_test.go
File metadata and controls
332 lines (294 loc) · 11.5 KB
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
package imageproxy
import (
"net/http"
"net/url"
"testing"
)
var emptyOptions = Options{}
func TestOptions_String(t *testing.T) {
tests := []struct {
Options Options
String string
}{
{
emptyOptions,
"0x0",
},
{
Options{1, 2, true, 90, true, true, 80, "", false, "", 0, 0, 0, 0, false},
"1x2,fit,r90,fv,fh,q80",
},
{
Options{0.15, 1.3, false, 45, false, false, 95, "c0ffee", false, "png", 0, 0, 0, 0, false},
"0.15x1.3,r45,q95,sc0ffee,png",
},
{
Options{0.15, 1.3, false, 45, false, false, 95, "c0ffee", false, "", 100, 200, 0, 0, false},
"0.15x1.3,r45,q95,sc0ffee,cx100,cy200",
},
{
Options{0.15, 1.3, false, 45, false, false, 95, "c0ffee", false, "png", 100, 200, 300, 400, false},
"0.15x1.3,r45,q95,sc0ffee,png,cx100,cy200,cw300,ch400",
},
}
for i, tt := range tests {
if got, want := tt.Options.String(), tt.String; got != want {
t.Errorf("%d. Options.String returned %v, want %v", i, got, want)
}
}
}
func TestParseFormValues(t *testing.T) {
tests := []struct {
InputQS string
Options Options
}{
{"", emptyOptions},
{"x", emptyOptions},
{"r", emptyOptions},
{"0", emptyOptions},
{"crop=,,,,", emptyOptions},
// size variations
{"width=1", Options{Width: 1}},
{"height=1", Options{Height: 1}},
{"width=1&height=2", Options{Width: 1, Height: 2, Fit: true}},
{"width=-1&height=-2", Options{Width: -1, Height: -2}},
{"width=0.1&height=0.2", Options{Width: 0.1, Height: 0.2, Fit: true}},
{"size=1", Options{Width: 1, Height: 1, Fit: true}},
{"size=0.1", Options{Width: 0.1, Height: 0.1, Fit: true}},
// sizes with dpr
{"width=1&dpr=3", Options{Width: 3}},
{"height=1&dpr=3", Options{Height: 3}},
{"width=1&height=2&dpr=3", Options{Width: 3, Height: 6, Fit: true}},
{"width=-1&height=-2&dpr=3", Options{Width: -3, Height: -6}},
{"width=0.1&height=0.2&dpr=3", Options{Width: 0.3, Height: 0.6, Fit: true}},
{"size=1&dpr=3", Options{Width: 3, Height: 3, Fit: true}},
{"size=0.1&dpr=3", Options{Width: 0.3, Height: 0.3, Fit: true}},
// crop is smart
{"mode=crop&size=200", Options{Width: 200, Height: 200, SmartCrop: true}},
{"mode=smartcrop&size=200", Options{Width: 200, Height: 200, SmartCrop: true}},
// additional flags
{"mode=fit", Options{Fit: true}},
{"rotate=90", Options{Rotate: 90}},
{"flip=v", Options{FlipVertical: true}},
{"flip=h", Options{FlipHorizontal: true}},
{"format=jpeg", Options{Format: "jpeg"}},
// mix of valid and invalid flags
{"FOO=BAR&size=1&BAR=foo&rotate=90&BAZ=DAS", Options{Width: 1, Height: 1, Rotate: 90, Fit: true}},
// flags, in different orders
{"quality=70&width=1&height=2&mode=fit&rotate=90&flip=v&flip=h&signature=c0ffee&format=png", Options{1, 2, true, 90, true, true, 70, "c0ffee", false, "png", 0, 0, 0, 0, false}},
{"rotate=90&flip=h&signature=c0ffee&format=png&quality=90&width=1&height=2&flip=v&mode=fit", Options{1, 2, true, 90, true, true, 90, "c0ffee", false, "png", 0, 0, 0, 0, false}},
// all flags, in different orders with crop
{"quality=70&width=1&height=2&mode=fit&crop=100,200,300,400&rotate=90&flip=v&flip=h&signature=c0ffee&format=png", Options{1, 2, true, 90, true, true, 70, "c0ffee", false, "png", 100, 200, 300, 400, false}},
{"rotate=90&flip=h&signature=c0ffee&format=png&crop=100,200,300,400&quality=90&width=1&height=2&flip=v&mode=fit", Options{1, 2, true, 90, true, true, 90, "c0ffee", false, "png", 100, 200, 300, 400, false}},
// all flags, in different orders with crop & different resizes
{"quality=70&crop=100,200,300,400&height=2&mode=fit&rotate=90&flip=v&flip=h&signature=c0ffee&format=png", Options{0, 2, true, 90, true, true, 70, "c0ffee", false, "png", 100, 200, 300, 400, false}},
{"crop=100,200,300,400&rotate=90&flip=h&quality=90&signature=c0ffee&format=png&width=1&flip=v&mode=fit", Options{1, 0, true, 90, true, true, 90, "c0ffee", false, "png", 100, 200, 300, 400, false}},
{"crop=100,200,300,400&rotate=90&flip=h&signature=c0ffee&flip=v&format=png&quality=90&mode=fit", Options{0, 0, true, 90, true, true, 90, "c0ffee", false, "png", 100, 200, 300, 400, false}},
{"crop=100,200,0,400&rotate=90&quality=90&flip=h&signature=c0ffee&format=png&flip=v&mode=fit&width=123&height=321", Options{123, 321, true, 90, true, true, 90, "c0ffee", false, "png", 100, 200, 0, 400, false}},
{"flip=v&width=123&height=321&crop=100,200,300,400&quality=90&rotate=90&flip=h&signature=c0ffee&format=png&mode=fit", Options{123, 321, true, 90, true, true, 90, "c0ffee", false, "png", 100, 200, 300, 400, false}},
}
for _, tt := range tests {
input, err := url.ParseQuery(tt.InputQS)
if err != nil {
panic(err)
}
if got, want := ParseFormValues(input, Options{}), tt.Options; !got.Equal(want) {
t.Errorf("ParseFormValues(%q) returned %#v, want %#v", tt.InputQS, got, want)
}
}
}
// Test that request URLs are properly parsed into Options and RemoteURL. This
// test verifies that invalid remote URLs throw errors, and that valid
// combinations of Options and URL are accept. This does not exhaustively test
// the various Options that can be specified; see TestParseOptions for that.
func TestNewRequest(t *testing.T) {
tests := []struct {
URL string // input URL to parse as an imageproxy request
RemoteURL string // expected URL of remote image parsed from input
Options Options // expected options parsed from input
ExpectError bool // whether an error is expected from NewRequest
}{
// invalid URLs
{"http://localhost/", "", emptyOptions, true},
{"http://localhost/?size=1", "", emptyOptions, true},
{"http://localhost/example.com/foo", "", emptyOptions, true},
{"http://localhost/ftp://example.com/foo", "", emptyOptions, true},
// invalid options. These won't return errors, but will not fully parse the options
{
"http://localhost/http://example.com/?s",
"http://example.com/?s", emptyOptions, false,
},
{
"http://localhost/http://example.com/?width=1&height=s",
"http://example.com/?width=1&height=s", Options{Width: 1}, false,
},
// valid URLs. the recognized query parameters are dropped just before querying upstream, so they
// are present in RemoteURLs in this phase :(
{
"http://localhost/http://example.com/foo?baz=baz",
"http://example.com/foo?baz=baz", emptyOptions, false,
},
{
"http://localhost/http://example.com/foo",
"http://example.com/foo", emptyOptions, false,
},
{
"http://localhost/http://example.com/foo?width=1&height=2",
"http://example.com/foo?width=1&height=2", Options{Width: 1, Height: 2, Fit: true}, false,
},
{
"http://localhost/http://example.com/foo?width=1&height=2&bar=baz",
"http://example.com/foo?width=1&height=2&bar=baz", Options{Width: 1, Height: 2, Fit: true}, false,
},
{
"http://localhost/http:/example.com/foo",
"http://example.com/foo", emptyOptions, false,
},
{
"http://localhost/http:///example.com/foo",
"http://example.com/foo", emptyOptions, false,
},
{ // escaped path
"http://localhost/http://example.com/%2C",
"http://example.com/%2C", emptyOptions, false,
},
// valid URLs with the prefix
{
"http://localhost/prefix/http://example.com/foo?bar=baz",
"http://example.com/foo?bar=baz", emptyOptions, false,
},
{
"http://localhost/prefix/http://example.com/foo",
"http://example.com/foo", emptyOptions, false,
},
{
"http://localhost/prefix/http://example.com/foo?width=1&height=2",
"http://example.com/foo?width=1&height=2", Options{Width: 1, Height: 2, Fit: true}, false,
},
{
"http://localhost/prefix/http://example.com/foo?width=1&height=2&bar=baz",
"http://example.com/foo?width=1&height=2&bar=baz", Options{Width: 1, Height: 2, Fit: true}, false,
},
{
"http://localhost/prefix/http:/example.com/foo",
"http://example.com/foo", emptyOptions, false,
},
{
"http://localhost/prefix/http:///example.com/foo",
"http://example.com/foo", emptyOptions, false,
},
{ // escaped path
"http://localhost/prefix/http://example.com/%2C",
"http://example.com/%2C", emptyOptions, false,
},
}
// Try with both versions of the same prefix. The results should be same.
prefixes := []string{"/prefix/", "/prefix"}
for _, prefix := range prefixes {
for _, tt := range tests {
req, err := http.NewRequest("GET", tt.URL, nil)
if err != nil {
t.Errorf("http.NewRequest(%q) returned error: %v", tt.URL, err)
continue
}
// Define that our prefix has to be stripped but does not specify a base URL to be used
cfg := SourceConfiguration{BaseURL: nil}
configMap := map[string]*SourceConfiguration{prefix: &cfg}
r, err := NewRequest(req, configMap)
if tt.ExpectError {
if err == nil {
t.Errorf("NewRequest(%v) did not return expected error", req)
}
continue
} else if err != nil {
t.Errorf("NewRequest(%v) return unexpected error: %v", req, err)
continue
}
if got, want := r.URL.String(), tt.RemoteURL; got != want {
t.Errorf("NewRequest(%q) request URL = %v, want %v", tt.URL, got, want)
}
if got, want := r.Options, tt.Options; got != want {
t.Errorf("NewRequest(%q) request options = %v, want %v", tt.URL, got, want)
}
}
}
}
func Test_NewRequest_PrefixAndBaseURL(t *testing.T) {
req, err := http.NewRequest("GET", "http://localhost/prefix/baz.jpg?size=123", nil)
if err != nil {
t.Errorf("http.NewRequest returned error: %s", err.Error())
return
}
baseURL, err := url.Parse("https://imagehost.invalid/foobar/")
if err != nil {
t.Errorf("url.Parse returned error: %s", err.Error())
return
}
cfg := SourceConfiguration{BaseURL: baseURL}
// Define that our prefix has to be stripped and it specifies a base URL
configMap := map[string]*SourceConfiguration{
"/prefix": &cfg,
}
r, err := NewRequest(req, configMap)
if err != nil {
t.Errorf("NewRequest(%v) return unexpected error: %v", req, err)
return
}
expectedRemoteURL := "https://imagehost.invalid/foobar/baz.jpg?size=123"
actualRemoteURL := r.URL.String()
expectedOptions := Options{Width: 123, Height: 123, Fit: true}
actualOptions := r.Options
if expectedRemoteURL != actualRemoteURL {
t.Errorf("NewRequest request URL = %v, want %v", actualRemoteURL, expectedRemoteURL)
}
if expectedOptions != actualOptions {
t.Errorf("NewRequest request options = %v, want %v", actualOptions, expectedOptions)
}
}
func Test_StripOurOptions_NoOptions(t *testing.T) {
input := ""
expected := ""
actual, err := StripOurOptions(input)
if err != nil {
t.Fatalf("caught unexpected error: %s", err.Error())
}
if expected != actual {
t.Fatalf("Got '%s', expecting '%s'", actual, expected)
}
}
func Test_StripOurOptions_OnlyOurOptions(t *testing.T) {
input := "mode=jpeg&width=50"
expected := ""
actual, err := StripOurOptions(input)
if err != nil {
t.Fatalf("caught unexpected error: %s", err.Error())
}
if expected != actual {
t.Fatalf("Got '%s', expecting '%s'", actual, expected)
}
}
func Test_StripOurOptions_OnlyRemoteoOptions(t *testing.T) {
input := "id=123&secret_token=aaa"
actual, err := StripOurOptions(input)
if err != nil {
t.Fatalf("caught unexpected error: %s", err.Error())
}
// Allow both orders
for _, allowed := range []string{"id=123&secret_token=aaa", "secret_token=aaa&id=123"} {
if actual == allowed {
return
}
}
t.Fatalf("Got '%s', expecting '%s' or equivalent", actual, input)
}
func Test_StripOurOptions_AllKindsOfOptions(t *testing.T) {
input := "mode=jpeg&id=123"
expected := "id=123"
actual, err := StripOurOptions(input)
if err != nil {
t.Fatalf("caught unexpected error: %s", err.Error())
}
if expected != actual {
t.Fatalf("Got '%s', expecting '%s'", actual, expected)
}
}