Browse files

#684 missing accept header should be same as */* per http standard

  • Loading branch information...
1 parent 5081d32 commit c307c3f0a7b84d62a0a7c603754db45e3a766e44 Greg Denton committed Mar 18, 2013
Showing with 31 additions and 19 deletions.
  1. +4 −2 lib/sinatra/base.rb
  2. +27 −17 test/routing_test.rb
View
6 lib/sinatra/base.rb
@@ -28,9 +28,11 @@ def accept
end
def preferred_type(*types)
- return accept.first if types.empty?
+ accepts = accept # just evaluate once
+ return accepts.first if types.empty?
types.flatten!
- accept.detect do |pattern|
+ return types.first if accepts.empty?
+ accepts.detect do |pattern|
type = types.detect { |t| File.fnmatch(pattern, t) }
return type if type
end
View
44 test/routing_test.rb
@@ -899,21 +899,47 @@ class RoutingTest < Test::Unit::TestCase
assert_equal 'application/xml', body
assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']
- get '/', {}, { :accept => 'text/html' }
+ get '/', {}, {}
+ assert ok?
+ assert_equal '', body
+ assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']
+
+ get '/', {}, { 'HTTP_ACCEPT' => '*/*' }
+ assert ok?
+ assert_equal '*/*', body
+ assert_equal 'application/xml;charset=utf-8', response.headers['Content-Type']
+
+ get '/', {}, { 'HTTP_ACCEPT' => 'text/html;q=0.9' }
assert !ok?
get '/foo', {}, { 'HTTP_ACCEPT' => 'text/html;q=0.9' }
assert ok?
assert_equal 'text/html;q=0.9', body
get '/foo', {}, { 'HTTP_ACCEPT' => '' }
+ assert ok?
+ assert_equal '', body
+
+ get '/foo', {}, { 'HTTP_ACCEPT' => '*/*' }
+ assert ok?
+ assert_equal '*/*', body
+
+ get '/foo', {}, { 'HTTP_ACCEPT' => 'application/xml' }
assert !ok?
get '/stream', {}, { 'HTTP_ACCEPT' => 'text/event-stream' }
assert ok?
assert_equal 'text/event-stream', body
get '/stream', {}, { 'HTTP_ACCEPT' => '' }
+ assert ok?
+ assert_equal '', body
+
+ get '/stream', {}, { 'HTTP_ACCEPT' => '*/*' }
+ assert ok?
+ assert_equal '*/*', body
+
+ get '/stream', {}, { 'HTTP_ACCEPT' => 'application/xml' }
assert !ok?
end
@@ -959,20 +985,6 @@ class RoutingTest < Test::Unit::TestCase
end
end
- it 'degrades gracefully when optional accept header is not provided' do
- mock_app {
- get '/', :provides => :xml do
- env['HTTP_ACCEPT']
- end
- get '/' do
- 'default'
- end
- }
- get '/'
- assert ok?
- assert_equal 'default', body
- end
-
it 'respects user agent preferences for the content type' do
mock_app { get('/', :provides => [:png, :html]) { content_type }}
get '/', {}, { 'HTTP_ACCEPT' => 'image/png;q=0.5,text/html;q=0.8' }
@@ -986,8 +998,6 @@ class RoutingTest < Test::Unit::TestCase
get('/', :provides => :xml) { content_type }
get('/') { 'no match' }
end
- get '/'
- assert_body 'no match'
get '/', {}, { 'HTTP_ACCEPT' => 'foo/*' }
assert_body 'no match'
get '/', {}, { 'HTTP_ACCEPT' => 'application/*' }

0 comments on commit c307c3f

Please sign in to comment.