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

Fix json encoded controller tests

The controller test case is trying to encode json parameters in the same
way it encodes query parameters. However we need to encode json
parameters as json.

This was broken in #39534 because `generate_extras` is encoding
everything into query parameters. But json parameters are in the post
body and need to be treated differently than the query parameters.

Followup to #39651

Aaron Patterson <tenderlove@ruby-lang.org>
This commit is contained in:
eileencodes 2020-06-17 13:22:45 -04:00
parent ad58f5810f
commit 9540a7b119
No known key found for this signature in database
GPG key ID: BA5C575120BBE8DF
2 changed files with 20 additions and 7 deletions

View file

@ -74,14 +74,21 @@ module ActionController
non_path_parameters = {} non_path_parameters = {}
path_parameters = {} path_parameters = {}
if parameters[:format] == :json
parameters = JSON.load(JSON.dump(parameters))
query_string_keys = query_string_keys.map(&:to_s)
end
parameters.each do |key, value| parameters.each do |key, value|
if query_string_keys.include?(key) if query_string_keys.include?(key)
non_path_parameters[key] = value non_path_parameters[key] = value
else else
if value.is_a?(Array) unless parameters["format"] == "json"
value = value.map(&:to_param) if value.is_a?(Array)
else value = value.map(&:to_param)
value = value.to_param else
value = value.to_param
end
end end
path_parameters[key] = value path_parameters[key] = value

View file

@ -597,9 +597,9 @@ XML
post :render_body, params: { bool_value: true, str_value: "string", num_value: 2 }, as: :json post :render_body, params: { bool_value: true, str_value: "string", num_value: 2 }, as: :json
assert_equal "application/json", @request.headers["CONTENT_TYPE"] assert_equal "application/json", @request.headers["CONTENT_TYPE"]
assert_equal true, @request.request_parameters[:bool_value] assert_equal true, @request.request_parameters["bool_value"]
assert_equal "string", @request.request_parameters[:str_value] assert_equal "string", @request.request_parameters["str_value"]
assert_equal 2, @request.request_parameters[:num_value] assert_equal 2, @request.request_parameters["num_value"]
end end
def test_using_as_json_sets_format_json def test_using_as_json_sets_format_json
@ -607,6 +607,12 @@ XML
assert_equal "json", @request.format assert_equal "json", @request.format
end end
def test_using_as_json_with_empty_params
post :test_params, params: { foo: { bar: [] } }, as: :json
assert_equal({ "bar" => [] }, JSON.load(response.body)["foo"])
end
def test_mutating_content_type_headers_for_plain_text_files_sets_the_header def test_mutating_content_type_headers_for_plain_text_files_sets_the_header
@request.headers["Content-Type"] = "text/plain" @request.headers["Content-Type"] = "text/plain"
post :render_body, params: { name: "foo.txt" } post :render_body, params: { name: "foo.txt" }