do not rely on to_ary, restores compatibility with Rails 3.1, fixes #325 and #326

This commit is contained in:
Konstantin Haase 2011-07-12 10:41:26 +02:00
parent 78784f9391
commit 5558cc2c88
2 changed files with 14 additions and 5 deletions

View File

@ -63,8 +63,8 @@ module Sinatra
# http://rack.rubyforge.org/doc/classes/Rack/Response/Helpers.html
class Response < Rack::Response
def body=(value)
value = value.body while value.respond_to? :body and value.body != value
@body = value.respond_to?(:to_str) ? [value.to_str] : value
value = value.body while Rack::Response === value
@body = String === value ? [value.to_str] : value
end
def each
@ -75,7 +75,7 @@ module Sinatra
if status.to_i / 100 == 1
headers.delete "Content-Length"
headers.delete "Content-Type"
elsif body.respond_to? :to_ary and not [204, 304].include?(status.to_i)
elsif Array === body and not [204, 304].include?(status.to_i)
headers["Content-Length"] = body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s
end
super
@ -614,7 +614,7 @@ module Sinatra
invoke { error_block!(response.status) }
unless @response['Content-Type']
if body.respond_to? :to_ary and body[0].respond_to? :content_type
if Array === body and body[0].respond_to? :content_type
content_type body[0].content_type
else
content_type :html
@ -700,7 +700,7 @@ module Sinatra
# Revert params afterwards.
#
# Returns pass block.
def process_route(pattern, keys, conditions, block = nil)
def process_route(pattern, keys, conditions, block = nil, values = nil)
@original_params ||= @params
route = @request.path_info
route = '/' if route.empty? and not settings.empty_path_info?

View File

@ -40,6 +40,15 @@ class ResponseTest < Test::Unit::TestCase
assert_equal @response.body, body.body
end
it 'does not call #to_ary or #inject on the body' do
object = Object.new
def object.inject(*) fail 'called' end
def object.to_ary(*) fail 'called' end
def object.each(*) end
@response.body = object
assert @response.finish
end
it 'does not nest a Sinatra::Response' do
@response.body = Sinatra::Response.new ["foo"]
assert_equal @response.body, ["foo"]