Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Follow Rack protocol, closes #325 #326

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 15 additions & 7 deletions lib/sinatra/base.rb
Expand Up @@ -75,8 +75,10 @@ def finish
if status.to_i / 100 == 1 if status.to_i / 100 == 1
headers.delete "Content-Length" headers.delete "Content-Length"
headers.delete "Content-Type" headers.delete "Content-Type"
elsif body.respond_to? :to_ary and not [204, 304].include?(status.to_i) elsif not [204, 304].include?(status.to_i)
headers["Content-Length"] = body.inject(0) { |l, p| l + Rack::Utils.bytesize(p) }.to_s length = 0
body.each { |part| length += Rack::Utils.bytesize(part) }
headers["Content-Length"] = length.to_s
end end
super super
end end
Expand Down Expand Up @@ -613,15 +615,21 @@ def call!(env) # :nodoc:
invoke { dispatch! } invoke { dispatch! }
invoke { error_block!(response.status) } invoke { error_block!(response.status) }


unless @response['Content-Type'] set_content_type! unless @response['Content-Type']
if body.respond_to? :to_ary and body[0].respond_to? :content_type
content_type body[0].content_type @response.finish
end

def set_content_type!
body.each do |part|
if part.respond_to? :content_type
content_type part.content_type
else else
content_type :html content_type :html
end end
return
end end

content_type :html
@response.finish
end end


# Access settings defined with Base.set. # Access settings defined with Base.set.
Expand Down
18 changes: 16 additions & 2 deletions test/base_test.rb
Expand Up @@ -57,10 +57,22 @@ class TestApp < Sinatra::Base
end end


describe "Sinatra::Base as Rack middleware" do describe "Sinatra::Base as Rack middleware" do
class StrictRackReponse
def initialize(array)
@array = array
end
def each(&block)
@array.each(&block)
end
def to_ary
raise "calling me is evil, please don't!"
end
end

app = lambda { |env| app = lambda { |env|
headers = {'X-Downstream' => 'true'} headers = {'X-Downstream' => 'true'}
headers['X-Route-Missing'] = env['sinatra.route-missing'] || '' headers['X-Route-Missing'] = env['sinatra.route-missing'] || ''
[210, headers, ['Hello from downstream']] } [210, headers, StrictRackReponse.new(['Hello from downstream'])] }


class TestMiddleware < Sinatra::Base class TestMiddleware < Sinatra::Base
end end
Expand Down Expand Up @@ -126,7 +138,9 @@ class TestMiddleware < Sinatra::Base
assert_nil res assert_nil res
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 from downstream'], response.body parts = []
response.body.each { |part| parts << part }
assert_equal ['Hello from downstream'], parts
'Hello after explicit forward' 'Hello after explicit forward'
end end
end end
Expand Down