Remove deprecated support to non-keyword arguments #process

上级 31639eab
* Remove deprecated support to non-keyword arguments in `ActionController::TestCase#process`,
`#get`, `#post`, `#patch`, `#put`, `#delete`, and `#head`.
*Rafael Mendonça França*
* Remove deprecated `xml_http_request` and `xhr` methods in `ActionController::TestCase`. * Remove deprecated `xml_http_request` and `xhr` methods in `ActionController::TestCase`.
*Rafael Mendonça França* *Rafael Mendonça França*
......
...@@ -386,40 +386,40 @@ def determine_default_controller_class(name) ...@@ -386,40 +386,40 @@ def determine_default_controller_class(name)
# #
# Note that the request method is not verified. The different methods are # Note that the request method is not verified. The different methods are
# available to make the tests more expressive. # available to make the tests more expressive.
def get(action, *args) def get(action, **args)
res = process_with_kwargs("GET", action, *args) res = process(action, method: "GET", **args)
cookies.update res.cookies cookies.update res.cookies
res res
end end
# Simulate a POST request with the given parameters and set/volley the response. # Simulate a POST request with the given parameters and set/volley the response.
# See +get+ for more details. # See +get+ for more details.
def post(action, *args) def post(action, **args)
process_with_kwargs("POST", action, *args) process(action, method: "POST", **args)
end end
# Simulate a PATCH request with the given parameters and set/volley the response. # Simulate a PATCH request with the given parameters and set/volley the response.
# See +get+ for more details. # See +get+ for more details.
def patch(action, *args) def patch(action, **args)
process_with_kwargs("PATCH", action, *args) process(action, method: "PATCH", **args)
end end
# Simulate a PUT request with the given parameters and set/volley the response. # Simulate a PUT request with the given parameters and set/volley the response.
# See +get+ for more details. # See +get+ for more details.
def put(action, *args) def put(action, **args)
process_with_kwargs("PUT", action, *args) process(action, method: "PUT", **args)
end end
# Simulate a DELETE request with the given parameters and set/volley the response. # Simulate a DELETE request with the given parameters and set/volley the response.
# See +get+ for more details. # See +get+ for more details.
def delete(action, *args) def delete(action, **args)
process_with_kwargs("DELETE", action, *args) process(action, method: "DELETE", **args)
end end
# Simulate a HEAD request with the given parameters and set/volley the response. # Simulate a HEAD request with the given parameters and set/volley the response.
# See +get+ for more details. # See +get+ for more details.
def head(action, *args) def head(action, **args)
process_with_kwargs("HEAD", action, *args) process(action, method: "HEAD", **args)
end end
# Simulate an HTTP request to +action+ by specifying request method, # Simulate an HTTP request to +action+ by specifying request method,
...@@ -452,36 +452,14 @@ def head(action, *args) ...@@ -452,36 +452,14 @@ def head(action, *args)
# respectively which will make tests more expressive. # respectively which will make tests more expressive.
# #
# Note that the request method is not verified. # Note that the request method is not verified.
def process(action, *args) def process(action, method: "GET", params: {}, session: nil, body: nil, flash: {}, format: nil, xhr: false, as: nil)
check_required_ivars check_required_ivars
if kwarg_request?(args)
parameters, session, body, flash, http_method, format, xhr, as = args[0].values_at(:params, :session, :body, :flash, :method, :format, :xhr, :as)
else
http_method, parameters, session, flash = args
format = nil
if parameters.is_a?(String) && http_method != "HEAD"
body = parameters
parameters = nil
end
if parameters || session || flash
non_kwarg_request_warning
end
end
if body if body
@request.set_header "RAW_POST_DATA", body @request.set_header "RAW_POST_DATA", body
end end
if http_method http_method = method.to_s.upcase
http_method = http_method.to_s.upcase
else
http_method = "GET"
end
parameters ||= {}
@html_document = nil @html_document = nil
...@@ -502,12 +480,12 @@ def process(action, *args) ...@@ -502,12 +480,12 @@ def process(action, *args)
format ||= as format ||= as
end end
parameters = params.symbolize_keys
if format if format
parameters[:format] = format parameters[:format] = format
end end
parameters = parameters.symbolize_keys
generated_extras = @routes.generate_extras(parameters.merge(controller: controller_class_name, action: action.to_s)) generated_extras = @routes.generate_extras(parameters.merge(controller: controller_class_name, action: action.to_s))
generated_path = generated_path(generated_extras) generated_path = generated_path(generated_extras)
query_string_keys = query_parameter_names(generated_extras) query_string_keys = query_parameter_names(generated_extras)
...@@ -626,38 +604,6 @@ def scrub_env!(env) ...@@ -626,38 +604,6 @@ def scrub_env!(env)
env env
end end
def process_with_kwargs(http_method, action, *args)
if kwarg_request?(args)
args.first.merge!(method: http_method)
process(action, *args)
else
non_kwarg_request_warning if args.any?
args = args.unshift(http_method)
process(action, *args)
end
end
REQUEST_KWARGS = %i(params session flash method body xhr)
def kwarg_request?(args)
args[0].respond_to?(:keys) && (
(args[0].key?(:format) && args[0].keys.size == 1) ||
args[0].keys.any? { |k| REQUEST_KWARGS.include?(k) }
)
end
def non_kwarg_request_warning
ActiveSupport::Deprecation.warn(<<-MSG.strip_heredoc)
ActionController::TestCase HTTP request methods will accept only
keyword arguments in future Rails versions.
Examples:
get :show, params: { id: 1 }, session: { user_id: 1 }
process :update, method: :post, params: { id: 1 }
MSG
end
def document_root_element def document_root_element
html_document.root html_document.root
end end
......
...@@ -229,14 +229,6 @@ def test_body_stream ...@@ -229,14 +229,6 @@ def test_body_stream
assert_equal params.to_query, @response.body assert_equal params.to_query, @response.body
end end
def test_deprecated_body_stream
params = Hash[:page, { name: "page name" }, "some key", 123]
assert_deprecated { post :render_body, params.dup }
assert_equal params.to_query, @response.body
end
def test_document_body_and_params_with_post def test_document_body_and_params_with_post
post :test_params, params: { id: 1 } post :test_params, params: { id: 1 }
assert_equal({ "id"=>"1", "controller"=>"test_case_test/test", "action"=>"test_params" }, ::JSON.parse(@response.body)) assert_equal({ "id"=>"1", "controller"=>"test_case_test/test", "action"=>"test_params" }, ::JSON.parse(@response.body))
...@@ -247,21 +239,11 @@ def test_document_body_with_post ...@@ -247,21 +239,11 @@ def test_document_body_with_post
assert_equal "document body", @response.body assert_equal "document body", @response.body
end end
def test_deprecated_document_body_with_post
assert_deprecated { post :render_body, "document body" }
assert_equal "document body", @response.body
end
def test_document_body_with_put def test_document_body_with_put
put :render_body, body: "document body" put :render_body, body: "document body"
assert_equal "document body", @response.body assert_equal "document body", @response.body
end end
def test_deprecated_document_body_with_put
assert_deprecated { put :render_body, "document body" }
assert_equal "document body", @response.body
end
def test_head def test_head
head :test_params head :test_params
assert_equal 200, @response.status assert_equal 200, @response.status
...@@ -272,11 +254,6 @@ def test_process_without_flash ...@@ -272,11 +254,6 @@ def test_process_without_flash
assert_equal "><", flash["test"] assert_equal "><", flash["test"]
end end
def test_deprecated_process_with_flash
assert_deprecated { process :set_flash, "GET", nil, nil, "test" => "value" }
assert_equal ">value<", flash["test"]
end
def test_process_with_flash def test_process_with_flash
process :set_flash, process :set_flash,
method: "GET", method: "GET",
...@@ -284,11 +261,6 @@ def test_process_with_flash ...@@ -284,11 +261,6 @@ def test_process_with_flash
assert_equal ">value<", flash["test"] assert_equal ">value<", flash["test"]
end end
def test_deprecated_process_with_flash_now
assert_deprecated { process :set_flash_now, "GET", nil, nil, "test_now" => "value_now" }
assert_equal ">value_now<", flash["test_now"]
end
def test_process_with_flash_now def test_process_with_flash_now
process :set_flash_now, process :set_flash_now,
method: "GET", method: "GET",
...@@ -311,14 +283,6 @@ def test_process_with_session ...@@ -311,14 +283,6 @@ def test_process_with_session
assert_equal "it works", session[:symbol], "Test session hash should allow indifferent access" assert_equal "it works", session[:symbol], "Test session hash should allow indifferent access"
end end
def test_process_with_session_arg
assert_deprecated { process :no_op, "GET", nil, "string" => "value1", symbol: "value2" }
assert_equal "value1", session["string"]
assert_equal "value1", session[:string]
assert_equal "value2", session["symbol"]
assert_equal "value2", session[:symbol]
end
def test_process_with_session_kwarg def test_process_with_session_kwarg
process :no_op, method: "GET", session: { "string" => "value1", symbol: "value2" } process :no_op, method: "GET", session: { "string" => "value1", symbol: "value2" }
assert_equal "value1", session["string"] assert_equal "value1", session["string"]
...@@ -327,15 +291,6 @@ def test_process_with_session_kwarg ...@@ -327,15 +291,6 @@ def test_process_with_session_kwarg
assert_equal "value2", session[:symbol] assert_equal "value2", session[:symbol]
end end
def test_deprecated_process_merges_session_arg
session[:foo] = "bar"
assert_deprecated {
get :no_op, nil, bar: "baz"
}
assert_equal "bar", session[:foo]
assert_equal "baz", session[:bar]
end
def test_process_merges_session_arg def test_process_merges_session_arg
session[:foo] = "bar" session[:foo] = "bar"
get :no_op, session: { bar: "baz" } get :no_op, session: { bar: "baz" }
...@@ -343,15 +298,6 @@ def test_process_merges_session_arg ...@@ -343,15 +298,6 @@ def test_process_merges_session_arg
assert_equal "baz", session[:bar] assert_equal "baz", session[:bar]
end end
def test_deprecated_merged_session_arg_is_retained_across_requests
assert_deprecated {
get :no_op, nil, foo: "bar"
}
assert_equal "bar", session[:foo]
get :no_op
assert_equal "bar", session[:foo]
end
def test_merged_session_arg_is_retained_across_requests def test_merged_session_arg_is_retained_across_requests
get :no_op, session: { foo: "bar" } get :no_op, session: { foo: "bar" }
assert_equal "bar", session[:foo] assert_equal "bar", session[:foo]
...@@ -393,11 +339,6 @@ def test_process_with_symbol_method ...@@ -393,11 +339,6 @@ def test_process_with_symbol_method
assert_equal "/test_case_test/test/test_uri", @response.body assert_equal "/test_case_test/test/test_uri", @response.body
end end
def test_deprecated_process_with_request_uri_with_params
assert_deprecated { process :test_uri, "GET", id: 7 }
assert_equal "/test_case_test/test/test_uri/7", @response.body
end
def test_process_with_request_uri_with_params def test_process_with_request_uri_with_params
process :test_uri, process :test_uri,
method: "GET", method: "GET",
...@@ -406,12 +347,6 @@ def test_process_with_request_uri_with_params ...@@ -406,12 +347,6 @@ def test_process_with_request_uri_with_params
assert_equal "/test_case_test/test/test_uri/7", @response.body assert_equal "/test_case_test/test/test_uri/7", @response.body
end end
def test_deprecated_process_with_request_uri_with_params_with_explicit_uri
@request.env["PATH_INFO"] = "/explicit/uri"
assert_deprecated { process :test_uri, "GET", id: 7 }
assert_equal "/explicit/uri", @response.body
end
def test_process_with_request_uri_with_params_with_explicit_uri def test_process_with_request_uri_with_params_with_explicit_uri
@request.env["PATH_INFO"] = "/explicit/uri" @request.env["PATH_INFO"] = "/explicit/uri"
process :test_uri, method: "GET", params: { id: 7 } process :test_uri, method: "GET", params: { id: 7 }
...@@ -491,20 +426,6 @@ def test_assert_routing_with_glob ...@@ -491,20 +426,6 @@ def test_assert_routing_with_glob
end end
end end
def test_deprecated_params_passing
assert_deprecated {
get :test_params, page: { name: "Page name", month: "4", year: "2004", day: "6" }
}
parsed_params = ::JSON.parse(@response.body)
assert_equal(
{
"controller" => "test_case_test/test", "action" => "test_params",
"page" => { "name" => "Page name", "month" => "4", "year" => "2004", "day" => "6" }
},
parsed_params
)
end
def test_params_passing def test_params_passing
get :test_params, params: { get :test_params, params: {
page: { page: {
...@@ -589,16 +510,6 @@ def test_params_passing_path_parameter_is_string_when_not_html_request ...@@ -589,16 +510,6 @@ def test_params_passing_path_parameter_is_string_when_not_html_request
) )
end end
def test_deprecated_params_passing_path_parameter_is_string_when_not_html_request
assert_deprecated { get :test_params, format: "json", id: 1 }
parsed_params = ::JSON.parse(@response.body)
assert_equal(
{ "controller" => "test_case_test/test", "action" => "test_params",
"format" => "json", "id" => "1" },
parsed_params
)
end
def test_params_passing_with_frozen_values def test_params_passing_with_frozen_values
assert_nothing_raised do assert_nothing_raised do
get :test_params, params: { get :test_params, params: {
...@@ -683,11 +594,6 @@ def test_id_converted_to_string ...@@ -683,11 +594,6 @@ def test_id_converted_to_string
assert_kind_of String, @request.path_parameters[:id] assert_kind_of String, @request.path_parameters[:id]
end end
def test_deprecared_id_converted_to_string
assert_deprecated { get :test_params, id: 20, foo: Object.new }
assert_kind_of String, @request.path_parameters[:id]
end
def test_array_path_parameter_handled_properly def test_array_path_parameter_handled_properly
with_routing do |set| with_routing do |set|
set.draw do set.draw do
...@@ -757,14 +663,6 @@ def test_xhr_with_session ...@@ -757,14 +663,6 @@ def test_xhr_with_session
assert_equal "it works", session[:symbol], "Test session hash should allow indifferent access" assert_equal "it works", session[:symbol], "Test session hash should allow indifferent access"
end end
def test_deprecated_params_reset_between_post_requests
assert_deprecated { post :no_op, foo: "bar" }
assert_equal "bar", @request.params[:foo]
post :no_op
assert @request.params[:foo].blank?
end
def test_params_reset_between_post_requests def test_params_reset_between_post_requests
post :no_op, params: { foo: "bar" } post :no_op, params: { foo: "bar" }
assert_equal "bar", @request.params[:foo] assert_equal "bar", @request.params[:foo]
...@@ -964,15 +862,6 @@ def test_fixture_file_upload_ignores_nil_fixture_path ...@@ -964,15 +862,6 @@ def test_fixture_file_upload_ignores_nil_fixture_path
assert_equal File.open("#{FILES_DIR}/mona_lisa.jpg", READ_PLAIN).read, uploaded_file.read assert_equal File.open("#{FILES_DIR}/mona_lisa.jpg", READ_PLAIN).read, uploaded_file.read
end end
def test_deprecated_action_dispatch_uploaded_file_upload
filename = "mona_lisa.jpg"
path = "#{FILES_DIR}/#{filename}"
assert_deprecated {
post :test_file_upload, file: Rack::Test::UploadedFile.new(path, "image/jpg", true)
}
assert_equal "159528", @response.body
end
def test_action_dispatch_uploaded_file_upload def test_action_dispatch_uploaded_file_upload
filename = "mona_lisa.jpg" filename = "mona_lisa.jpg"
path = "#{FILES_DIR}/#{filename}" path = "#{FILES_DIR}/#{filename}"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册