Skip to content

Instantly share code, notes, and snippets.

@vangberg
Created February 24, 2009 10:09
Show Gist options
  • Save vangberg/69493 to your computer and use it in GitHub Desktop.
Save vangberg/69493 to your computer and use it in GitHub Desktop.
diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb
index 1d0cbe6..46d9d7b 100644
--- a/lib/sinatra/base.rb
+++ b/lib/sinatra/base.rb
@@ -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
diff --git a/test/base_test.rb b/test/base_test.rb
index cdddb5f..fae3bc7 100644
--- a/test/base_test.rb
+++ b/test/base_test.rb
@@ -109,4 +109,21 @@ describe "Sinatra::Base as Rack middleware" do
assert_equal 'true', response['X-Downstream']
assert_equal 'Hello after explicit forward', response.body
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 from last upstream body" do
+ response = request_content_length.get('/forward')
+ assert_equal '28', response['Content-Length']
+ end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment