Ensure that raise_on_unexpected_params configuration will work

上级 df1f2907
......@@ -21,24 +21,22 @@ class Railtie < Rails::Railtie #:nodoc:
initializer "action_controller.parameters_config" do |app|
ActionController::Parameters.permit_all_parameters = app.config.action_controller.delete(:permit_all_parameters) { false }
ActionController::Parameters.raise_on_unexpected = app.config.action_controller.raise_on_unexpected_params
ActionController::Parameters.raise_on_unexpected = app.config.action_controller.delete(:raise_on_unexpected_params) { Rails.env.test? || Rails.env.development? }
end
initializer "action_controller.set_configs" do |app|
paths = app.config.paths
options = app.config.action_controller
options.logger ||= Rails.logger
options.cache_store ||= Rails.cache
options.logger ||= Rails.logger
options.cache_store ||= Rails.cache
options.javascripts_dir ||= paths["public/javascripts"].first
options.stylesheets_dir ||= paths["public/stylesheets"].first
options.javascripts_dir ||= paths["public/javascripts"].first
options.stylesheets_dir ||= paths["public/stylesheets"].first
# Ensure readers methods get compiled
options.asset_host ||= app.config.asset_host
options.relative_url_root ||= app.config.relative_url_root
options.raise_on_unexpected_params ||= (Rails.env.test? || Rails.env.development?)
options.asset_host ||= app.config.asset_host
options.relative_url_root ||= app.config.relative_url_root
ActiveSupport.on_load(:action_controller) do
include app.routes.mounted_helpers
......
......@@ -567,6 +567,54 @@ def create
assert_equal 'permitted', last_response.body
end
test "config.action_controller.raise_on_unexpected_params = true" do
app_file 'app/controllers/posts_controller.rb', <<-RUBY
class PostsController < ActionController::Base
def create
render text: params.require(:post).permit(:name)
end
end
RUBY
add_to_config <<-RUBY
routes.prepend do
resources :posts
end
config.action_controller.raise_on_unexpected_params = true
RUBY
require "#{app_path}/config/environment"
assert_equal true, ActionController::Parameters.raise_on_unexpected
post "/posts", {post: {"title" =>"zomg"}}
assert_match /We're sorry, but something went wrong/, last_response.body
end
test "config.action_controller.raise_on_unexpected_params is true by default on development" do
ENV["RAILS_ENV"] = "development"
require "#{app_path}/config/environment"
assert_equal true, ActionController::Parameters.raise_on_unexpected
end
test "config.action_controller.raise_on_unexpected_params is true by defaul on test" do
ENV["RAILS_ENV"] = "test"
require "#{app_path}/config/environment"
assert_equal true, ActionController::Parameters.raise_on_unexpected
end
test "config.action_controller.raise_on_unexpected_params is false by default on production" do
ENV["RAILS_ENV"] = "production"
require "#{app_path}/config/environment"
assert_equal false, ActionController::Parameters.raise_on_unexpected
end
test "config.action_dispatch.ignore_accept_header" do
make_basic_app do |app|
app.config.action_dispatch.ignore_accept_header = true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册