diff options
author | Prem Sichanugrist <s@sikachu.com> | 2011-04-28 15:56:11 +0700 |
---|---|---|
committer | Prem Sichanugrist <s@sikachu.com> | 2011-05-03 03:21:43 +0700 |
commit | 8c9e4d520291871e5319bc0e0a890527d8aea099 (patch) | |
tree | d6733837636fe79992e5deb9702b65f98c7cdd6a /actionpack/test/dispatch/request/json_params_parsing_test.rb | |
parent | 23eb81a3d1eb154a3aefe569408d1bb2ed63c554 (diff) | |
download | rails-8c9e4d520291871e5319bc0e0a890527d8aea099.tar.gz rails-8c9e4d520291871e5319bc0e0a890527d8aea099.tar.bz2 rails-8c9e4d520291871e5319bc0e0a890527d8aea099.zip |
Add `ActionController::ParamsWrapper` to wrap parameters into a nested hash
This will allow us to do a rootless JSON/XML request to server.
Diffstat (limited to 'actionpack/test/dispatch/request/json_params_parsing_test.rb')
-rw-r--r-- | actionpack/test/dispatch/request/json_params_parsing_test.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/actionpack/test/dispatch/request/json_params_parsing_test.rb b/actionpack/test/dispatch/request/json_params_parsing_test.rb index 34db7a4c66..d854d55173 100644 --- a/actionpack/test/dispatch/request/json_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/json_params_parsing_test.rb @@ -63,3 +63,56 @@ class JsonParamsParsingTest < ActionDispatch::IntegrationTest end end end + +class RootLessJSONParamsParsingTest < ActionDispatch::IntegrationTest + class UsersController < ActionController::Base + wrap_parameters :format => :json + + class << self + attr_accessor :last_request_parameters, :last_parameters + end + + def parse + self.class.last_request_parameters = request.request_parameters + self.class.last_parameters = params + head :ok + end + end + + def teardown + UsersController.last_request_parameters = nil + end + + test "parses json params for application json" do + assert_parses( + {"user" => {"username" => "sikachu"}, "username" => "sikachu"}, + "{\"username\": \"sikachu\"}", { 'CONTENT_TYPE' => 'application/json' } + ) + end + + test "parses json params for application jsonrequest" do + assert_parses( + {"user" => {"username" => "sikachu"}, "username" => "sikachu"}, + "{\"username\": \"sikachu\"}", { 'CONTENT_TYPE' => 'application/jsonrequest' } + ) + end + + private + def assert_parses(expected, actual, headers = {}) + with_test_routing(UsersController) do + post "/parse", actual, headers + assert_response :ok + assert_equal(expected, UsersController.last_request_parameters) + assert_equal(expected.merge({"action" => "parse"}), UsersController.last_parameters) + end + end + + def with_test_routing(controller) + with_routing do |set| + set.draw do + match ':action', :to => controller + end + yield + end + end +end |