Created
June 19, 2009 02:32
-
-
Save jduff/132369 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From f6b4095185f4aff912b3bda59033ed2d259da735 Mon Sep 17 00:00:00 2001 | |
From: John Duff <[email protected]> | |
Date: Thu, 18 Jun 2009 22:24:00 -0400 | |
Subject: [PATCH] make pass through error code configurable | |
--- | |
railties/lib/rails/rack/metal.rb | 8 ++++- | |
.../metal/multiplemetals/app/metal/metal_a.rb | 4 +- | |
.../metal/multiplemetals/app/metal/metal_b.rb | 4 +- | |
railties/test/metal_test.rb | 30 ++++++++++++++++++++ | |
4 files changed, 41 insertions(+), 5 deletions(-) | |
diff --git a/railties/lib/rails/rack/metal.rb b/railties/lib/rails/rack/metal.rb | |
index b031be2..6c0732f 100644 | |
--- a/railties/lib/rails/rack/metal.rb | |
+++ b/railties/lib/rails/rack/metal.rb | |
@@ -11,6 +11,9 @@ module Rails | |
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/ | |
@@ -36,6 +39,9 @@ module Rails | |
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 | |
@@ -44,7 +50,7 @@ module Rails | |
def call(env) | |
@metals.keys.each do |app| | |
result = app.call(env) | |
- return result unless result[0].to_i == 404 | |
+ return result unless @pass_through_on.include?(result[0].to_i) | |
end | |
@app.call(env) | |
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 | |
index 2d373ce..4ca4ddd 100644 | |
--- a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb | |
+++ b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_a.rb | |
@@ -1,5 +1,5 @@ | |
-class MetalA < Rails::Rack::Metal | |
+class MetalA | |
def self.call(env) | |
- [200, { "Content-Type" => "text/html"}, ["Hi"]] | |
+ [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 | |
index a8bbf3f..80e69fe 100644 | |
--- a/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb | |
+++ b/railties/test/fixtures/metal/multiplemetals/app/metal/metal_b.rb | |
@@ -1,5 +1,5 @@ | |
-class MetalB < Rails::Rack::Metal | |
+class MetalB | |
def self.call(env) | |
- [200, { "Content-Type" => "text/html"}, ["Hi"]] | |
+ [200, { "Content-Type" => "text/html"}, ["Metal B"]] | |
end | |
end | |
diff --git a/railties/test/metal_test.rb b/railties/test/metal_test.rb | |
index d3d2311..c79a819 100644 | |
--- a/railties/test/metal_test.rb | |
+++ b/railties/test/metal_test.rb | |
@@ -55,8 +55,38 @@ class MetalTest < Test::Unit::TestCase | |
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{[402,{},["End of the Line"]]} | |
+ end | |
def use_appdir(root) | |
dir = "#{File.dirname(__FILE__)}/fixtures/metal/#{root}" | |
-- | |
1.6.1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment