Permalink
Browse files

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

  • Loading branch information...
1 parent d4680f7 commit 7196ea41c9fb7e07ea192b1bd5e6c4e4e98ef7da @vangberg vangberg committed with rtomayko Feb 24, 2009
Showing with 22 additions and 4 deletions.
  1. +4 −4 lib/sinatra/base.rb
  2. +18 −0 test/base_test.rb
View
@@ -49,7 +49,7 @@ def finish
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 @@ def call!(env)
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
@@ -108,5 +108,23 @@ class TestMiddleware < Sinatra::Base
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

0 comments on commit 7196ea4

Please sign in to comment.