Skip to content

Commit

Permalink
Monkey patch Rack::Lint to allow string subclass body
Browse files Browse the repository at this point in the history
  • Loading branch information
lifo committed Oct 6, 2009
1 parent f98302e commit 91b61a8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions actionpack/lib/action_controller/integration.rb
@@ -1,6 +1,7 @@
require 'stringio' require 'stringio'
require 'uri' require 'uri'
require 'active_support/test_case' require 'active_support/test_case'
require 'action_controller/rack_lint_patch'


module ActionController module ActionController
module Integration #:nodoc: module Integration #:nodoc:
Expand Down
36 changes: 36 additions & 0 deletions actionpack/lib/action_controller/rack_lint_patch.rb
@@ -0,0 +1,36 @@
# Rack 1.0 does not allow string subclass body. This does not play well with our ActionView::SafeBuffer.
# The next release of Rack will be allowing string subclass body - http://github.com/rack/rack/commit/de668df02802a0335376a81ba709270e43ba9d55
# TODO : Remove this monkey patch after the next release of Rack

module RackLintPatch
module AllowStringSubclass
def self.included(base)
base.send :alias_method, :each, :each_with_hack
end

def each_with_hack
@closed = false

@body.each { |part|
assert("Body yielded non-string value #{part.inspect}") {
part.kind_of?(String)
}
yield part
}

if @body.respond_to?(:to_path)
assert("The file identified by body.to_path does not exist") {
::File.exist? @body.to_path
}
end
end
end

begin
app = proc {|env| [200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, [Class.new(String).new("Hello World!")]] }
response = Rack::MockRequest.new(Rack::Lint.new(app)).get('/')
rescue Rack::Lint::LintError => e
raise(e) unless e.message =~ /Body yielded non-string value/
Rack::Lint.send :include, AllowStringSubclass
end
end
20 changes: 20 additions & 0 deletions actionpack/test/controller/integration_test.rb
Expand Up @@ -443,3 +443,23 @@ def test_failed_get
assert_equal '', response.body assert_equal '', response.body
end end
end end

class StringSubclassBodyTest < ActionController::IntegrationTest
class SafeString < String
end

class SafeStringMiddleware
def self.call(env)
[200, {"Content-Type" => "text/plain", "Content-Length" => "12"}, [SafeString.new("Hello World!")]]
end
end

def setup
@integration_session = ActionController::Integration::Session.new(SafeStringMiddleware)
end

def test_string_subclass_body
get '/'
assert_equal 'Hello World!', response.body
end
end

0 comments on commit 91b61a8

Please sign in to comment.