diff options
author | Joshua Peek <josh@joshpeek.com> | 2009-01-09 15:43:32 -0600 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2009-01-09 15:43:32 -0600 |
commit | 38a723e6ea770eb8a902ee85ef40f6a22e7cd1b8 (patch) | |
tree | 884b212b80e2de484e2041992eb8d6fdb773b442 /actionpack/test/controller | |
parent | ac4bf1180aa0f82616038522bddaf3ff3d5020c8 (diff) | |
download | rails-38a723e6ea770eb8a902ee85ef40f6a22e7cd1b8.tar.gz rails-38a723e6ea770eb8a902ee85ef40f6a22e7cd1b8.tar.bz2 rails-38a723e6ea770eb8a902ee85ef40f6a22e7cd1b8.zip |
Refactor request json params parsing tests
Diffstat (limited to 'actionpack/test/controller')
-rw-r--r-- | actionpack/test/controller/request/json_params_parsing_test.rb | 45 | ||||
-rw-r--r-- | actionpack/test/controller/request_test.rb | 22 |
2 files changed, 45 insertions, 22 deletions
diff --git a/actionpack/test/controller/request/json_params_parsing_test.rb b/actionpack/test/controller/request/json_params_parsing_test.rb new file mode 100644 index 0000000000..a3dde72c4e --- /dev/null +++ b/actionpack/test/controller/request/json_params_parsing_test.rb @@ -0,0 +1,45 @@ +require 'abstract_unit' + +class JsonParamsParsingTest < ActionController::IntegrationTest + class TestController < ActionController::Base + class << self + attr_accessor :last_request_parameters + end + + def parse + self.class.last_request_parameters = request.request_parameters + head :ok + end + end + + def teardown + TestController.last_request_parameters = nil + end + + test "parses json params for application json" do + assert_parses( + {"person" => {"name" => "David"}}, + "{\"person\": {\"name\": \"David\"}}", { 'CONTENT_TYPE' => 'application/json' } + ) + end + + test "parses json params for application jsonrequest" do + assert_parses( + {"person" => {"name" => "David"}}, + "{\"person\": {\"name\": \"David\"}}", { 'CONTENT_TYPE' => 'application/jsonrequest' } + ) + end + + private + def assert_parses(expected, actual, headers = {}) + with_routing do |set| + set.draw do |map| + map.connect ':action', :controller => "json_params_parsing_test/test" + end + + post "/parse", actual, headers + assert_response :ok + assert_equal(expected, TestController.last_request_parameters) + end + end +end diff --git a/actionpack/test/controller/request_test.rb b/actionpack/test/controller/request_test.rb index 64cc3f5291..2eb2693644 100644 --- a/actionpack/test/controller/request_test.rb +++ b/actionpack/test/controller/request_test.rb @@ -764,25 +764,3 @@ class LegacyXmlParamsParsingTest < XmlParamsParsingTest ActionController::Request.new(env).request_parameters end end - -class JsonParamsParsingTest < ActiveSupport::TestCase - def test_hash_params_for_application_json - person = parse_body({:person => {:name => "David"}}.to_json,'application/json')[:person] - assert_kind_of Hash, person - assert_equal 'David', person['name'] - end - - def test_hash_params_for_application_jsonrequest - person = parse_body({:person => {:name => "David"}}.to_json,'application/jsonrequest')[:person] - assert_kind_of Hash, person - assert_equal 'David', person['name'] - end - - private - def parse_body(body,content_type) - env = { 'rack.input' => StringIO.new(body), - 'CONTENT_TYPE' => content_type, - 'CONTENT_LENGTH' => body.size.to_s } - ActionController::Request.new(env).request_parameters - end -end |