Skip to content

Commit

Permalink
Return first provided mime-type on ACCEPT = */*
Browse files Browse the repository at this point in the history
fixes #433
  • Loading branch information
skade committed Mar 4, 2011
1 parent 25042a6 commit 7127017
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions padrino-core/lib/padrino-core/application/routing.rb
Expand Up @@ -503,9 +503,14 @@ def provides(*types)
condition do
mime_types = types.map { |t| mime_type(t) }
accepts = request.accept.map { |a| a.split(";")[0].strip }
matching_types = (accepts & mime_types)
request.path_info =~ /\.([^\.\/]+)$/
url_format = $1.to_sym if $1

if accepts.any? { |a| a == "*/*" }
matching_types = mime_types.slice(0,1)
else
matching_types = (accepts & mime_types)
end

if params[:format]
accept_format = params[:format]
Expand All @@ -517,7 +522,6 @@ def provides(*types)
matched_format = types.include?(:any) ||
types.include?(accept_format) ||
types.include?(url_format) ||
accepts.any? { |a| a == "*/*" } ||
((!url_format) && request.accept.empty? && types.include?(:html))

# per rfc: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Expand Down
9 changes: 9 additions & 0 deletions padrino-core/test/test_routing.rb
Expand Up @@ -178,6 +178,15 @@ class TestRouting < Test::Unit::TestCase
get "/a", {}, {"HTTP_ACCEPT" => "application/yaml"}
assert_equal 406, status
end

should "not set content_type to :html if Accept */* and html not in provides" do
mock_app do
get("/foo", :provides => [:json, :xml]) { content_type.to_s }
end

get '/foo', {}, { 'HTTP_ACCEPT' => '*/*;q=0.5' }
assert_equal 'json', body
end

should "not default to HTML if HTML is not provided and no type is given" do
mock_app do
Expand Down

0 comments on commit 7127017

Please sign in to comment.