Permalink
Browse files

Fix error of routing when using provides :any and Accept contains */*

  • Loading branch information...
1 parent 6b00a0a commit 7495b6ebd991bd0e7f36ea282c55a0db555f5f74 @tyabe tyabe committed Apr 9, 2013
Showing with 23 additions and 2 deletions.
  1. +5 −2 padrino-core/lib/padrino-core/application/routing.rb
  2. +18 −0 padrino-core/test/test_routing.rb
View
7 padrino-core/lib/padrino-core/application/routing.rb
@@ -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
View
18 padrino-core/test/test_routing.rb
@@ -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)

0 comments on commit 7495b6e

Please sign in to comment.