From 8c9e4d520291871e5319bc0e0a890527d8aea099 Mon Sep 17 00:00:00 2001 From: Prem Sichanugrist Date: Thu, 28 Apr 2011 15:56:11 +0700 Subject: Add `ActionController::ParamsWrapper` to wrap parameters into a nested hash This will allow us to do a rootless JSON/XML request to server. --- actionpack/test/controller/params_wrapper_test.rb | 187 ++++++++++++++++++++++ 1 file changed, 187 insertions(+) create mode 100644 actionpack/test/controller/params_wrapper_test.rb (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb new file mode 100644 index 0000000000..2e5d096fcd --- /dev/null +++ b/actionpack/test/controller/params_wrapper_test.rb @@ -0,0 +1,187 @@ +require 'abstract_unit' + +module Admin; class User; end; end + +class ParamsWrapperTest < ActionController::TestCase + class UsersController < ActionController::Base + def test + render :json => params.except(:controller, :action) + end + end + + class User; end + class Person; end + + tests UsersController + + def test_derivered_name_from_controller + with_default_wrapper_options do + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu' } + assert_equal '{"username":"sikachu","user":{"username":"sikachu"}}', @response.body + end + end + + def test_specify_wrapper_name + with_default_wrapper_options do + UsersController.wrap_parameters :person + + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu' } + assert_equal '{"username":"sikachu","person":{"username":"sikachu"}}', @response.body + end + end + + def test_specify_wrapper_model + with_default_wrapper_options do + UsersController.wrap_parameters Person + + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu' } + assert_equal '{"username":"sikachu","person":{"username":"sikachu"}}', @response.body + end + end + + def test_specify_only_option + with_default_wrapper_options do + UsersController.wrap_parameters :only => :username + + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body + end + end + + def test_specify_except_option + with_default_wrapper_options do + UsersController.wrap_parameters :except => :title + + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body + end + end + + def test_specify_both_wrapper_name_and_only_option + with_default_wrapper_options do + UsersController.wrap_parameters :person, :only => :username + + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer","person":{"username":"sikachu"}}', @response.body + end + end + + def test_not_enabled_format + with_default_wrapper_options do + @request.env['CONTENT_TYPE'] = 'application/xml' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer"}', @response.body + end + end + + def test_specify_format + with_default_wrapper_options do + UsersController.wrap_parameters :format => :xml + + @request.env['CONTENT_TYPE'] = 'application/xml' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu","title":"Developer"}}', @response.body + end + end + + def test_not_wrap_reserved_parameters + with_default_wrapper_options do + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '☃', 'username' => 'sikachu' } + assert_equal '{"authenticity_token":"pwned","_method":"put","utf8":"☃","username":"sikachu","user":{"username":"sikachu"}}', @response.body + end + end + + def test_no_double_wrap_if_key_exists + with_default_wrapper_options do + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'user' => { 'username' => 'sikachu' }} + assert_equal '{"user":{"username":"sikachu"}}', @response.body + end + end + + def test_nested_params + with_default_wrapper_options do + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'person' => { 'username' => 'sikachu' }} + assert_equal '{"person":{"username":"sikachu"},"user":{"person":{"username":"sikachu"}}}', @response.body + end + end + + def test_derived_wrapped_keys_from_matching_model + with_default_wrapper_options do + User.expects(:respond_to?).with(:column_names).returns(true) + User.expects(:column_names).returns(["username"]) + + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body + end + end + + def test_derived_wrapped_keys_from_specified_model + with_default_wrapper_options do + Person.expects(:respond_to?).with(:column_names).returns(true) + Person.expects(:column_names).returns(["username"]) + + UsersController.wrap_parameters Person + + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer","person":{"username":"sikachu"}}', @response.body + end + end + + private + def with_default_wrapper_options(&block) + @controller.class._wrapper_options = {:format => [:json]} + @controller.class.inherited(@controller.class) + yield + end +end + +class NamespacedParamsWrapperTest < ActionController::TestCase + module Admin + class UsersController < ActionController::Base + def test + render :json => params.except(:controller, :action) + end + end + + class User; end + end + class User; end + class Person; end + + tests Admin::UsersController + + def test_derivered_name_from_controller + with_default_wrapper_options do + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu' } + assert_equal '{"username":"sikachu","user":{"username":"sikachu"}}', @response.body + end + end + + def test_namespace_lookup_when_namespaced_model_available + with_default_wrapper_options do + Admin::User.expects(:respond_to?).with(:column_names).returns(false) + + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu' } + end + end + + private + def with_default_wrapper_options(&block) + @controller.class._wrapper_options = {:format => [:json]} + @controller.class.inherited(@controller.class) + yield + end +end -- cgit v1.2.3 From 4bddc06e83acecce662b4282159c5eb0096c4783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 3 May 2011 00:37:40 +0200 Subject: Move most processing to load time for performance and improve test suite. --- actionpack/test/controller/params_wrapper_test.rb | 39 ++++++++++++++++------- 1 file changed, 27 insertions(+), 12 deletions(-) (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb index 2e5d096fcd..314b27cf47 100644 --- a/actionpack/test/controller/params_wrapper_test.rb +++ b/actionpack/test/controller/params_wrapper_test.rb @@ -80,6 +80,15 @@ class ParamsWrapperTest < ActionController::TestCase end end + def test_wrap_parameters_false + with_default_wrapper_options do + UsersController.wrap_parameters false + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer"}', @response.body + end + end + def test_specify_format with_default_wrapper_options do UsersController.wrap_parameters :format => :xml @@ -115,10 +124,10 @@ class ParamsWrapperTest < ActionController::TestCase end def test_derived_wrapped_keys_from_matching_model - with_default_wrapper_options do - User.expects(:respond_to?).with(:column_names).returns(true) - User.expects(:column_names).returns(["username"]) + User.expects(:respond_to?).with(:column_names).returns(true) + User.expects(:column_names).returns(["username"]) + with_default_wrapper_options do @request.env['CONTENT_TYPE'] = 'application/json' post :test, { 'username' => 'sikachu', 'title' => 'Developer' } assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body @@ -153,11 +162,13 @@ class NamespacedParamsWrapperTest < ActionController::TestCase render :json => params.except(:controller, :action) end end + end - class User; end + class Sample + def self.column_names + ["username"] + end end - class User; end - class Person; end tests Admin::UsersController @@ -169,12 +180,16 @@ class NamespacedParamsWrapperTest < ActionController::TestCase end end - def test_namespace_lookup_when_namespaced_model_available - with_default_wrapper_options do - Admin::User.expects(:respond_to?).with(:column_names).returns(false) - - @request.env['CONTENT_TYPE'] = 'application/json' - post :test, { 'username' => 'sikachu' } + def test_namespace_lookup_from_model + Admin.const_set(:User, Class.new(Sample)) + begin + with_default_wrapper_options do + @request.env['CONTENT_TYPE'] = 'application/json' + post :test, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_equal '{"username":"sikachu","title":"Developer","user":{"username":"sikachu"}}', @response.body + end + ensure + Admin.send :remove_const, :User end end -- cgit v1.2.3 From a55f2de0c5baae589b1730df1e4068f0cd1474ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 3 May 2011 01:03:21 +0200 Subject: Improve performance for filtered parameters and add tests. --- actionpack/test/controller/log_subscriber_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/log_subscriber_test.rb b/actionpack/test/controller/log_subscriber_test.rb index ddfa3df552..5d7a51e902 100644 --- a/actionpack/test/controller/log_subscriber_test.rb +++ b/actionpack/test/controller/log_subscriber_test.rb @@ -4,6 +4,8 @@ require "action_controller/log_subscriber" module Another class LogSubscribersController < ActionController::Base + wrap_parameters :person, :only => :name, :format => :json + def show render :nothing => true end @@ -95,6 +97,15 @@ class ACLogSubscriberTest < ActionController::TestCase assert_equal 'Parameters: {"id"=>"10"}', logs[1] end + def test_process_action_with_wrapped_parameters + @request.env['CONTENT_TYPE'] = 'application/json' + post :show, :id => '10', :name => 'jose' + wait + + assert_equal 3, logs.size + assert_match '"person"=>{"name"=>"jose"}', logs[1] + end + def test_process_action_with_view_runtime get :show wait -- cgit v1.2.3