Skip to content

Commit

Permalink
Fix error of routing when using provides :any and Accept contains */*
Browse files Browse the repository at this point in the history
  • Loading branch information
tyabe committed Apr 9, 2013
1 parent 6b00a0a commit 7495b6e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
7 changes: 5 additions & 2 deletions padrino-core/lib/padrino-core/application/routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -813,19 +813,22 @@ def process_path_for_provides(path, format_params)
def provides(*types)
@_use_format = true
condition do
mime_types = types.map { |t| mime_type(t) }
mime_types = types.map { |t| mime_type(t) }.compact
url_format = params[:format].to_sym if params[:format]
accepts = request.accept.map { |a| a.to_str }

# per rfc2616-sec14:
# Assume */* if no ACCEPT header is given.
catch_all = (accepts.delete "*/*" || accepts.empty?)
matching_types = accepts.empty? ? mime_types.slice(0,1) : (accepts & mime_types)
if matching_types.empty? && types.include?(:any)
matching_types = accepts
end

if !url_format && matching_types.first
type = ::Rack::Mime::MIME_TYPES.find { |k, v| v == matching_types.first }[0].sub(/\./,'').to_sym
accept_format = CONTENT_TYPE_ALIASES[type] || type
elsif catch_all
elsif catch_all && !types.include?(:any)
type = types.first
accept_format = CONTENT_TYPE_ALIASES[type] || type
end
Expand Down
18 changes: 18 additions & 0 deletions padrino-core/test/test_routing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,24 @@ class FooError < RuntimeError; end
assert_equal 'js', body
end

should "set content_type to :html if Accept */* and provides of :any" do
mock_app do
get("/foo", :provides => :any) { content_type.to_s }
end

get '/foo', {}, { 'HTTP_ACCEPT' => '*/*' }
assert_equal 'html', body
end

should "set content_type to :js if Accept includes both application/javascript, */*;q=0.5 and provides of :any" do
mock_app do
get("/foo", :provides => :any) { content_type.to_s }
end

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

should 'allows custom route-conditions to be set via route options and halt' do
protector = Module.new do
def protect(*args)
Expand Down

0 comments on commit 7495b6e

Please sign in to comment.