1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Merge pull request #8958 from balexand/strong_parameters_exception_handling

Strong parameters exception handling
This commit is contained in:
Rafael Mendonça França 2013-01-15 17:28:29 -08:00
commit e42b5f99eb
4 changed files with 14 additions and 16 deletions

View file

@ -373,12 +373,6 @@ module ActionController
extend ActiveSupport::Concern
include ActiveSupport::Rescuable
included do
rescue_from(ActionController::ParameterMissing) do |parameter_missing_exception|
render text: "Required parameter missing: #{parameter_missing_exception.param}", status: :bad_request
end
end
# Returns a new ActionController::Parameters object that
# has been instantiated with the <tt>request.parameters</tt>.
def params

View file

@ -12,7 +12,8 @@ module ActionDispatch
'ActionController::NotImplemented' => :not_implemented,
'ActionController::UnknownFormat' => :not_acceptable,
'ActionController::InvalidAuthenticityToken' => :unprocessable_entity,
'ActionController::BadRequest' => :bad_request
'ActionController::BadRequest' => :bad_request,
'ActionController::ParameterMissing' => :bad_request
)
cattr_accessor :rescue_templates

View file

@ -11,20 +11,17 @@ class ActionControllerRequiredParamsTest < ActionController::TestCase
tests BooksController
test "missing required parameters will raise exception" do
post :create, { magazine: { name: "Mjallo!" } }
assert_response :bad_request
assert_raise (ActionController::ParameterMissing) do
post :create, { magazine: { name: "Mjallo!" } }
end
post :create, { book: { title: "Mjallo!" } }
assert_response :bad_request
assert_raise (ActionController::ParameterMissing) do
post :create, { book: { title: "Mjallo!" } }
end
end
test "required parameters that are present will not raise" do
post :create, { book: { name: "Mjallo!" } }
assert_response :ok
end
test "missing parameters will be mentioned in the return" do
post :create, { magazine: { name: "Mjallo!" } }
assert_equal "Required parameter missing: book", response.body
end
end

View file

@ -39,6 +39,8 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
raise ActionController::BadRequest
when "/missing_keys"
raise ActionController::UrlGenerationError, "No route matches"
when "/parameter_missing"
raise ActionController::ParameterMissing, :missing_param_key
else
raise "puke!"
end
@ -114,6 +116,10 @@ class DebugExceptionsTest < ActionDispatch::IntegrationTest
get "/bad_request", {}, {'action_dispatch.show_exceptions' => true}
assert_response 400
assert_match(/ActionController::BadRequest/, body)
get "/parameter_missing", {}, {'action_dispatch.show_exceptions' => true}
assert_response 400
assert_match(/ActionController::ParameterMissing/, body)
end
test "does not show filtered parameters" do