Ticket #9935: json_request_parsing.diff
| File json_request_parsing.diff, 3.1 kB (added by Assaf, 1 year ago) |
|---|
-
test/controller/request_test.rb
old new 327 327 assert_equal Mime::TEXT, @request.format 328 328 end 329 329 330 def test_json_format 331 @request.instance_eval { @parameters = { :format => 'json' } } 332 assert_equal Mime::JSON, @request.format 333 end 334 330 335 def test_nil_format 331 336 @request.instance_eval { @parameters = { :format => nil } } 332 337 @request.env["HTTP_ACCEPT"] = "text/javascript" … … 352 357 assert_equal Mime::XML, @request.content_type 353 358 end 354 359 360 def test_content_type_json 361 @request.env["CONTENT_TYPE"] = "application/json" 362 assert_equal Mime::JSON, @request.content_type 363 end 364 355 365 def test_user_agent 356 366 assert_not_nil @request.user_agent 357 367 end … … 782 792 ActionController::CgiRequest.new(cgi).request_parameters 783 793 end 784 794 end 795 796 797 class JsonParamsParsingTest < Test::Unit::TestCase 798 def test_json_hash 799 person = parse_body(<<-JSON) 800 { person: { 801 name: "David", 802 url: "http://rubyonrails.org/", 803 count: 1 804 } } 805 JSON 806 807 assert_equal 'David', person['person']['name'] 808 assert_equal 'http://rubyonrails.org/', person['person']['url'] 809 assert_equal 1, person['person']['count'] 810 end 811 812 def test_json_array 813 person = parse_body(<<-JSON) 814 [ { name: "David" }, 815 { name: "Allan" } ] 816 JSON 817 818 assert_equal 'David', person.first['name'] 819 assert_equal 'Allan', person.last['name'] 820 end 821 822 private 823 def parse_body(body) 824 env = { 'CONTENT_TYPE' => 'application/json', 825 'CONTENT_LENGTH' => body.size.to_s } 826 cgi = ActionController::Integration::Session::MockCGI.new(env, body) 827 ActionController::CgiRequest.new(cgi).request_parameters 828 end 829 end -
lib/action_controller/request.rb
old new 373 373 self.class.parse_multipart_form_parameters(body, boundary, content_length, env) 374 374 when :xml_simple, :xml_node 375 375 body.blank? ? {} : Hash.from_xml(body).with_indifferent_access 376 when :json 377 body.blank? ? {} : ActiveSupport::JSON.decode(body) 376 378 when :yaml 377 379 YAML.load(body) 378 380 else -
lib/action_controller/base.rb
old new 306 306 # ActionController::Base.param_parsers[Mime::YAML] = :yaml 307 307 @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form, 308 308 Mime::URL_ENCODED_FORM => :url_encoded_form, 309 Mime::XML => :xml_simple } 309 Mime::XML => :xml_simple, 310 Mime::JSON => :json } 310 311 cattr_accessor :param_parsers 311 312 312 313 # Controls the default charset for all renders.