diff options
author | José Valim <jose.valim@gmail.com> | 2011-05-03 02:57:16 -0700 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-05-03 02:57:16 -0700 |
commit | ce6fef9d8b3ed282d4735fcff3dcf7fe9d89e57d (patch) | |
tree | 692b029f58883e9da2ba72f3a9ea5973f56941ba /actionpack/test/controller | |
parent | 2fbbd08616c25dc2931eca914349dd2161766f69 (diff) | |
parent | 31f412fc975033a9293888e8618fc933097d1d35 (diff) | |
download | rails-ce6fef9d8b3ed282d4735fcff3dcf7fe9d89e57d.tar.gz rails-ce6fef9d8b3ed282d4735fcff3dcf7fe9d89e57d.tar.bz2 rails-ce6fef9d8b3ed282d4735fcff3dcf7fe9d89e57d.zip |
Merge pull request #373 from sikachu/bomb.
Fixing failing test case on 1.8.7 & bump Rack version
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r-- | actionpack/test/controller/params_wrapper_test.rb | 116 |
1 files changed, 80 insertions, 36 deletions
diff --git a/actionpack/test/controller/params_wrapper_test.rb b/actionpack/test/controller/params_wrapper_test.rb index 314b27cf47..d6f8f28d0d 100644 --- a/actionpack/test/controller/params_wrapper_test.rb +++ b/actionpack/test/controller/params_wrapper_test.rb @@ -4,8 +4,13 @@ module Admin; class User; end; end class ParamsWrapperTest < ActionController::TestCase class UsersController < ActionController::Base - def test - render :json => params.except(:controller, :action) + class << self + attr_accessor :last_parameters + end + + def parse + self.class.last_parameters = request.params.except(:controller, :action) + head :ok end end @@ -14,11 +19,15 @@ class ParamsWrapperTest < ActionController::TestCase tests UsersController + def teardown + UsersController.last_parameters = nil + end + 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 + post :parse, { 'username' => 'sikachu' } + assert_parameters({ 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }}) end end @@ -27,8 +36,8 @@ class ParamsWrapperTest < ActionController::TestCase UsersController.wrap_parameters :person @request.env['CONTENT_TYPE'] = 'application/json' - post :test, { 'username' => 'sikachu' } - assert_equal '{"username":"sikachu","person":{"username":"sikachu"}}', @response.body + post :parse, { 'username' => 'sikachu' } + assert_parameters({ 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }}) end end @@ -37,8 +46,8 @@ class ParamsWrapperTest < ActionController::TestCase UsersController.wrap_parameters Person @request.env['CONTENT_TYPE'] = 'application/json' - post :test, { 'username' => 'sikachu' } - assert_equal '{"username":"sikachu","person":{"username":"sikachu"}}', @response.body + post :parse, { 'username' => 'sikachu' } + assert_parameters({ 'username' => 'sikachu', 'person' => { 'username' => 'sikachu' }}) end end @@ -47,8 +56,8 @@ class ParamsWrapperTest < ActionController::TestCase 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }}) end end @@ -57,8 +66,8 @@ class ParamsWrapperTest < ActionController::TestCase 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }}) end end @@ -67,16 +76,16 @@ class ParamsWrapperTest < ActionController::TestCase 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }}) 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer' }) end end @@ -84,8 +93,8 @@ class ParamsWrapperTest < ActionController::TestCase 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer' }) end end @@ -94,32 +103,32 @@ class ParamsWrapperTest < ActionController::TestCase 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu', 'title' => 'Developer' }}) 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 + post :parse, { 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '☃', 'username' => 'sikachu' } + assert_parameters({ 'authenticity_token' => 'pwned', '_method' => 'put', 'utf8' => '☃', 'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }}) 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 + post :parse, { 'user' => { 'username' => 'sikachu' }} + assert_parameters({ 'user' => { 'username' => 'sikachu' }}) 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 + post :parse, { 'person' => { 'username' => 'sikachu' }} + assert_parameters({ 'person' => { 'username' => 'sikachu' }, 'user' => {'person' => { 'username' => 'sikachu' }}}) end end @@ -129,8 +138,8 @@ class ParamsWrapperTest < ActionController::TestCase 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }}) end end @@ -142,8 +151,8 @@ class ParamsWrapperTest < ActionController::TestCase 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'person' => { 'username' => 'sikachu' }}) end end @@ -153,13 +162,22 @@ class ParamsWrapperTest < ActionController::TestCase @controller.class.inherited(@controller.class) yield end + + def assert_parameters(expected) + assert_equal expected, UsersController.last_parameters + end end class NamespacedParamsWrapperTest < ActionController::TestCase module Admin class UsersController < ActionController::Base - def test - render :json => params.except(:controller, :action) + class << self + attr_accessor :last_parameters + end + + def parse + self.class.last_parameters = request.params.except(:controller, :action) + head :ok end end end @@ -172,11 +190,15 @@ class NamespacedParamsWrapperTest < ActionController::TestCase tests Admin::UsersController + def teardown + Admin::UsersController.last_parameters = nil + end + 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 + post :parse, { 'username' => 'sikachu' } + assert_parameters({'username' => 'sikachu', 'user' => { 'username' => 'sikachu' }}) end end @@ -185,18 +207,40 @@ class NamespacedParamsWrapperTest < ActionController::TestCase 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 + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }}) end ensure Admin.send :remove_const, :User end end + def test_heirarchy_namespace_lookup_from_model + # Make sure that we cleanup ::Admin::User + admin_user_constant = ::Admin::User + ::Admin.send :remove_const, :User + + Object.const_set(:User, Class.new(Sample)) + begin + with_default_wrapper_options do + @request.env['CONTENT_TYPE'] = 'application/json' + post :parse, { 'username' => 'sikachu', 'title' => 'Developer' } + assert_parameters({ 'username' => 'sikachu', 'title' => 'Developer', 'user' => { 'username' => 'sikachu' }}) + end + ensure + Object.send :remove_const, :User + ::Admin.const_set(:User, admin_user_constant) + end + end + private def with_default_wrapper_options(&block) @controller.class._wrapper_options = {:format => [:json]} @controller.class.inherited(@controller.class) yield end + + def assert_parameters(expected) + assert_equal expected, Admin::UsersController.last_parameters + end end |