imgix 3.2.1 → 4.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class DomainsTest < Imgix::Test
6
- def test_invalid_domain_append_slash
7
- assert_raises(ArgumentError) {Imgix::Client.new(host: "assets.imgix.net/")}
8
- end
9
-
10
- def test_invalid_domain_prepend_scheme
11
- assert_raises(ArgumentError) {Imgix::Client.new(host: "https://assets.imgix.net")}
12
- end
13
-
14
- def test_invalid_domain_append_dash
15
- assert_raises(ArgumentError) {Imgix::Client.new(host: "assets.imgix.net-")}
16
- end
17
- end
@@ -1,132 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'test_helper'
4
-
5
- class PathTest < Imgix::Test
6
- def test_creating_a_path
7
- path = client.path('/images/demo.png')
8
- assert_equal 'https://demo.imgix.net/images/demo.png?s=2c7c157eaf23b06a0deb2f60b81938c4', path.to_url
9
-
10
- path = client.path('images/demo.png')
11
- assert_equal 'https://demo.imgix.net/images/demo.png?s=2c7c157eaf23b06a0deb2f60b81938c4', path.to_url
12
- end
13
-
14
- def test_signing_path_with_param
15
- url = 'https://demo.imgix.net/images/demo.png?w=200&s=da421114ca238d1f4a927b889f67c34e'
16
- path = client.path('/images/demo.png')
17
- path.width = 200
18
-
19
- assert_equal url, path.to_url
20
-
21
- path = client.path('/images/demo.png')
22
- assert_equal url, path.to_url(w: 200)
23
-
24
- path = client.path('/images/demo.png')
25
- assert_equal url, path.width(200).to_url
26
- end
27
-
28
- def test_resetting_defaults
29
- url = 'https://demo.imgix.net/images/demo.png?w=200&s=da421114ca238d1f4a927b889f67c34e'
30
- path = client.path('/images/demo.png')
31
- path.height = 300
32
-
33
- assert_equal url, path.defaults.to_url(w: 200)
34
- end
35
-
36
- def test_path_with_multiple_params
37
- url = 'https://demo.imgix.net/images/demo.png?h=200&w=200&s=d570a1ecd765470f7b34a69b56718a7a'
38
- path = client.path('/images/demo.png')
39
-
40
- assert_equal url, path.to_url(h: 200, w: 200)
41
-
42
- path = client.path('/images/demo.png')
43
- assert_equal url, path.height(200).width(200).to_url
44
- end
45
-
46
- def test_path_with_multi_value_param_safely_encoded
47
- url = 'https://demo.imgix.net/images/demo.png?markalign=middle%2Ccenter&s=f0d0e28a739f022638f4ba6dddf9b694'
48
- path = client.path('/images/demo.png')
49
-
50
- assert_equal url, path.markalign('middle', 'center').to_url
51
- end
52
-
53
- def test_param_keys_are_escaped
54
- ix_url = unsigned_client.path('demo.png').to_url({
55
- :'hello world' => 'interesting'
56
- })
57
-
58
- assert_equal "https://demo.imgix.net/demo.png?hello%20world=interesting", ix_url
59
- end
60
-
61
- def test_param_values_are_escaped
62
- ix_url = unsigned_client.path('demo.png').to_url({
63
- hello_world: '/foo"> <script>alert("hacked")</script><'
64
- })
65
-
66
- assert_equal "https://demo.imgix.net/demo.png?hello_world=%2Ffoo%22%3E%20%3Cscript%3Ealert%28%22hacked%22%29%3C%2Fscript%3E%3C", ix_url
67
- end
68
-
69
- def test_base64_param_variants_are_base64_encoded
70
- ix_url = unsigned_client.path('~text').to_url({
71
- txt64: 'I cannøt belîév∑ it wors! 😱'
72
- })
73
-
74
- assert_equal "https://demo.imgix.net/~text?txt64=SSBjYW5uw7h0IGJlbMOuw6l24oiRIGl0IHdvcu-jv3MhIPCfmLE", ix_url
75
- end
76
-
77
- def test_host_is_required
78
- assert_raises(ArgumentError) {Imgix::Client.new}
79
- end
80
-
81
- def test_token_is_optional
82
- client = Imgix::Client.new(host: 'demo.imgix.net', include_library_param: false)
83
- url = 'https://demo.imgix.net/images/demo.png'
84
- path = client.path('/images/demo.png')
85
-
86
- assert_equal url, path.to_url
87
- end
88
-
89
- def test_https_is_optional
90
- client = Imgix::Client.new(host: 'demo.imgix.net', include_library_param: false, use_https: false)
91
- url = 'http://demo.imgix.net/images/demo.png'
92
- path = client.path('/images/demo.png')
93
-
94
- assert_equal url, path.to_url
95
- end
96
-
97
- def test_full_url
98
- path = 'https://google.com/cats.gif'
99
-
100
- assert_equal "https://demo.imgix.net/#{CGI.escape(path)}?s=e686099fbba86fc2b8141d3c1ff60605", client.path(path).to_url
101
- end
102
-
103
- def test_full_url_with_a_space
104
- path = 'https://my-demo-site.com/files/133467012/avatar icon.png'
105
- assert_equal "https://demo.imgix.net/#{CGI.escape(path)}?s=35ca40e2e7b6bd208be2c4f7073f658e", client.path(path).to_url
106
- end
107
-
108
- def test_include_library_param
109
- client = Imgix::Client.new(host: 'demo.imgix.net') # enabled by default
110
- url = client.path('/images/demo.png').to_url
111
-
112
- assert_equal "ixlib=rb-#{Imgix::VERSION}", URI(url).query
113
- end
114
-
115
- def test_configure_library_param
116
- library = "sinatra"
117
- version = Imgix::VERSION
118
- client = Imgix::Client.new(host: 'demo.imgix.net', library_param: library, library_version: version) # enabled by default
119
- url = client.path('/images/demo.png').to_url
120
-
121
- assert_equal "ixlib=#{library}-#{version}", URI(url).query
122
- end
123
-
124
- private
125
- def client
126
- @client ||= Imgix::Client.new(host: 'demo.imgix.net', secure_url_token: '10adc394', include_library_param: false)
127
- end
128
-
129
- def unsigned_client
130
- @unsigned_client ||= Imgix::Client.new(host: 'demo.imgix.net', include_library_param: false)
131
- end
132
- end
@@ -1,25 +0,0 @@
1
- require 'test_helper'
2
-
3
- class PurgeTest < Imgix::Test
4
- def test_runtime_error_without_api_key
5
- assert_raises(RuntimeError) {
6
- Imgix::Client.new(host: 'demo.imgix.net', include_library_param: false)
7
- .purge('https://demo.imgix.net/images/demo.png')
8
- }
9
- end
10
-
11
- def test_successful_purge
12
- stub_request(:post, "https://api.imgix.com/v2/image/purger").
13
- with(
14
- body: {"url"=>"https://demo.imgix.net/images/demo.png"}).
15
- to_return(status: 200)
16
-
17
- Imgix::Client.new(host: 'demo.imgix.net', api_key: '10adc394')
18
- .purge('/images/demo.png')
19
-
20
- assert_requested :post, 'https://api.imgix.com/v2/image/purger',
21
- body: 'url=https%3A%2F%2Fdemo.imgix.net%2Fimages%2Fdemo.png',
22
- headers: {'Accept'=>'*/*', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Authorization'=>'Basic MTBhZGMzOTQ6', 'Content-Type'=>'application/x-www-form-urlencoded', 'User-Agent'=>"imgix rb-#{ Imgix::VERSION}"},
23
- times: 1
24
- end
25
- end
@@ -1,716 +0,0 @@
1
- require 'test_helper'
2
-
3
- module SrcsetTest
4
-
5
- class SrcsetDefault < Imgix::Test
6
- def test_no_parameters
7
- srcset = path.to_srcset()
8
- expected_number_of_pairs = 31
9
- assert_equal expected_number_of_pairs, srcset.split(',').length
10
- end
11
-
12
- def test_srcset_pair_values
13
- resolutions = [100, 116, 134, 156, 182, 210, 244, 282,
14
- 328, 380, 442, 512, 594, 688, 798, 926,
15
- 1074, 1246, 1446, 1678, 1946, 2258, 2618,
16
- 3038, 3524, 4088, 4742, 5500, 6380, 7400, 8192]
17
- srcset = path.to_srcset()
18
- srclist = srcset.split(',').map { |srcset_split|
19
- srcset_split.split(' ')[1].to_i
20
- }
21
-
22
- for i in 0..srclist.length - 1 do
23
- assert_equal(srclist[i], resolutions[i])
24
- end
25
- end
26
-
27
- private
28
- def path
29
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg')
30
- end
31
- end
32
-
33
- class SrcsetGivenWidth < Imgix::Test
34
- def test_srcset_in_dpr_form
35
- device_pixel_ratio = 1
36
-
37
- srcset.split(',').map { |src|
38
- ratio = src.split(' ')[1]
39
- assert_equal ("#{device_pixel_ratio}x"), ratio
40
- device_pixel_ratio += 1
41
- }
42
- end
43
-
44
- def test_srcset_has_dpr_params
45
- i = 1
46
- srcset.split(',').map { |srcset_split|
47
- src = srcset_split.split(' ')[0]
48
- assert_includes src, "dpr=#{i}"
49
- i += 1
50
- }
51
- end
52
-
53
- def test_srcset_signs_urls
54
- srcset.split(',').map { |srcset_split|
55
- src = srcset_split.split(' ')[0]
56
- assert_includes src, 's='
57
-
58
- # parses out all parameters except for 's=...'
59
- params = src[src.index('?')..src.index('s=') - 2]
60
-
61
- # parses out the 's=...' parameter
62
- generated_signature = src.slice(src.index('s=') + 2, src.length)
63
-
64
- signature_base = 'MYT0KEN' + '/image.jpg' + params;
65
- expected_signature = Digest::MD5.hexdigest(signature_base)
66
-
67
- assert_equal expected_signature, generated_signature
68
- }
69
- end
70
-
71
- def test_srcset_has_variable_qualities
72
- i = 0
73
- srcset.split(',').map { |src|
74
- assert_includes src, "q=#{DPR_QUALITY[i]}"
75
- i += 1
76
- }
77
- end
78
-
79
- def test_srcset_respects_overriding_quality
80
- quality_override = 100
81
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, q:quality_override)
82
-
83
- srcset.split(',').map { |src|
84
- assert_includes src, "q=#{quality_override}"
85
- }
86
- end
87
-
88
- def test_disable_variable_quality
89
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, options: { disable_variable_quality: true })
90
-
91
- srcset.split(',').map { |src|
92
- assert(not(src.include? "q="))
93
- }
94
- end
95
-
96
- def test_respects_quality_param_when_disabled
97
- quality_override = 100
98
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, q:100, options: { disable_variable_quality: true })
99
-
100
- srcset.split(',').map { |src|
101
- assert_includes src, "q=#{quality_override}"
102
- }
103
- end
104
-
105
- private
106
- DPR_QUALITY = [75, 50, 35, 23, 20]
107
-
108
- def srcset
109
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100)
110
- end
111
- end
112
-
113
- class SrcsetGivenHeight < Imgix::Test
114
- def test_srcset_generates_width_pairs
115
- expected_number_of_pairs = 31
116
- assert_equal expected_number_of_pairs, srcset.split(',').length
117
- end
118
-
119
- def test_srcset_pair_values
120
- resolutions = [100, 116, 134, 156, 182, 210, 244, 282,
121
- 328, 380, 442, 512, 594, 688, 798, 926,
122
- 1074, 1246, 1446, 1678, 1946, 2258, 2618,
123
- 3038, 3524, 4088, 4742, 5500, 6380, 7400, 8192]
124
- srclist = srcset.split(',').map { |srcset_split|
125
- srcset_split.split(' ')[1].to_i
126
- }
127
-
128
- for i in 0..srclist.length - 1 do
129
- assert_equal(srclist[i], resolutions[i])
130
- end
131
- end
132
-
133
- def test_srcset_respects_height_parameter
134
- srcset.split(',').map { |src|
135
- assert_includes src, 'h='
136
- }
137
- end
138
-
139
- def test_srcset_within_bounds
140
- min, *max = srcset.split(',')
141
-
142
- # parse out the width descriptor as an integer
143
- min = min.split(' ')[1].to_i
144
- max = max[max.length - 1].split(' ')[1].to_i
145
-
146
- assert_operator min, :>=, 100
147
- assert_operator max, :<=, 8192
148
- end
149
-
150
- # a 17% testing threshold is used to account for rounding
151
- def test_srcset_iterates_17_percent
152
- increment_allowed = 0.17
153
-
154
- # create an array of widths
155
- widths = srcset.split(',').map { |src|
156
- src.split(' ')[1].to_i
157
- }
158
-
159
- prev = widths[0]
160
-
161
- for i in 1..widths.length - 1 do
162
- element = widths[i]
163
- assert_operator (element.to_f / prev.to_f), :<, (1 + increment_allowed)
164
- prev = element
165
- end
166
- end
167
-
168
- def test_srcset_signs_urls
169
- srcset.split(',').map { |srcset_split|
170
- src = srcset_split.split(' ')[0]
171
- assert_includes src, 's='
172
-
173
- # parses out all parameters except for 's=...'
174
- params = src[src.index('?')..src.index('s=') - 2]
175
-
176
- # parses out the 's=...' parameter
177
- generated_signature = src.slice(src.index('s=') + 2, src.length)
178
-
179
- signature_base = 'MYT0KEN' + '/image.jpg' + params;
180
- expected_signature = Digest::MD5.hexdigest(signature_base)
181
-
182
- assert_equal expected_signature, generated_signature
183
- }
184
- end
185
-
186
- private
187
- def srcset
188
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(h:100)
189
- end
190
- end
191
-
192
- class SrcsetGivenWidthAndHeight < Imgix::Test
193
- def test_srcset_in_dpr_form
194
- device_pixel_ratio = 1
195
- srcset.split(',').map { |src|
196
- ratio = src.split(' ')[1]
197
- assert_equal ("#{device_pixel_ratio}x"), ratio
198
- device_pixel_ratio += 1
199
- }
200
- end
201
-
202
- def test_srcset_has_dpr_params
203
- i = 1
204
- srcset.split(',').map { |srcset_split|
205
- src = srcset_split.split(' ')[0]
206
- assert_includes src, "dpr=#{i}"
207
- i += 1
208
- }
209
- end
210
-
211
- def test_srcset_signs_urls
212
- srcset.split(',').map { |srcset_split|
213
- src = srcset_split.split(' ')[0]
214
- assert_includes src, 's='
215
-
216
- # parses out all parameters except for 's=...'
217
- params = src[src.index('?')..src.index('s=') - 2]
218
-
219
- # parses out the 's=...' parameter
220
- generated_signature = src.slice(src.index('s=') + 2, src.length)
221
-
222
- signature_base = 'MYT0KEN' + '/image.jpg' + params;
223
- expected_signature = Digest::MD5.hexdigest(signature_base)
224
-
225
- assert_equal expected_signature, generated_signature
226
- }
227
- end
228
-
229
- def test_srcset_has_variable_qualities
230
- i = 0
231
- srcset.split(',').map { |src|
232
- assert_includes src, "q=#{DPR_QUALITY[i]}"
233
- i += 1
234
- }
235
- end
236
-
237
- def test_srcset_respects_overriding_quality
238
- quality_override = 100
239
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, h:100, q:quality_override)
240
-
241
- srcset.split(',').map { |src|
242
- assert_includes src, "q=#{quality_override}"
243
- }
244
- end
245
-
246
- def test_disable_variable_quality
247
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, h:100, options: { disable_variable_quality: true })
248
-
249
- srcset.split(',').map { |src|
250
- assert(not(src.include? "q="))
251
- }
252
- end
253
-
254
- def test_respects_quality_param_when_disabled
255
- quality_override = 100
256
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, h:100, q:100, options: { disable_variable_quality: true })
257
-
258
- srcset.split(',').map { |src|
259
- assert_includes src, "q=#{quality_override}"
260
- }
261
- end
262
-
263
- private
264
- DPR_QUALITY = [75, 50, 35, 23, 20]
265
-
266
- def srcset
267
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100,h:100)
268
- end
269
- end
270
-
271
- class SrcsetGivenAspectRatio < Imgix::Test
272
- def test_srcset_generates_width_pairs
273
- expected_number_of_pairs = 31
274
- assert_equal expected_number_of_pairs, srcset.split(',').length
275
- end
276
-
277
- def test_srcset_pair_values
278
- resolutions = [100, 116, 134, 156, 182, 210, 244, 282,
279
- 328, 380, 442, 512, 594, 688, 798, 926,
280
- 1074, 1246, 1446, 1678, 1946, 2258, 2618,
281
- 3038, 3524, 4088, 4742, 5500, 6380, 7400, 8192]
282
- srclist = srcset.split(',').map { |srcset_split|
283
- srcset_split.split(' ')[1].to_i
284
- }
285
-
286
- for i in 0..srclist.length - 1 do
287
- assert_equal(srclist[i], resolutions[i])
288
- end
289
- end
290
-
291
- def test_srcset_within_bounds
292
- min, *max = srcset.split(',')
293
-
294
- # parse out the width descriptor as an integer
295
- min = min.split(' ')[1].to_i
296
- max = max[max.length - 1].split(' ')[1].to_i
297
-
298
- assert_operator min, :>=, 100
299
- assert_operator max, :<=, 8192
300
- end
301
-
302
- # a 17% testing threshold is used to account for rounding
303
- def test_srcset_iterates_17_percent
304
- increment_allowed = 0.17
305
-
306
- # create an array of widths
307
- widths = srcset.split(',').map { |src|
308
- src.split(' ')[1].to_i
309
- }
310
-
311
- prev = widths[0]
312
-
313
- for i in 1..widths.length - 1 do
314
- element = widths[i]
315
- assert_operator (element.to_f / prev.to_f), :<, (1 + increment_allowed)
316
- prev = element
317
- end
318
- end
319
-
320
- def test_srcset_signs_urls
321
- srcset.split(',').map { |srcset_split|
322
- src = srcset_split.split(' ')[0]
323
- assert_includes src, 's='
324
-
325
- # parses out all parameters except for 's=...'
326
- params = src[src.index('?')..src.index('s=') - 2]
327
-
328
- # parses out the 's=...' parameter
329
- generated_signature = src.slice(src.index('s=') + 2, src.length)
330
-
331
- signature_base = 'MYT0KEN' + '/image.jpg' + params;
332
- expected_signature = Digest::MD5.hexdigest(signature_base)
333
-
334
- assert_equal expected_signature, generated_signature
335
- }
336
- end
337
-
338
- private
339
- def srcset
340
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(ar:'3:2')
341
- end
342
- end
343
-
344
- class SrcsetGivenAspectRatioAndHeight < Imgix::Test
345
- def test_srcset_in_dpr_form
346
- device_pixel_ratio = 1
347
-
348
- srcset.split(',').map { |src|
349
- ratio = src.split(' ')[1]
350
- assert_equal ("#{device_pixel_ratio}x"), ratio
351
- device_pixel_ratio += 1
352
- }
353
- end
354
-
355
- def test_srcset_has_dpr_params
356
- i = 1
357
- srcset.split(',').map { |srcset_split|
358
- src = srcset_split.split(' ')[0]
359
- assert_includes src, "dpr=#{i}"
360
- i += 1
361
- }
362
- end
363
-
364
- def test_srcset_signs_urls
365
- srcset.split(',').map { |srcset_split|
366
- src = srcset_split.split(' ')[0]
367
- assert_includes src, 's='
368
-
369
- # parses out all parameters except for 's=...'
370
- params = src[src.index('?')..src.index('s=') - 2]
371
-
372
- # parses out the 's=...' parameter
373
- generated_signature = src.slice(src.index('s=') + 2, src.length)
374
-
375
- signature_base = 'MYT0KEN' + '/image.jpg' + params;
376
- expected_signature = Digest::MD5.hexdigest(signature_base)
377
-
378
- assert_equal expected_signature, generated_signature
379
- }
380
- end
381
-
382
- def test_srcset_has_variable_qualities
383
- i = 0
384
- srcset.split(',').map { |src|
385
- assert_includes src, "q=#{DPR_QUALITY[i]}"
386
- i += 1
387
- }
388
- end
389
-
390
- def test_srcset_respects_overriding_quality
391
- quality_override = 100
392
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, ar:'3:2', q:quality_override)
393
-
394
- srcset.split(',').map { |src|
395
- assert_includes src, "q=#{quality_override}"
396
- }
397
- end
398
-
399
- def test_disable_variable_quality
400
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, ar:'3:2', options: { disable_variable_quality: true })
401
-
402
- srcset.split(',').map { |src|
403
- assert(not(src.include? "q="))
404
- }
405
- end
406
-
407
- def test_respects_quality_param_when_disabled
408
- quality_override = 100
409
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(w:100, h:100, q:100, options: { disable_variable_quality: true })
410
-
411
- srcset.split(',').map { |src|
412
- assert_includes src, "q=#{quality_override}"
413
- }
414
- end
415
-
416
- private
417
- DPR_QUALITY = [75, 50, 35, 23, 20]
418
-
419
- def srcset
420
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset({h:100,ar:'3:2'})
421
- end
422
- end
423
-
424
- class SrcsetWidthTolerance < Imgix::Test
425
- def test_srcset_generates_width_pairs
426
- expected_number_of_pairs = 15
427
- assert_equal expected_number_of_pairs, srcset.split(',').length
428
- end
429
-
430
- def test_srcset_pair_values
431
- resolutions = [100,140,196,274,384,538,752,1054,1476,2066,2892,4050,5670,7938,8192]
432
- srclist = srcset.split(',').map { |srcset_split|
433
- srcset_split.split(' ')[1].to_i
434
- }
435
-
436
- for i in 0..srclist.length - 1 do
437
- assert_equal(srclist[i], resolutions[i])
438
- end
439
- end
440
-
441
- def test_srcset_within_bounds
442
- min, *max = srcset.split(',')
443
-
444
- # parse out the width descriptor as an integer
445
- min = min.split(' ')[1].to_i
446
- max = max[max.length - 1].split(' ')[1].to_i
447
- assert_operator min, :>=, 100
448
- assert_operator max, :<=, 8192
449
- end
450
-
451
- # a 41% testing threshold is used to account for rounding
452
- def test_srcset_iterates_41_percent
453
- increment_allowed = 0.41
454
-
455
- # create an array of widths
456
- widths = srcset.split(',').map { |src|
457
- src.split(' ')[1].to_i
458
- }
459
-
460
- prev = widths[0]
461
-
462
- for i in 1..widths.length - 1 do
463
- element = widths[i]
464
- assert_operator (element.to_f / prev.to_f), :<, (1 + increment_allowed)
465
- prev = element
466
- end
467
- end
468
-
469
- def test_invalid_tolerance_emits_error
470
- assert_raises(ArgumentError) {
471
- Imgix::Client.new(host: 'testing.imgix.net')
472
- .path('image.jpg')
473
- .to_srcset(options: {width_tolerance: 'abc'})
474
- }
475
- end
476
-
477
- def test_negative_tolerance_emits_error
478
- assert_raises(ArgumentError) {
479
- Imgix::Client.new(host: 'testing.imgix.net')
480
- .path('image.jpg')
481
- .to_srcset(options: {width_tolerance: -0.10})
482
- }
483
- end
484
-
485
- def test_with_param_after
486
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false)
487
- .path('image.jpg')
488
- .to_srcset(options: {width_tolerance: 0.20}, h:1000, fit:"clip")
489
- assert_includes(srcset, "h=")
490
- assert(not(srcset.include? "width_tolerance="))
491
- end
492
-
493
- def test_with_param_before
494
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false)
495
- .path('image.jpg')
496
- .to_srcset(h:1000, fit:"clip", options: {width_tolerance: 0.20})
497
- assert_includes(srcset, "h=")
498
- assert(not(srcset.include? "width_tolerance="))
499
- end
500
-
501
- private
502
- def srcset
503
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(options: {width_tolerance: 0.20})
504
- end
505
- end
506
-
507
- class SrcsetCustomWidths < Imgix::Test
508
- def test_srcset_generates_width_pairs
509
- expected_number_of_pairs = 4
510
- assert_equal expected_number_of_pairs, srcset.split(',').length
511
- end
512
-
513
- def test_srcset_pair_values
514
- resolutions = [100, 500, 1000, 1800]
515
- srclist = srcset.split(',').map { |srcset_split|
516
- srcset_split.split(' ')[1].to_i
517
- }
518
-
519
- for i in 0..srclist.length - 1 do
520
- assert_equal(srclist[i], resolutions[i])
521
- end
522
- end
523
-
524
- def test_srcset_within_bounds
525
- min, *max = srcset.split(',')
526
-
527
- # parse out the width descriptor as an integer
528
- min = min.split(' ')[1].to_i
529
- max = max[max.length - 1].split(' ')[1].to_i
530
-
531
- assert_operator min, :>=, @widths[0]
532
- assert_operator max, :<=, @widths[-1]
533
- end
534
-
535
- def test_invalid_widths_input_emits_error
536
- assert_raises(ArgumentError) {
537
- Imgix::Client.new(host: 'testing.imgix.net')
538
- .path('image.jpg')
539
- .to_srcset(options: {widths: 'abc'})
540
- }
541
- end
542
-
543
- def test_non_integer_array_emits_error
544
- assert_raises(ArgumentError) {
545
- Imgix::Client.new(host: 'testing.imgix.net')
546
- .path('image.jpg')
547
- .to_srcset(options: {widths: [100, 200, false]})
548
- }
549
- end
550
-
551
- def test_negative_integer_array_emits_error
552
- assert_raises(ArgumentError) {
553
- Imgix::Client.new(host: 'testing.imgix.net')
554
- .path('image.jpg')
555
- .to_srcset(options: {widths: [100, 200, -100]})
556
- }
557
- end
558
-
559
- def test_with_param_after
560
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false)
561
- .path('image.jpg')
562
- .to_srcset(options: {widths: [100, 200, 300]}, h:1000, fit:"clip")
563
- assert_includes(srcset, "h=")
564
- assert(not(srcset.include? "widths="))
565
- end
566
-
567
- def test_with_param_before
568
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false)
569
- .path('image.jpg')
570
- .to_srcset(h:1000, fit:"clip", options: {widths: [100, 200, 300]})
571
- assert_includes(srcset, "h=")
572
- assert(not(srcset.include? "widths="))
573
- end
574
-
575
- private
576
- def srcset
577
- @widths = [100, 500, 1000, 1800]
578
- @client ||= Imgix::Client.new(
579
- host: 'testing.imgix.net',
580
- include_library_param: false)
581
- .path('image.jpg')
582
- .to_srcset(options: {widths: @widths})
583
- end
584
- end
585
-
586
- class SrcsetMinMaxWidths < Imgix::Test
587
- def test_srcset_generates_width_pairs
588
- expected_number_of_pairs = 11
589
- assert_equal expected_number_of_pairs, srcset.split(',').length
590
- end
591
-
592
- def test_srcset_pair_values
593
- resolutions = [500,580,672,780,906,1050,1218,1414,1640,1902,2000]
594
- srclist = srcset.split(',').map { |srcset_split|
595
- srcset_split.split(' ')[1].to_i
596
- }
597
-
598
- for i in 0..srclist.length - 1 do
599
- assert_equal(srclist[i], resolutions[i])
600
- end
601
- end
602
-
603
- def test_srcset_within_bounds
604
- min, *max = srcset.split(',')
605
-
606
- # parse out the width descriptor as an integer
607
- min = min.split(' ')[1].to_i
608
- max = max[max.length - 1].split(' ')[1].to_i
609
-
610
- assert_operator min, :>=, @MIN
611
- assert_operator max, :<=, @MAX
612
- end
613
-
614
- # a 41% testing threshold is used to account for rounding
615
- def test_with_custom_width_tolerance
616
- srcset = Imgix::Client.new(host: 'testing.imgix.net', secure_url_token: 'MYT0KEN', include_library_param: false).path('image.jpg').to_srcset(options: {min_width: 500, max_width: 2000, width_tolerance: 0.20})
617
-
618
- increment_allowed = 0.41
619
-
620
- # create an array of widths
621
- widths = srcset.split(',').map { |src|
622
- src.split(' ')[1].to_i
623
- }
624
-
625
- prev = widths[0]
626
-
627
- for i in 1..widths.length - 1 do
628
- element = widths[i]
629
- assert_operator (element.to_f / prev.to_f), :<, (1 + increment_allowed)
630
- prev = element
631
- end
632
- end
633
-
634
- def test_invalid_min_emits_error
635
- assert_raises(ArgumentError) {
636
- Imgix::Client.new(host: 'testing.imgix.net')
637
- .path('image.jpg')
638
- .to_srcset(options: {min_width: 'abc'})
639
- }
640
- end
641
-
642
- def test_negative_max_emits_error
643
- assert_raises(ArgumentError) {
644
- Imgix::Client.new(host: 'testing.imgix.net')
645
- .path('image.jpg')
646
- .to_srcset(options: {max_width: -100})
647
- }
648
- end
649
-
650
- def test_with_param_after
651
- srcset = Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false)
652
- .path('image.jpg')
653
- .to_srcset(options: {min_width: 500, max_width:2000}, h:1000, fit:"clip")
654
-
655
- assert_includes(srcset, "h=")
656
- assert(not(srcset.include? "min_width="))
657
- assert(not(srcset.include? "max_width="))
658
- end
659
-
660
- def test_with_param_before
661
- srcset = Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false)
662
- .path('image.jpg')
663
- .to_srcset(h:1000, fit:"clip", options: {min_width: 500, max_width:2000})
664
-
665
- assert_includes(srcset, "h=")
666
- assert(not(srcset.include? "min_width="))
667
- assert(not(srcset.include? "max_width="))
668
- end
669
-
670
- def test_only_min
671
- min_width = 1000
672
- max_width = 8192
673
- srcset = Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false).path('image.jpg').to_srcset(options: {min_width: min_width})
674
-
675
- min, *max = srcset.split(',')
676
-
677
- # parse out the width descriptor as an integer
678
- min = min.split(' ')[1].to_i
679
- max = max[max.length - 1].split(' ')[1].to_i
680
-
681
- assert_operator min, :>=, min_width
682
- assert_operator max, :<=, max_width
683
- end
684
-
685
- def test_only_max
686
- min_width = 100
687
- max_width = 1000
688
- srcset = Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false).path('image.jpg').to_srcset(options: {max_width: max_width})
689
- min, *max = srcset.split(',')
690
-
691
- # parse out the width descriptor as an integer
692
- min = min.split(' ')[1].to_i
693
- max = max[max.length - 1].split(' ')[1].to_i
694
-
695
- assert_operator min, :>=, min_width
696
- assert_operator max, :<=, max_width
697
- end
698
-
699
- def test_max_as_100
700
- srcset = Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false).path('image.jpg').to_srcset(options: {max_width: 100})
701
- assert_equal(srcset, "https://testing.imgix.net/image.jpg?w=100 100w")
702
- end
703
-
704
- def test_min_as_8192
705
- srcset = Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false).path('image.jpg').to_srcset(options: {min_width: 8192})
706
- assert_equal(srcset, "https://testing.imgix.net/image.jpg?w=8192 8192w")
707
- end
708
-
709
- private
710
- def srcset
711
- @MIN = 500
712
- @MAX = 2000
713
- @client ||= Imgix::Client.new(host: 'testing.imgix.net', include_library_param: false).path('image.jpg').to_srcset(options: {min_width: @MIN, max_width: @MAX})
714
- end
715
- end
716
- end