Fix issues with Content-Length after explicit forward [#159]

This commit is contained in:
Harry Vangberg 2009-02-24 16:43:53 +01:00 committed by Ryan Tomayko
parent d4680f721b
commit 7196ea41c9
2 changed files with 22 additions and 4 deletions

View File

@ -49,7 +49,7 @@ module Sinatra
else
body = @body || []
body = [body] if body.respond_to? :to_str
if header["Content-Length"].nil? && body.respond_to?(:to_ary)
if body.respond_to?(:to_ary)
header["Content-Length"] = body.to_ary.
inject(0) { |len, part| len + part.bytesize }.to_s
end
@ -345,9 +345,9 @@ module Sinatra
invoke { error_block!(response.status) }
# never respond with a body on HEAD requests
@response.body = [] if @env['REQUEST_METHOD'] == 'HEAD'
@response.finish
status, header, body = @response.finish
body = [] if @env['REQUEST_METHOD'] == 'HEAD'
[status, header, body]
end
# Access options defined with Base.set.

View File

@ -108,5 +108,23 @@ describe "Sinatra::Base as Rack middleware" do
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal 'Hello after explicit forward', response.body
assert_equal '28', response['Content-Length']
end
app_content_length = lambda {|env|
[200, {'Content-Length' => '16'}, 'From downstream!']}
class TestMiddlewareContentLength < Sinatra::Base
get '/forward' do
res = forward
'From after explicit forward!'
end
end
middleware_content_length = TestMiddlewareContentLength.new(app_content_length)
request_content_length = Rack::MockRequest.new(middleware_content_length)
it "sets content length for last response" do
response = request_content_length.get('/forward')
assert_equal '28', response['Content-Length']
end
end