Fix Request#accept? NoMethodError

Allow Request#accept? to return false when type parameter does not
match any of the preferred types. When `preferred_type(type)` returns
nil, `include?` will throw a NoMethodError. By converting output to a
string, `include?` will return false.
This commit is contained in:
sbonami 2013-10-25 20:46:40 -04:00
parent f538253cc8
commit da3282f493
2 changed files with 6 additions and 1 deletions

View File

@ -32,7 +32,7 @@ module Sinatra
end
def accept?(type)
preferred_type(type).include?(type)
preferred_type(type).to_s.include?(type)
end
def preferred_type(*types)

View File

@ -89,4 +89,9 @@ class RequestTest < Test::Unit::TestCase
assert request.accept?('text/html')
assert_equal '*/*', request.preferred_type.to_s
end
it 'does not accept */* when parameter does not match HTTP_ACCEPT' do
request = Sinatra::Request.new 'HTTP_ACCEPT' => 'application/json'
assert !request.accept?('text/html')
end
end