From 5ddc597066603875b4e59c811d0d78c84842111c Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 10 Jan 2010 18:13:43 -0800 Subject: Use safe_concat instead of trying to html_safe! what might be a frozen string --- actionpack/lib/action_controller/caching/fragments.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/actionpack/lib/action_controller/caching/fragments.rb b/actionpack/lib/action_controller/caching/fragments.rb index 806e6a1750..00a7f034d3 100644 --- a/actionpack/lib/action_controller/caching/fragments.rb +++ b/actionpack/lib/action_controller/caching/fragments.rb @@ -36,8 +36,8 @@ module ActionController #:nodoc: def fragment_for(buffer, name = {}, options = nil, &block) #:nodoc: if perform_caching - if fragment_exist?(name,options) - buffer.concat(read_fragment(name, options).html_safe!) + if fragment_exist?(name, options) + buffer.safe_concat(read_fragment(name, options)) else pos = buffer.length block.call -- cgit v1.2.3 From 2c2b84f93c6eb1a170467c1340a4eeddfa13f7da Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 10 Jan 2010 18:25:17 -0800 Subject: Javascript tests expects the old default of escaping HTML, make it so for now --- actionpack/test/template/javascript_helper_test.rb | 5 +++++ actionpack/test/template/prototype_helper_test.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index f0f686f6e2..03caad3d46 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -13,8 +13,13 @@ class JavaScriptHelperTest < ActionView::TestCase def setup super + ActiveSupport.escape_html_entities_in_json = true @template = self end + + def teardown + ActiveSupport.escape_html_entities_in_json = false + end def _evaluate_assigns_and_ivars() end diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 313a769088..9225153798 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -317,6 +317,11 @@ class JavaScriptGeneratorTest < PrototypeHelperBaseTest def setup super @generator = create_generator + ActiveSupport.escape_html_entities_in_json = true + end + + def teardown + ActiveSupport.escape_html_entities_in_json = false end def _evaluate_assigns_and_ivars() end -- cgit v1.2.3 From 39215860912e4a29def2973b684d0830fc8b9904 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 10 Jan 2010 22:33:34 -0600 Subject: Rewrite Metal tests --- railties/test/application/metal_test.rb | 86 ++++++++++++++++++ .../metal/multiplemetals/app/metal/metal_a.rb | 5 - .../metal/multiplemetals/app/metal/metal_b.rb | 5 - .../metal/pluralmetal/app/metal/legacy_routes.rb | 5 - .../metal/singlemetal/app/metal/foo_metal.rb | 5 - .../metal/subfolders/app/metal/Folder/metal_a.rb | 7 -- .../metal/subfolders/app/metal/Folder/metal_b.rb | 7 -- railties/test/metal_test.rb | 101 --------------------- 8 files changed, 86 insertions(+), 135 deletions(-) create mode 100644 railties/test/application/metal_test.rb delete mode 100644 railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb delete mode 100644 railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb delete mode 100644 railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb delete mode 100644 railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb delete mode 100644 railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb delete mode 100644 railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb delete mode 100644 railties/test/metal_test.rb diff --git a/railties/test/application/metal_test.rb b/railties/test/application/metal_test.rb new file mode 100644 index 0000000000..d3dfb79e8a --- /dev/null +++ b/railties/test/application/metal_test.rb @@ -0,0 +1,86 @@ +require 'isolation/abstract_unit' + +module ApplicationTests + class MetalTest < Test::Unit::TestCase + include ActiveSupport::Testing::Isolation + + def setup + build_app + boot_rails + + require 'rack/test' + extend Rack::Test::Methods + end + + def app + @app ||= begin + require "#{app_path}/config/environment" + Rails.application + end + end + + test "single metal endpoint" do + app_file 'app/metal/foo_metal.rb', <<-RUBY + class FooMetal + def self.call(env) + [200, { "Content-Type" => "text/html"}, ["FooMetal"]] + end + end + RUBY + + get "/" + assert_equal 200, last_response.status + assert_equal "FooMetal", last_response.body + end + + test "multiple metal endpoints" do + app_file 'app/metal/metal_a.rb', <<-RUBY + class MetalA + def self.call(env) + [404, { "Content-Type" => "text/html"}, ["Metal A"]] + end + end + RUBY + + app_file 'app/metal/metal_b.rb', <<-RUBY + class MetalB + def self.call(env) + [200, { "Content-Type" => "text/html"}, ["Metal B"]] + end + end + RUBY + + get "/" + assert_equal 200, last_response.status + assert_equal "Metal B", last_response.body + end + + test "pass through to application" do + app_file 'app/metal/foo_metal.rb', <<-RUBY + class FooMetal + def self.call(env) + [404, { "Content-Type" => "text/html"}, ["Not Found"]] + end + end + RUBY + + controller :foo, <<-RUBY + class FooController < ActionController::Base + def index + render :text => "foo" + end + end + RUBY + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do |map| + match ':controller(/:action)' + end + RUBY + + get "/foo" + assert_equal 200, last_response.status + assert_equal "foo", last_response.body + end + end +end diff --git a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb deleted file mode 100644 index 4ca4ddd447..0000000000 --- a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb +++ /dev/null @@ -1,5 +0,0 @@ -class MetalA - def self.call(env) - [404, { "Content-Type" => "text/html"}, ["Metal A"]] - end -end diff --git a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb deleted file mode 100644 index 80e69fe0b0..0000000000 --- a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb +++ /dev/null @@ -1,5 +0,0 @@ -class MetalB - def self.call(env) - [200, { "Content-Type" => "text/html"}, ["Metal B"]] - end -end diff --git a/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb b/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb deleted file mode 100644 index 0cd3737c32..0000000000 --- a/railties/test/fixtures/metal/pluralmetal/app/metal/legacy_routes.rb +++ /dev/null @@ -1,5 +0,0 @@ -class LegacyRoutes < Rails::Rack::Metal - def self.call(env) - [301, { "Location" => "http://example.com"}, []] - end -end diff --git a/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb b/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb deleted file mode 100644 index 5f5b087592..0000000000 --- a/railties/test/fixtures/metal/singlemetal/app/metal/foo_metal.rb +++ /dev/null @@ -1,5 +0,0 @@ -class FooMetal < Rails::Rack::Metal - def self.call(env) - [200, { "Content-Type" => "text/html"}, ["Hi"]] - end -end diff --git a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb b/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb deleted file mode 100644 index 25b3bb0abc..0000000000 --- a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_a.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Folder - class MetalA < Rails::Rack::Metal - def self.call(env) - [200, { "Content-Type" => "text/html"}, ["Hi"]] - end - end -end diff --git a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb b/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb deleted file mode 100644 index 7583363f71..0000000000 --- a/railties/test/fixtures/metal/subfolders/app/metal/Folder/metal_b.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Folder - class MetalB < Rails::Rack::Metal - def self.call(env) - [200, { "Content-Type" => "text/html"}, ["Hi"]] - end - end -end diff --git a/railties/test/metal_test.rb b/railties/test/metal_test.rb deleted file mode 100644 index 91f55c2b5e..0000000000 --- a/railties/test/metal_test.rb +++ /dev/null @@ -1,101 +0,0 @@ -require 'abstract_unit' - -class MetalTest < Test::Unit::TestCase - def test_metals_should_return_list_of_found_metal_apps - use_appdir("singlemetal") do - assert_equal(["FooMetal"], found_metals_as_string_array) - end - end - - def test_metals_should_respect_class_name_conventions - use_appdir("pluralmetal") do - assert_equal(["LegacyRoutes"], found_metals_as_string_array) - end - end - - def test_metals_should_return_alphabetical_list_of_found_metal_apps - use_appdir("multiplemetals") do - assert_equal(["MetalA", "MetalB"], found_metals_as_string_array) - end - end - - def test_metals_load_order_should_be_overriden_by_requested_metals - use_appdir("multiplemetals") do - Rails::Rack::Metal.requested_metals = ["MetalB", "MetalA"] - assert_equal(["MetalB", "MetalA"], found_metals_as_string_array) - end - end - - def test_metals_not_listed_should_not_load - use_appdir("multiplemetals") do - Rails::Rack::Metal.requested_metals = ["MetalB"] - assert_equal(["MetalB"], found_metals_as_string_array) - end - end - - def test_metal_finding_should_work_with_subfolders - use_appdir("subfolders") do - assert_equal(["Folder::MetalA", "Folder::MetalB"], found_metals_as_string_array) - end - end - - def test_metal_finding_with_requested_metals_should_work_with_subfolders - use_appdir("subfolders") do - Rails::Rack::Metal.requested_metals = ["Folder::MetalB"] - assert_equal(["Folder::MetalB"], found_metals_as_string_array) - end - end - - def test_metal_finding_should_work_with_multiple_metal_paths_in_185_and_below - use_appdir("singlemetal") do - engine_metal_path = "#{File.dirname(__FILE__)}/fixtures/plugins/engines/engine/app/metal" - Rails::Rack::Metal.metal_paths << engine_metal_path - $LOAD_PATH << engine_metal_path - assert_equal(["FooMetal", "EngineMetal"], found_metals_as_string_array) - end - end - - def test_metal_default_pass_through_on_404 - use_appdir("multiplemetals") do - result = Rails::Rack::Metal.new(app).call({}) - assert_equal 200, result.first - assert_equal ["Metal B"], result.last - end - end - - def test_metal_pass_through_on_417 - use_appdir("multiplemetals") do - Rails::Rack::Metal.pass_through_on = 417 - result = Rails::Rack::Metal.new(app).call({}) - assert_equal 404, result.first - assert_equal ["Metal A"], result.last - end - end - - def test_metal_pass_through_on_404_and_200 - use_appdir("multiplemetals") do - Rails::Rack::Metal.pass_through_on = [404, 200] - result = Rails::Rack::Metal.new(app).call({}) - assert_equal 402, result.first - assert_equal ["End of the Line"], result.last - end - end - - private - - def app - lambda{|env|[402,{},["End of the Line"]]} - end - - def use_appdir(root) - dir = "#{File.dirname(__FILE__)}/fixtures/metal/#{root}" - Rails::Rack::Metal.metal_paths = ["#{dir}/app/metal"] - Rails::Rack::Metal.requested_metals = nil - $LOAD_PATH << "#{dir}/app/metal" - yield - end - - def found_metals_as_string_array - Rails::Rack::Metal.metals.map { |m| m.to_s } - end -end -- cgit v1.2.3 From 02bbde4e78f958d5495c19d953aaec58bb8ef998 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Sun, 10 Jan 2010 23:09:10 -0600 Subject: Cleanup junk metal and revise API API Change: Returning a "X-Cascade: pass" header triggers the cascade instead of a 404 response. --- actionpack/lib/action_controller/railtie.rb | 28 +++++++++++-- railties/lib/rails/rack.rb | 1 - railties/lib/rails/rack/metal.rb | 59 ---------------------------- railties/test/application/metal_test.rb | 4 +- railties/test/application/middleware_test.rb | 1 + 5 files changed, 28 insertions(+), 65 deletions(-) delete mode 100644 railties/lib/rails/rack/metal.rb diff --git a/actionpack/lib/action_controller/railtie.rb b/actionpack/lib/action_controller/railtie.rb index 6b94c32b2a..42257f9400 100644 --- a/actionpack/lib/action_controller/railtie.rb +++ b/actionpack/lib/action_controller/railtie.rb @@ -37,11 +37,33 @@ module ActionController ActionController::Base.view_paths = view_path if ActionController::Base.view_paths.blank? end + class MetalMiddlewareBuilder + def initialize(metals) + @metals = metals + end + + def new(app) + ActionDispatch::Cascade.new(@metals, app) + end + + def name + ActionDispatch::Cascade.name + end + alias_method :to_s, :name + end + initializer "action_controller.initialize_metal" do |app| - Rails::Rack::Metal.requested_metals = app.config.metals + metal_root = "#{Rails.root}/app/metal" + load_list = app.config.metals || Dir["#{metal_root}/**/*.rb"] + + metals = load_list.map { |metal| + metal = File.basename(metal.gsub("#{metal_root}/", ''), '.rb') + require_dependency metal + metal.camelize.constantize + }.compact - app.config.middleware.insert_before(:"ActionDispatch::ParamsParser", - Rails::Rack::Metal, :if => Rails::Rack::Metal.metals.any?) + middleware = MetalMiddlewareBuilder.new(metals) + app.config.middleware.insert_before(:"ActionDispatch::ParamsParser", middleware) end # # Prepare dispatcher callbacks and run 'prepare' callbacks diff --git a/railties/lib/rails/rack.rb b/railties/lib/rails/rack.rb index 9705f65e52..d487bd0542 100644 --- a/railties/lib/rails/rack.rb +++ b/railties/lib/rails/rack.rb @@ -2,7 +2,6 @@ module Rails module Rack autoload :Debugger, "rails/rack/debugger" autoload :LogTailer, "rails/rack/log_tailer" - autoload :Metal, "rails/rack/metal" autoload :Static, "rails/rack/static" end end diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb deleted file mode 100644 index 6c0732f732..0000000000 --- a/railties/lib/rails/rack/metal.rb +++ /dev/null @@ -1,59 +0,0 @@ -require 'active_support/ordered_hash' -require 'active_support/core_ext/class/attribute_accessors' -require 'active_support/dependencies' - -module Rails - module Rack - class Metal - NotFoundResponse = [404, {}, []].freeze - NotFound = lambda { NotFoundResponse } - - cattr_accessor :metal_paths - self.metal_paths = ["#{Rails.root}/app/metal"] - cattr_accessor :requested_metals - - cattr_accessor :pass_through_on - self.pass_through_on = 404 - - def self.metals - matcher = /#{Regexp.escape('/app/metal/')}(.*)\.rb\Z/ - metal_glob = metal_paths.map{ |base| "#{base}/**/*.rb" } - all_metals = {} - - metal_glob.each do |glob| - Dir[glob].sort.map do |file| - file = file.match(matcher)[1] - all_metals[file.camelize] = file - end - end - - load_list = requested_metals || all_metals.keys - - load_list.map do |requested_metal| - if metal = all_metals[requested_metal] - require_dependency metal - requested_metal.constantize - end - end.compact - end - - def initialize(app) - @app = app - @pass_through_on = {} - [*self.class.pass_through_on].each { |status| @pass_through_on[status] = true } - - @metals = ActiveSupport::OrderedHash.new - self.class.metals.each { |app| @metals[app] = true } - freeze - end - - def call(env) - @metals.keys.each do |app| - result = app.call(env) - return result unless @pass_through_on.include?(result[0].to_i) - end - @app.call(env) - end - end - end -end diff --git a/railties/test/application/metal_test.rb b/railties/test/application/metal_test.rb index d3dfb79e8a..225bede117 100644 --- a/railties/test/application/metal_test.rb +++ b/railties/test/application/metal_test.rb @@ -37,7 +37,7 @@ module ApplicationTests app_file 'app/metal/metal_a.rb', <<-RUBY class MetalA def self.call(env) - [404, { "Content-Type" => "text/html"}, ["Metal A"]] + [404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Metal A"]] end end RUBY @@ -59,7 +59,7 @@ module ApplicationTests app_file 'app/metal/foo_metal.rb', <<-RUBY class FooMetal def self.call(env) - [404, { "Content-Type" => "text/html"}, ["Not Found"]] + [404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Not Found"]] end end RUBY diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index 397968a4e7..8ba3a7bdfc 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -20,6 +20,7 @@ module ApplicationTests "ActionDispatch::ShowExceptions", "ActionDispatch::Callbacks", "ActionDispatch::Session::CookieStore", + "ActionDispatch::Cascade", "ActionDispatch::ParamsParser", "Rack::MethodOverride", "Rack::Head", -- cgit v1.2.3