assets_test.rb 3.9 KB
Newer Older
1
require 'isolation/abstract_unit'
2
require 'active_support/core_ext/kernel/reporting'
3 4 5
require 'rack/test'

module ApplicationTests
6
  class AssetsTest < Test::Unit::TestCase
7 8 9 10
    include ActiveSupport::Testing::Isolation
    include Rack::Test::Methods

    def setup
11
      build_app(:initializers => true)
12 13 14
      boot_rails
    end

15 16 17 18
    def teardown
      teardown_app
    end

J
José Valim 已提交
19 20 21 22
    def app
      @app ||= Rails.application
    end

23 24 25 26 27 28 29 30 31
    test "assets routes have higher priority" do
      app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"

      app_file 'config/routes.rb', <<-RUBY
        AppTemplate::Application.routes.draw do
          match '*path', :to => lambda { |env| [200, { "Content-Type" => "text/html" }, "Not an asset"] }
        end
      RUBY

32 33
      require "#{app_path}/config/environment"

34 35 36
      get "/assets/demo.js"
      assert_match "alert()", last_response.body
    end
J
José Valim 已提交
37

J
José Valim 已提交
38 39 40 41 42 43 44 45 46 47 48
    test "assets do not require compressors until it is used" do
      app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"
      ENV["RAILS_ENV"] = "production"
      require "#{app_path}/config/environment"

      assert !defined?(Uglifier)
      get "/assets/demo.js"
      assert_match "alert()", last_response.body
      assert defined?(Uglifier)
    end

49
    test "precompile creates the file and gives it the original asset's content" do
50
      app_file "app/assets/javascripts/application.js", "alert();"
51
      app_file "app/assets/javascripts/foo/application.js", "alert();"
52 53 54 55

      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end
56 57 58 59 60 61
      files = Dir["#{app_path}/public/assets/application-*.js"]
      files << Dir["#{app_path}/public/assets/foo/application-*.js"].first
      files.each do |file|
        assert_not_nil file, "Expected application.js asset to be generated, but none found"
        assert_equal "alert();\n", File.read(file)
      end
62 63
    end

64
    test "precompile appends the md5 hash to files referenced with asset_path" do
65 66 67 68 69 70 71 72 73
      app_file "app/assets/stylesheets/application.css.erb", "<%= asset_path('rails.png') %>"

      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:precompile` }
      end
      file = Dir["#{app_path}/public/assets/application-*.css"].first
      assert_match /\/assets\/rails-([0-z]+)\.png/, File.read(file)
    end

74 75 76 77 78 79 80 81 82 83 84 85 86
    test "assets are cleaned up properly" do
      app_file "public/assets/application.js", "alert();"
      app_file "public/assets/application.css", "a { color: green; }"
      app_file "public/assets/subdir/broken.png", "not really an image file"

      capture(:stdout) do
        Dir.chdir(app_path){ `bundle exec rake assets:clean` }
      end

      files = Dir["#{app_path}/public/assets/**/*"]
      assert_equal 0, files.length, "Expected no assets, but found #{files.join(', ')}"
    end

J
José Valim 已提交
87
    test "does not stream session cookies back" do
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
      app_file "app/assets/javascripts/demo.js.erb", "<%= :alert %>();"

      app_file "config/routes.rb", <<-RUBY
        AppTemplate::Application.routes.draw do
          match '/omg', :to => "omg#index"
        end
      RUBY

      require "#{app_path}/config/environment"

      class ::OmgController < ActionController::Base
        def index
          flash[:cool_story] = true
          render :text => "ok"
        end
      end

      get "/omg"
      assert_equal 'ok', last_response.body

      get "/assets/demo.js"
      assert_match "alert()", last_response.body
      assert_equal nil, last_response.headers["Set-Cookie"]
J
José Valim 已提交
111
    end
112 113 114 115 116 117 118 119 120 121 122 123 124 125

    test "files in any assets/ directories are not added to Sprockets" do
      %w[app lib vendor].each do |dir|
        app_file "#{dir}/assets/#{dir}_test.erb", "testing"
      end

      app_file "app/assets/javascripts/demo.js", "alert();"

      require "#{app_path}/config/environment"

      get "/assets/demo.js"
      assert_match "alert();", last_response.body
      assert_equal 200, last_response.status
    end
126 127
  end
end