提交 0c68d23f 编写于 作者: J John Duff 提交者: Joshua Peek

make pass through error code configurable [#2817 state:resolved]

Signed-off-by: NJoshua Peek <josh@joshpeek.com>
上级 272c504f
......@@ -11,6 +11,9 @@ class Metal
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 @@ def self.metals
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 @@ def initialize(app)
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
......
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
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
......@@ -55,8 +55,38 @@ def test_metal_finding_should_work_with_multiple_metal_paths_in_185_and_below
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}"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册