From a1d9001a7abd1ba203de6f1c2696bdce826aed29 Mon Sep 17 00:00:00 2001 From: Devlin Daley Date: Wed, 28 Jan 2009 11:55:49 -0700 Subject: [PATCH] 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. --- lib/sinatra/base.rb | 2 +- test/routing_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/sinatra/base.rb b/lib/sinatra/base.rb index 6df4a9ae..98f9cc6f 100644 --- a/lib/sinatra/base.rb +++ b/lib/sinatra/base.rb @@ -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) diff --git a/test/routing_test.rb b/test/routing_test.rb index faf54553..e8c472b7 100644 --- a/test/routing_test.rb +++ b/test/routing_test.rb @@ -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