class ParamsWrapperTest

Public Instance Methods

teardown() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 51
def teardown
  UsersController.last_parameters = nil
end
test_derived_name_from_controller() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 63
def test_derived_name_from_controller
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu" }
    assert_parameters("username" => "sikachu", "user" => { "username" => "sikachu" })
  end
end
test_derived_wrapped_keys_from_matching_model() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 191
def test_derived_wrapped_keys_from_matching_model
  assert_called(User, :attribute_names, times: 2, returns: ["username"]) do
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
    end
  end
end
test_derived_wrapped_keys_from_specified_model() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 201
def test_derived_wrapped_keys_from_specified_model
  with_default_wrapper_options do
    assert_called(Person, :attribute_names, times: 2, returns: ["username"]) do
      UsersController.wrap_parameters Person

      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "title" => "Developer" }
      assert_parameters("username" => "sikachu", "title" => "Developer", "person" => { "username" => "sikachu" })
    end
  end
end
test_empty_parameter_set() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 239
def test_empty_parameter_set
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: {}
    assert_parameters(
      "user" => {}
    )
  end
end
test_filtered_parameters() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 55
def test_filtered_parameters
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu" }
    assert_equal({ "controller" => "params_wrapper_test/users", "action" => "parse", "username" => "sikachu", "user" => { "username" => "sikachu" } }, @request.filtered_parameters)
  end
end
test_handles_empty_content_type() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 249
def test_handles_empty_content_type
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = nil
    _controller_class.dispatch(:parse, @request, @response)

    assert_equal 200, @response.status
    assert_equal "", @response.body
  end
end
test_nested_params() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 183
def test_nested_params
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "person" => { "username" => "sikachu" } }
    assert_parameters("person" => { "username" => "sikachu" }, "user" => { "person" => { "username" => "sikachu" } })
  end
end
test_no_double_wrap_if_key_exists() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 167
def test_no_double_wrap_if_key_exists
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "user" => { "username" => "sikachu" } }
    assert_parameters("user" => { "username" => "sikachu" })
  end
end
test_no_double_wrap_if_key_exists_and_value_is_nil() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 175
def test_no_double_wrap_if_key_exists_and_value_is_nil
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "user" => nil }
    assert_parameters("user" => nil)
  end
end
test_not_enabled_format() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 132
def test_not_enabled_format
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/xml"
    post :parse, params: { "username" => "sikachu", "title" => "Developer" }
    assert_parameters("username" => "sikachu", "title" => "Developer")
  end
end
test_not_wrap_reserved_parameters() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 159
def test_not_wrap_reserved_parameters
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "authenticity_token" => "pwned", "_method" => "put", "utf8" => "☃", "username" => "sikachu" }
    assert_parameters("authenticity_token" => "pwned", "_method" => "put", "utf8" => "☃", "username" => "sikachu", "user" => { "username" => "sikachu" })
  end
end
test_not_wrapping_abstract_model() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 213
def test_not_wrapping_abstract_model
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu", "title" => "Developer" }
    assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu", "title" => "Developer" })
  end
end
test_preserves_query_string_params() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 221
def test_preserves_query_string_params
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    get :parse, params: { "user" => { "username" => "nixon" } }
    assert_parameters(
      "user" => { "username" => "nixon" }
    )
  end
end
test_preserves_query_string_params_in_filtered_params() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 231
def test_preserves_query_string_params_in_filtered_params
  with_default_wrapper_options do
    @request.env["CONTENT_TYPE"] = "application/json"
    get :parse, params: { "user" => { "username" => "nixon" } }
    assert_equal({ "controller" => "params_wrapper_test/users", "action" => "parse", "user" => { "username" => "nixon" } }, @request.filtered_parameters)
  end
end
test_specify_both_wrapper_name_and_include_option() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 122
def test_specify_both_wrapper_name_and_include_option
  with_default_wrapper_options do
    UsersController.wrap_parameters :person, include: :username

    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu", "title" => "Developer" }
    assert_parameters("username" => "sikachu", "title" => "Developer", "person" => { "username" => "sikachu" })
  end
end
test_specify_exclude_option() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 112
def test_specify_exclude_option
  with_default_wrapper_options do
    UsersController.wrap_parameters exclude: :title

    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu", "title" => "Developer" }
    assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
  end
end
test_specify_format() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 149
def test_specify_format
  with_default_wrapper_options do
    UsersController.wrap_parameters format: :xml

    @request.env["CONTENT_TYPE"] = "application/xml"
    post :parse, params: { "username" => "sikachu", "title" => "Developer" }
    assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu", "title" => "Developer" })
  end
end
test_specify_include_option() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 102
def test_specify_include_option
  with_default_wrapper_options do
    UsersController.wrap_parameters include: :username

    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu", "title" => "Developer" }
    assert_parameters("username" => "sikachu", "title" => "Developer", "user" => { "username" => "sikachu" })
  end
end
test_specify_wrapper_model() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 92
def test_specify_wrapper_model
  with_default_wrapper_options do
    UsersController.wrap_parameters Person

    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu" }
    assert_parameters("username" => "sikachu", "person" => { "username" => "sikachu" })
  end
end
test_specify_wrapper_name() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 82
def test_specify_wrapper_name
  with_default_wrapper_options do
    UsersController.wrap_parameters :person

    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu" }
    assert_parameters("username" => "sikachu", "person" => { "username" => "sikachu" })
  end
end
test_store_accessors_wrapped() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 71
def test_store_accessors_wrapped
  assert_called(User, :attribute_names, times: 2, returns: ["username"]) do
    with_default_wrapper_options do
      @request.env["CONTENT_TYPE"] = "application/json"
      post :parse, params: { "username" => "sikachu", "color" => "blue", "size" => "large" }
      assert_parameters("username" => "sikachu", "color" => "blue", "size" => "large",
                         "user" => { "username" => "sikachu", "color" => "blue", "size" => "large" })
    end
  end
end
test_wrap_parameters_false() click to toggle source
# File actionpack/test/controller/params_wrapper_test.rb, line 140
def test_wrap_parameters_false
  with_default_wrapper_options do
    UsersController.wrap_parameters false
    @request.env["CONTENT_TYPE"] = "application/json"
    post :parse, params: { "username" => "sikachu", "title" => "Developer" }
    assert_parameters("username" => "sikachu", "title" => "Developer")
  end
end