Fix :provides crashes with no Accept header [#139]

An exception was raised on every request that did not have an
Accept header due to the Accept parsing code calling split on
nil.

The Sinatra::Request#accept method now returns an empty collection
if the HTTP Accept header is not present.
This commit is contained in:
Devlin Daley 2009-01-28 11:55:49 -07:00 committed by Ryan Tomayko
parent 7a2ef0a6a1
commit a1d9001a7a
2 changed files with 15 additions and 1 deletions

View File

@ -12,7 +12,7 @@ module Sinatra
end
def accept
@env['HTTP_ACCEPT'].split(',').map { |a| a.strip }
@env['HTTP_ACCEPT'].to_s.split(',').map { |a| a.strip }
end
# Override Rack 0.9.x's #params implementation (see #72 in lighthouse)

View File

@ -484,4 +484,18 @@ describe "Routing" do
assert_equal type, response.headers['Content-Type']
end
end
it 'degrades gracefully when optional accept header is not provided' do
mock_app {
get '/', :provides => :xml do
request.env['HTTP_ACCEPT']
end
get '/' do
'default'
end
}
get '/'
assert ok?
assert_equal 'default', body
end
end