spec/plugin/assets_spec.rb in roda-2.16.0 vs spec/plugin/assets_spec.rb in roda-2.17.0

- old
+ new

@@ -52,10 +52,11 @@ File.delete(metadata_file) if File.file?(metadata_file) end 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') + FileUtils.rm(Dir['spec/assets/app.*.{js,css}*']) end def gunzip(body) Zlib::GzipReader.wrap(StringIO.new(body), &:read) end @@ -264,10 +265,35 @@ css.must_match(/color: ?red/) css.must_match(/color: ?blue/) js.must_include('console.log') end + [[:sha256, 64, 44], [:sha384, 96, 64], [:sha512, 128, 88]].each do |algo, hex_length, base64_length| + it 'should handle :sri option for subresource integrity when compiling assets' do + app.plugin :assets, :sri=>algo + app.compile_assets + html = body('/test') + html.scan(/<link/).length.must_equal 1 + html =~ %r|integrity="#{algo}-([^"]+)" />| + css_hash = $1.gsub('&#x2F;', '/') + css_hash.length.must_equal base64_length + html =~ %r|href="(/assets/app\.[a-f0-9]{#{hex_length}}\.css)"| + css = body($1) + [Digest.const_get(algo.to_s.upcase).digest(css)].pack('m').tr("\n", "").must_equal css_hash + html.scan(/<script/).length.must_equal 1 + html =~ %r|integrity="#{algo}-([^"]+)"></script>| + js_hash = $1.gsub('&#x2F;', '/') + js_hash.length.must_equal base64_length + html =~ %r|src="(/assets/app\.head\.[a-f0-9]{#{hex_length}}\.js)"| + js = body($1) + [Digest.const_get(algo.to_s.upcase).digest(js)].pack('m').tr("\n", "").must_equal js_hash + css.must_match(/color: ?red/) + css.must_match(/color: ?blue/) + js.must_include('console.log') + end + end + it 'should handle linking to compiled assets when a compiled asset host is used' do app.plugin :assets, :compiled_asset_host=>'https://cdn.example.com' app.compile_assets html = body('/test') html.scan(/<link/).length.must_equal 1 @@ -517,17 +543,41 @@ it 'should not add routes for empty asset types' do app.plugin :assets, :css=>nil a = app::RodaRequest.assets_matchers a.length.must_equal 1 a.first.length.must_equal 2 - a.first.first.must_equal 'js' + a.first.first.must_equal :js 'assets/js/head/app.js'.must_match a.first.last 'assets/js/head/app2.js'.wont_match a.first.last end it 'should not add routes if no asset types' do app.plugin :assets, :js=>nil, :css=>nil app::RodaRequest.assets_matchers.must_equal [] + end + + it 'should support :postprocessor option' do + postprocessor = lambda do |file, type, content| + "file=#{file} type=#{type} tc=#{type.class} #{content.sub('color', 'font')}" + end + + app.plugin :assets, :path=>'spec', :js_dir=>nil, :css_dir=>nil, :prefix=>nil, + :postprocessor=>postprocessor, + :css=>{:assets=>{:css=>%w'app.scss'}} + app.route do |r| + r.assets + r.is 'test' do + "#{assets([:css, :assets, :css])}" + end + end + html = body('/test') + html.scan(/<link/).length.must_equal 1 + html =~ %r{href="(/assets/css/app\.scss)"} + css = body($1) + css.must_match(/app\.scss/) + css.must_match(/type=css/) + css.must_match(/tc=Symbol/) + css.must_match(/font: red;/) end it 'should support :precompiled option' do app.plugin :assets, :precompiled=>metadata_file File.exist?(metadata_file).must_equal false