Skip to content

Commit

Permalink
Merge pull request #2313 from ssoroka/master
Browse files Browse the repository at this point in the history
Raise a rescuable exception, rather than responding with a head :not_acceptable (406)
  • Loading branch information
josevalim committed May 6, 2012
2 parents bad119c + 6471ced commit 13811ee
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
3 changes: 3 additions & 0 deletions actionpack/lib/action_controller/metal/exceptions.rb
Expand Up @@ -38,4 +38,7 @@ def initialize(message = nil)

class UnknownHttpMethod < ActionControllerError #:nodoc:
end

class UnknownFormat < ActionControllerError #:nodoc:
end
end
3 changes: 1 addition & 2 deletions actionpack/lib/action_controller/metal/mime_responds.rb
Expand Up @@ -375,8 +375,7 @@ def retrieve_collector_from_mimes(mimes=nil, &block) #:nodoc:
lookup_context.rendered_format = lookup_context.formats.first
collector
else
head :not_acceptable
nil
raise ActionController::UnknownFormat
end
end

Expand Down
Expand Up @@ -10,6 +10,7 @@ class ExceptionWrapper
'AbstractController::ActionNotFound' => :not_found,
'ActionController::MethodNotAllowed' => :method_not_allowed,
'ActionController::NotImplemented' => :not_implemented,
'ActionController::UnknownFormat' => :not_acceptable,
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity
)

Expand Down
9 changes: 9 additions & 0 deletions actionpack/test/controller/integration_test.rb
Expand Up @@ -405,6 +405,15 @@ def test_xml_http_request_get
end
end

def test_request_with_bad_format
with_test_route_set do
xhr :get, '/get.php'
assert_equal 406, status
assert_response 406
assert_response :not_acceptable
end
end

def test_get_with_query_string
with_test_route_set do
get '/get_with_params?foo=bar'
Expand Down
32 changes: 19 additions & 13 deletions actionpack/test/controller/mime_responds_test.rb
Expand Up @@ -207,8 +207,9 @@ def test_html
get :html_or_xml
assert_equal 'HTML', @response.body

get :just_xml
assert_response 406
assert_raises(ActionController::UnknownFormat) do
get :just_xml
end
end

def test_all
Expand Down Expand Up @@ -239,8 +240,10 @@ def test_js_or_html
assert_equal 'HTML', @response.body

@request.accept = "text/javascript, text/html"
xhr :get, :just_xml
assert_response 406

assert_raises(ActionController::UnknownFormat) do
xhr :get, :just_xml
end
end

def test_json_or_yaml_with_leading_star_star
Expand Down Expand Up @@ -495,9 +498,9 @@ def test_format_with_custom_response_type_and_request_headers
end

def test_invalid_format
get :using_defaults, :format => "invalidformat"
assert_equal " ", @response.body
assert_equal "text/html", @response.content_type
assert_raises(ActionController::UnknownFormat) do
get :using_defaults, :format => "invalidformat"
end
end
end

Expand Down Expand Up @@ -701,12 +704,14 @@ def test_using_resource_with_overwrite_block

def test_not_acceptable
@request.accept = "application/xml"
get :using_resource_with_block
assert_equal 406, @response.status
assert_raises(ActionController::UnknownFormat) do
get :using_resource_with_block
end

@request.accept = "text/javascript"
get :using_resource_with_overwrite_block
assert_equal 406, @response.status
assert_raises(ActionController::UnknownFormat) do
get :using_resource_with_overwrite_block
end
end

def test_using_resource_for_post_with_html_redirects_on_success
Expand Down Expand Up @@ -984,8 +989,9 @@ def test_respond_as_responder_entry_point
def test_clear_respond_to
@controller = InheritedRespondWithController.new
@request.accept = "text/html"
get :index
assert_equal 406, @response.status
assert_raises(ActionController::UnknownFormat) do
get :index
end
end

def test_first_in_respond_to_has_higher_priority
Expand Down

0 comments on commit 13811ee

Please sign in to comment.