rack-zippy 3.0.1 → 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,14 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- module Rack
4
- module Zippy
5
- class NullAssetCompilerTest < TestCase
6
-
7
- def test_never_wants_to_compile_assets
8
- asset_compiler = NullAssetCompiler.new
9
- assert !asset_compiler.compiles?('/assets/application.css')
10
- end
11
-
12
- end
13
- end
14
- end
@@ -1,46 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- module Rack
4
- module Zippy
5
- class RailsAssetCompilerTest < TestCase
6
-
7
- def setup
8
- enter_rails_env
9
- ::Rails.configuration.assets.compile = false
10
- end
11
-
12
- def test_rails_env_detected_if_rails_version_defined
13
- assert ::Rails.version
14
- assert RailsAssetCompiler.rails_env?
15
- end
16
-
17
- def test_not_rails_env
18
- exit_rails_env
19
- assert !RailsAssetCompiler.rails_env?
20
- end
21
-
22
- def test_wants_to_compile_assets_when_active
23
- ::Rails.configuration.assets.compile = true
24
- asset_compiler = RailsAssetCompiler.new
25
- assert asset_compiler.send(:active?), 'should be active'
26
- assert asset_compiler.compiles?('/assets/application.css')
27
- end
28
-
29
- def test_does_not_want_to_compile_anything_when_inactive
30
- assert !::Rails.configuration.assets.compile, 'assets.compile should not be active'
31
- asset_compiler = RailsAssetCompiler.new
32
- assert !asset_compiler.send(:active?), 'should not be active'
33
- assert !asset_compiler.compiles?('/assets/application.css')
34
- assert !asset_compiler.compiles?('/thanks.html')
35
- end
36
-
37
- def test_does_not_want_to_compile_non_asset_when_active
38
- ::Rails.configuration.assets.compile = true
39
- asset_compiler = RailsAssetCompiler.new
40
- assert asset_compiler.send(:active?), 'should be active'
41
- assert !asset_compiler.compiles?('/thanks.html')
42
- end
43
-
44
- end
45
- end
46
- end
@@ -1,494 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- module Rack
4
- module Zippy
5
- class ServeableFileTest < TestCase
6
-
7
- CACHE_FRIENDLY_LAST_MODIFIED = 'Mon, 10 Jan 2005 10:00:00 GMT'
8
-
9
- def setup
10
- ensure_correct_working_directory
11
- enter_rails_env
12
- ::Rails.configuration.assets.compile = false
13
- end
14
-
15
- def teardown
16
- revert_to_original_working_directory
17
- Rack::Zippy.reset_static_extensions
18
- end
19
-
20
- def test_day_long_cache_headers_for_root_html_requests
21
- serveable_file = ServeableFile.new(
22
- :full_path_info => "/thanks.html",
23
- :path => "#{asset_root}/thanks.html",
24
- :has_encoding_variants => false,
25
- :is_gzipped => false
26
- )
27
-
28
- cache_headers = serveable_file.cache_headers
29
-
30
- assert_cache_max_age cache_headers, :day
31
- assert_last_modified cache_headers, nil
32
- end
33
-
34
- def test_max_age_fallback_used_for_cache_headers
35
-
36
- ten_mins_in_secs = 10*60
37
-
38
- serveable_file = ServeableFile.new(
39
- :full_path_info => "/thanks.html",
40
- :path => "#{asset_root}/thanks.html",
41
- :has_encoding_variants => false,
42
- :is_gzipped => false,
43
- :max_age_fallback => ten_mins_in_secs
44
- )
45
-
46
- cache_headers = serveable_file.cache_headers
47
-
48
- assert_cache_max_age cache_headers, ten_mins_in_secs
49
- assert_last_modified cache_headers, nil
50
- end
51
-
52
- def test_cache_max_age_is_month_for_root_favicon
53
- serveable_file = ServeableFile.new(
54
- :full_path_info => "/favicon.ico",
55
- :path => "#{asset_root}/favicon.ico",
56
- :has_encoding_variants => false,
57
- :is_gzipped => false
58
- )
59
-
60
- assert_cache_max_age serveable_file.cache_headers, :month
61
- end
62
-
63
- def test_maximum_cache_headers_for_assets_subdir_requests
64
- serveable_file = ServeableFile.new(
65
- :full_path_info => "/assets/favicon.ico",
66
- :path => "#{asset_root}/assets/favicon.ico",
67
- :has_encoding_variants => false,
68
- :is_gzipped => false
69
- )
70
-
71
- cache_headers = serveable_file.cache_headers
72
-
73
- assert_cache_max_age cache_headers, :year
74
- assert_cache_friendly_last_modified cache_headers
75
- end
76
-
77
- def test_cache_friendly_last_modified_is_not_set_for_files_outside_of_assets_subdir
78
- serveable_file = ServeableFile.new(
79
- :full_path_info => "/robots.txt",
80
- :path => "#{asset_root}/robots.txt",
81
- :has_encoding_variants => false,
82
- :is_gzipped => false
83
- )
84
-
85
- assert_last_modified serveable_file.cache_headers, nil
86
- end
87
-
88
- def test_cache_friendly_last_modified_is_set_for_root_favicon_as_it_rarely_changes
89
- serveable_file = ServeableFile.new(
90
- :full_path_info => "/favicon.ico",
91
- :path => "#{asset_root}/favicon.ico",
92
- :has_encoding_variants => false,
93
- :is_gzipped => false
94
- )
95
-
96
- assert_cache_friendly_last_modified serveable_file.cache_headers
97
- end
98
-
99
- def test_headers_sets_content_length
100
- path = "#{asset_root}/thanks.html"
101
-
102
- serveable_file = ServeableFile.new(
103
- :full_path_info => "/thanks.html",
104
- :path => path,
105
- :has_encoding_variants => false,
106
- :is_gzipped => false
107
- )
108
-
109
- headers = serveable_file.headers
110
-
111
- file_size_in_bytes = ::File.size(path)
112
- assert_equal 108, file_size_in_bytes
113
- assert_equal file_size_in_bytes, headers['Content-Length'].to_i
114
- end
115
-
116
- def test_headers_does_not_set_vary_header_for_file_without_gzipped_counterpart
117
- serveable_file = ServeableFile.new(
118
- :full_path_info => "/thanks.html",
119
- :path => "#{asset_root}/thanks.html",
120
- :has_encoding_variants => false,
121
- :is_gzipped => false
122
- )
123
-
124
- headers = serveable_file.headers
125
-
126
- assert_nil headers['Vary']
127
- assert_nil headers['Content-Encoding']
128
- end
129
-
130
- def test_headers_sets_encoding_related_headers_for_gzipped_asset
131
- serveable_file = ServeableFile.new(
132
- :full_path_info => "/assets/application.css",
133
- :path => "#{asset_root}/assets/application.css.gz",
134
- :has_encoding_variants => true,
135
- :is_gzipped => true
136
- )
137
-
138
- headers = serveable_file.headers
139
-
140
- assert_equal 'Accept-Encoding', headers['Vary']
141
- assert_equal 'gzip', headers['Content-Encoding']
142
- end
143
-
144
- def test_headers_sets_vary_header_for_uncompressed_asset_with_gzipped_counterpart
145
- serveable_file = ServeableFile.new(
146
- :full_path_info => "/assets/application.css",
147
- :path => "#{asset_root}/assets/application.css",
148
- :has_encoding_variants => true,
149
- :is_gzipped => false
150
- )
151
-
152
- headers = serveable_file.headers
153
-
154
- assert_equal 'Accept-Encoding', headers['Vary']
155
- assert_nil headers['Content-Encoding']
156
- end
157
-
158
- def test_headers_sets_content_type_header_for_gzipped_asset
159
- serveable_file = ServeableFile.new(
160
- :full_path_info => "/assets/application.js",
161
- :path => "#{asset_root}/assets/application.js.gz",
162
- :has_encoding_variants => true,
163
- :is_gzipped => true
164
- )
165
-
166
- headers = serveable_file.headers
167
-
168
- assert_equal "application/javascript", headers['Content-Type']
169
- end
170
-
171
- def test_response_body_returns_file_contents_in_array_as_required_by_rack
172
- path = "#{asset_root}/thanks.html"
173
-
174
- serveable_file = ServeableFile.new(
175
- :path => path,
176
- :has_encoding_variants => false
177
- )
178
-
179
- assert_equal [::File.read(path)], serveable_file.response_body
180
- end
181
-
182
- def test_has_encoding_variants_must_be_given_as_constructor_option_to_ensure_vary_encoding_header_can_be_determined
183
- e = assert_raises ArgumentError do
184
- ServeableFile.new({})
185
- end
186
- assert_equal ':has_encoding_variants option must be given', e.message
187
- end
188
-
189
- def test_encoding_variants_returns_true_when_constructor_option_true
190
- serveable_file = ServeableFile.new(:has_encoding_variants => true)
191
- assert serveable_file.encoding_variants?
192
- end
193
-
194
- def test_encoding_variants_returns_false_when_constructor_option_false
195
- serveable_file = ServeableFile.new(:has_encoding_variants => false)
196
- assert !serveable_file.encoding_variants?
197
- end
198
-
199
- def test_gzipped_returns_false_when_constructor_option_false
200
- serveable_file = ServeableFile.new(:is_gzipped => false, :has_encoding_variants => true)
201
- assert !serveable_file.gzipped?
202
- end
203
-
204
- def test_gzipped_returns_true_when_constructor_option_true
205
- serveable_file = ServeableFile.new(:is_gzipped => true, :has_encoding_variants => true)
206
- assert serveable_file.gzipped?
207
- end
208
-
209
- def test_gzipped_defaults_to_false_when_no_constructor_option_given
210
- serveable_file = ServeableFile.new({:has_encoding_variants => true})
211
- assert !serveable_file.gzipped?
212
- end
213
-
214
-
215
- def test_serveable_files_with_same_options_are_equal
216
- file1 = ServeableFile.new :path => "#{asset_root}/hello/world.html.gz",
217
- :full_path_info => '/hello/world.html',
218
- :has_encoding_variants => true,
219
- :is_gzipped => true
220
-
221
- file2 = ServeableFile.new :path => "#{asset_root}/hello/world.html.gz",
222
- :full_path_info => '/hello/world.html',
223
- :has_encoding_variants => true,
224
- :is_gzipped => true
225
-
226
- assert_equal file1, file2
227
- assert_equal file2, file1
228
- assert file1.eql?(file2)
229
- assert file2.eql?(file1)
230
- end
231
-
232
- def test_serveable_files_with_different_paths_are_not_equal
233
- file1 = ServeableFile.new :path => "#{asset_root}/hello/world.html.gz",
234
- :full_path_info => '/hello/world.html',
235
- :has_encoding_variants => true,
236
- :is_gzipped => true
237
-
238
- file2 = ServeableFile.new :path => "#{asset_root}/foo/bar.html.gz",
239
- :full_path_info => '/hello/world.html',
240
- :has_encoding_variants => true,
241
- :is_gzipped => true
242
-
243
- assert_not_equal file1, file2
244
- assert_not_equal file2, file1
245
- assert !file1.eql?(file2)
246
- assert !file2.eql?(file1)
247
- end
248
-
249
- def test_serveable_files_with_different_path_info_are_not_equal
250
- file1 = ServeableFile.new :path => "#{asset_root}/hello/world.html.gz",
251
- :full_path_info => '/hello/world.html',
252
- :has_encoding_variants => true,
253
- :is_gzipped => true
254
-
255
- file2 = ServeableFile.new :path => "#{asset_root}/hello/world.html.gz",
256
- :full_path_info => '/foo/bar.html',
257
- :has_encoding_variants => true,
258
- :is_gzipped => true
259
-
260
- assert_not_equal file1, file2
261
- assert_not_equal file2, file1
262
- assert !file1.eql?(file2)
263
- assert !file2.eql?(file1)
264
- end
265
-
266
- def test_serveable_files_with_different_has_encoding_variants_are_not_equal
267
- file1 = ServeableFile.new :path => "#{asset_root}/hello/world.html.gz",
268
- :full_path_info => '/hello/world.html',
269
- :has_encoding_variants => true,
270
- :is_gzipped => true
271
-
272
- file2 = ServeableFile.new :path => "#{asset_root}/hello/world.html.gz",
273
- :full_path_info => '/hello/world.html',
274
- :has_encoding_variants => false,
275
- :is_gzipped => true
276
-
277
- assert_not_equal file1, file2
278
- assert_not_equal file2, file1
279
- assert !file1.eql?(file2)
280
- assert !file2.eql?(file1)
281
- end
282
-
283
- def test_serveable_files_with_different_is_gzipped_are_not_equal
284
- file1 = ServeableFile.new :path => "#{asset_root}/hello/world.html",
285
- :full_path_info => '/hello/world.html',
286
- :has_encoding_variants => true,
287
- :is_gzipped => false
288
-
289
- file2 = ServeableFile.new :path => "#{asset_root}/hello/world.html",
290
- :full_path_info => '/hello/world.html',
291
- :has_encoding_variants => true,
292
- :is_gzipped => true
293
-
294
- assert_not_equal file1, file2
295
- assert_not_equal file2, file1
296
- assert !file1.eql?(file2)
297
- assert !file2.eql?(file1)
298
- end
299
-
300
- def test_gzipped_serveable_file_does_not_equal_its_non_gzipped_counterpart
301
- gzipped = ServeableFile.new :path => "#{asset_root}/hello/world.html.gz",
302
- :full_path_info => '/hello/world.html',
303
- :has_encoding_variants => true,
304
- :is_gzipped => true
305
-
306
- not_gzipped = ServeableFile.new :path => "#{asset_root}/hello/world.html",
307
- :full_path_info => '/hello/world.html',
308
- :has_encoding_variants => true,
309
- :is_gzipped => false
310
-
311
- assert_not_equal gzipped, not_gzipped
312
- assert_not_equal not_gzipped, gzipped
313
- assert !gzipped.eql?(not_gzipped)
314
- assert !not_gzipped.eql?(gzipped)
315
- end
316
-
317
- def test_find_first_finds_static_file_as_directory
318
- paths = ['/foo/bar.html', '/foo/bar/', '/foo/bar']
319
- paths.each do |path|
320
- serveable_file = ServeableFile.find_first(
321
- :path_info => path,
322
- :asset_compiler => NullAssetCompiler.new,
323
- :asset_root => asset_root,
324
- :include_gzipped => false
325
- )
326
-
327
- assert serveable_file, "ServeableFile should be found for path info '#{path}'"
328
- assert_equal "#{asset_root}/foo/bar.html", serveable_file.path
329
- assert_equal "/foo/bar.html", serveable_file.full_path_info
330
- end
331
- end
332
-
333
- def test_find_first_finds_static_index_in_directory
334
- paths = ['/foo/index.html', '/foo/', '/foo']
335
- paths.each do |path|
336
- serveable_file = ServeableFile.find_first(
337
- :path_info => path,
338
- :asset_compiler => NullAssetCompiler.new,
339
- :asset_root => asset_root,
340
- :include_gzipped => false
341
- )
342
-
343
- assert serveable_file, "ServeableFile should be found for path info '#{path}'"
344
- assert_equal "#{asset_root}/foo/index.html", serveable_file.path
345
- assert_equal "/foo/index.html", serveable_file.full_path_info
346
- end
347
- end
348
-
349
- def test_find_first_finds_static_index_at_root
350
- valid_root_paths = [
351
- '/index.html', '/index', '/', ''
352
- ]
353
- valid_root_paths.each do |root|
354
-
355
- serveable_file = ServeableFile.find_first(
356
- :path_info => root,
357
- :asset_compiler => NullAssetCompiler.new,
358
- :asset_root => asset_root,
359
- :include_gzipped => false
360
- )
361
-
362
- assert serveable_file, "ServeableFile should be found for root path info '#{root}'"
363
- assert_equal "#{asset_root}/index.html", serveable_file.path
364
- assert_equal "/index.html", serveable_file.full_path_info
365
- end
366
- end
367
-
368
- def test_find_first_finds_nothing_for_non_static_extension
369
- assert_nil ServeableFile.find_first(
370
- :path_info => '/about',
371
- :asset_compiler => NullAssetCompiler.new,
372
- :asset_root => asset_root
373
- )
374
- end
375
-
376
- def test_find_first_finds_nothing_for_compileable_path_info
377
- ::Rails.configuration.assets.compile = true
378
-
379
- asset_compiler = RailsAssetCompiler.new
380
-
381
- path_info = '/assets/main.css'
382
-
383
- assert asset_compiler.compiles?(path_info),
384
- "asset compiler should compile '#{path_info}'"
385
-
386
- assert_nil ServeableFile.find_first(
387
- :path_info => path_info,
388
- :asset_compiler => asset_compiler
389
- )
390
- end
391
-
392
- def test_find_first_finds_gzip_variant
393
-
394
- result = ServeableFile.find_first(
395
- :path_info => "/assets/application.css",
396
- :asset_compiler => NullAssetCompiler.new,
397
- :asset_root => asset_root,
398
- :include_gzipped => true
399
- )
400
-
401
- assert_equal "#{asset_root}/assets/application.css.gz", result.path,
402
- "gzipped file variant should be found"
403
- assert_equal "/assets/application.css", result.full_path_info
404
- assert result.encoding_variants?
405
- assert result.gzipped?
406
- end
407
-
408
- def test_find_first_finds_uncompressed_file_when_include_gzipped_false
409
- result = ServeableFile.find_first(
410
- :path_info => "/assets/application.css",
411
- :asset_compiler => NullAssetCompiler.new,
412
- :asset_root => asset_root,
413
- :include_gzipped => false
414
- )
415
-
416
- assert_equal "#{asset_root}/assets/application.css", result.path,
417
- "non-gzipped file variant should be found"
418
- assert_equal "/assets/application.css", result.full_path_info
419
- assert result.encoding_variants?
420
- assert !result.gzipped?
421
- end
422
-
423
- def test_find_first_finds_uncompressed_file_that_has_no_gzip_variant
424
- result = ServeableFile.find_first(
425
- :path_info => "/thanks.html",
426
- :asset_compiler => NullAssetCompiler.new,
427
- :asset_root => asset_root,
428
- :include_gzipped => true
429
- )
430
-
431
- assert_equal "#{asset_root}/thanks.html", result.path
432
- assert_equal "/thanks.html", result.full_path_info
433
- assert !result.encoding_variants?
434
- assert !result.gzipped?
435
- end
436
-
437
- def test_find_first_finds_nothing_for_static_extension_and_non_existent_file
438
- assert_nil ServeableFile.find_first(
439
- :path_info => '/about.html',
440
- :asset_compiler => NullAssetCompiler.new,
441
- :asset_root => asset_root,
442
- :include_gzipped => false
443
- )
444
- end
445
-
446
- def test_has_static_extension_handles_non_lowercase_chars
447
- ['pNG', 'JPEG', 'HTML', 'HtM', 'GIF', 'Ico'].each do |extension|
448
- assert ServeableFile.has_static_extension?("/some-asset.#{extension}")
449
- end
450
- end
451
-
452
- def test_has_static_extension_returns_false_for_asset_paths_without_period
453
- ['/assets/somepng', '/indexhtml', '/assets/applicationcss'].each do |path|
454
- assert !ServeableFile.has_static_extension?(path)
455
- end
456
- end
457
-
458
- def test_has_static_extension_returns_true_for_fonts
459
- font_extensions = ['woff', 'woff2', 'ttf', 'eot', 'otf']
460
- font_extensions.each do |extension|
461
- assert ServeableFile.has_static_extension?("/comic-sans.#{extension}"),
462
- "'#{extension}' font extension not recognized"
463
- end
464
- end
465
-
466
- def test_has_static_extension_returns_true_for_flash
467
- assert ServeableFile.has_static_extension?('/splash-page-like-its-1999.swf'),
468
- "Should handle flash .swf files"
469
- end
470
-
471
- def test_has_static_extension_returns_true_for_configured_extension
472
- Rack::Zippy.static_extensions << 'csv'
473
- assert ServeableFile.has_static_extension?('/static-file.csv'),
474
- "Should handle files with user configured extensions"
475
- end
476
-
477
- private
478
-
479
- def assert_last_modified(headers, expected)
480
- assert_equal expected, headers['Last-Modified']
481
- end
482
-
483
- def assert_cache_friendly_last_modified(headers)
484
- assert_last_modified headers, CACHE_FRIENDLY_LAST_MODIFIED
485
- end
486
-
487
- def assert_cache_max_age(headers, expected_duration)
488
- duration_in_secs = DURATIONS_IN_SECS[expected_duration] || expected_duration
489
- assert_equal "public, max-age=#{duration_in_secs}", headers['Cache-Control']
490
- end
491
-
492
- end
493
- end
494
- end