mirror of
https://github.com/sinatra/sinatra
synced 2023-03-27 23:18:01 -04:00
Fix issues with Content-Length after explicit forward [#159]
This commit is contained in:
parent
d4680f721b
commit
7196ea41c9
2 changed files with 22 additions and 4 deletions
|
@ -49,7 +49,7 @@ module Sinatra
|
||||||
else
|
else
|
||||||
body = @body || []
|
body = @body || []
|
||||||
body = [body] if body.respond_to? :to_str
|
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.
|
header["Content-Length"] = body.to_ary.
|
||||||
inject(0) { |len, part| len + part.bytesize }.to_s
|
inject(0) { |len, part| len + part.bytesize }.to_s
|
||||||
end
|
end
|
||||||
|
@ -345,9 +345,9 @@ module Sinatra
|
||||||
invoke { error_block!(response.status) }
|
invoke { error_block!(response.status) }
|
||||||
|
|
||||||
# never respond with a body on HEAD requests
|
# never respond with a body on HEAD requests
|
||||||
@response.body = [] if @env['REQUEST_METHOD'] == 'HEAD'
|
status, header, body = @response.finish
|
||||||
|
body = [] if @env['REQUEST_METHOD'] == 'HEAD'
|
||||||
@response.finish
|
[status, header, body]
|
||||||
end
|
end
|
||||||
|
|
||||||
# Access options defined with Base.set.
|
# Access options defined with Base.set.
|
||||||
|
|
|
@ -108,5 +108,23 @@ describe "Sinatra::Base as Rack middleware" do
|
||||||
assert_equal 210, response.status
|
assert_equal 210, response.status
|
||||||
assert_equal 'true', response['X-Downstream']
|
assert_equal 'true', response['X-Downstream']
|
||||||
assert_equal 'Hello after explicit forward', response.body
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue