diff options
-rw-r--r-- | actionpack/lib/action_dispatch/http/parameters.rb | 19 | ||||
-rw-r--r-- | actionpack/test/dispatch/show_exceptions_test.rb | 16 |
2 files changed, 32 insertions, 3 deletions
diff --git a/actionpack/lib/action_dispatch/http/parameters.rb b/actionpack/lib/action_dispatch/http/parameters.rb index c9df787351..5c20bc53c2 100644 --- a/actionpack/lib/action_dispatch/http/parameters.rb +++ b/actionpack/lib/action_dispatch/http/parameters.rb @@ -41,9 +41,9 @@ module ActionDispatch # Returns a hash with the \parameters used to form the \path of the request. # Returned hash keys are strings: # - # {'action' => 'my_action', 'controller' => 'my_controller'} + # {'action' => 'my_action', 'controller' => 'my_controller', format => 'html'} def path_parameters - get_header(PARAMETERS_KEY) || {} + get_header(PARAMETERS_KEY) || default_path_parameters end private @@ -66,6 +66,21 @@ module ActionDispatch def params_parsers ActionDispatch::Request.parameter_parsers end + + def default_path_parameters + if format = format_from_path_extension + { 'format' => format } + else + {} + end + end + + def format_from_path_extension + path = @env['action_dispatch.original_path'] + if match = path.match(/\.(\w+)$/) + match.captures.first + end + end end end end diff --git a/actionpack/test/dispatch/show_exceptions_test.rb b/actionpack/test/dispatch/show_exceptions_test.rb index ffdf775836..14894d4b82 100644 --- a/actionpack/test/dispatch/show_exceptions_test.rb +++ b/actionpack/test/dispatch/show_exceptions_test.rb @@ -8,7 +8,7 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest case req.path when "/not_found" raise AbstractController::ActionNotFound - when "/bad_params" + when "/bad_params", "/bad_params.json" begin raise StandardError.new rescue @@ -120,4 +120,18 @@ class ShowExceptionsTest < ActionDispatch::IntegrationTest assert_response 405 assert_equal "", body end + + test "bad params exception is returned in the correct format" do + @app = ProductionApp + + get "/bad_params", headers: { 'action_dispatch.show_exceptions' => true } + assert_equal "text/html; charset=utf-8", response.headers["Content-Type"] + assert_response 400 + assert_match(/400 error/, body) + + get "/bad_params.json", headers: { 'action_dispatch.show_exceptions' => true } + assert_equal "application/json; charset=utf-8", response.headers["Content-Type"] + assert_response 400 + assert_equal("{\"status\":400,\"error\":\"Bad Request\"}", body) + end end |