spec/plugin/assets_spec.rb in roda-2.3.0 vs spec/plugin/assets_spec.rb in roda-2.4.0
- old
+ new
@@ -28,11 +28,13 @@
plugin :assets,
:css => ['app.scss', 'raw.css'],
:js => { :head => ['app.js'] },
:path => 'spec/assets',
:public => 'spec',
- :css_opts => {:cache=>false}
+ :css_opts => {:cache=>false},
+ :css_compressor => :none,
+ :js_compressor => :none
route do |r|
r.assets
r.is 'test' do
@@ -49,10 +51,14 @@
after(:all) do
FileUtils.rm_r('spec/assets/tmp') if File.directory?('spec/assets/tmp')
FileUtils.rm_r('spec/public') if File.directory?('spec/public')
end
+ def gunzip(body)
+ Zlib::GzipReader.wrap(StringIO.new(body), &:read)
+ end
+
it 'assets_opts should use correct paths given options' do
fpaths = [:js_path, :css_path, :compiled_js_path, :compiled_css_path]
rpaths = [:js_prefix, :css_prefix, :compiled_js_prefix, :compiled_css_prefix]
app.assets_opts.values_at(*fpaths).must_equal %w"spec/assets/js/ spec/assets/css/ spec/assets/app spec/assets/app".map{|s| File.join(Dir.pwd, s)}
app.assets_opts.values_at(*rpaths).must_equal %w"assets/js/ assets/css/ assets/app assets/app"
@@ -222,18 +228,61 @@
css.must_match(/color: red;/)
css2.must_match(/color: blue;/)
js.must_include('console.log')
end
+ it 'should handle compressing using different libraries' do
+ try_compressor = proc do |css, js|
+ app.plugin :assets, :css_compressor=>css, :js_compressor=>js
+ begin
+ app.compile_assets
+ rescue LoadError, Roda::RodaPlugins::Assets::CompressorNotFound
+ next
+ end
+ File.read("spec/assets/app.#{app.assets_opts[:compiled]['css']}.css").must_match(/color: ?blue/)
+ File.read("spec/assets/app.head.#{app.assets_opts[:compiled]['js.head']}.js").must_include('console.log')
+ end
+
+ try_compressor.call(nil, nil)
+ try_compressor.call(:yui, :yui)
+ try_compressor.call(:none, :closure)
+ try_compressor.call(:none, :uglifier)
+ try_compressor.call(:none, :minjs)
+ end
+
it 'should handle compiling assets, linking to them, and accepting requests for them' do
app.compile_assets
html = body('/test')
html.scan(/<link/).length.must_equal 1
html =~ %r{href="(/assets/app\.[a-f0-9]{40}\.css)"}
css = body($1)
html.scan(/<script/).length.must_equal 1
html =~ %r{src="(/assets/app\.head\.[a-f0-9]{40}\.js)"}
js = body($1)
+ css.must_match(/color: ?red/)
+ css.must_match(/color: ?blue/)
+ js.must_include('console.log')
+ end
+
+ it 'should handle compiling assets, linking to them, and accepting requests for them when :gzip is set' do
+ app.plugin :assets, :gzip=>true
+ app.compile_assets
+ html = body('/test')
+ html.scan(/<link/).length.must_equal 1
+ html =~ %r{href="(/assets/app\.[a-f0-9]{40}\.css)"}
+ css_path = $1
+ html.scan(/<script/).length.must_equal 1
+ html =~ %r{src="(/assets/app\.head\.[a-f0-9]{40}\.js)"}
+ js_path = $1
+
+ css = body(css_path)
+ js = body(js_path)
+ css.must_match(/color: ?red/)
+ css.must_match(/color: ?blue/)
+ js.must_include('console.log')
+
+ css = gunzip(body(css_path, 'HTTP_ACCEPT_ENCODING'=>'deflate, gzip'))
+ js = gunzip(body(js_path, 'HTTP_ACCEPT_ENCODING'=>'deflate, gzip'))
css.must_match(/color: ?red/)
css.must_match(/color: ?blue/)
js.must_include('console.log')
end