Ticket #9935: json_request_parsing.2.diff
| File json_request_parsing.2.diff, 3.3 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 params = parse_body(<<-JSON) 800 { person: { 801 name: "David", 802 url: "http://rubyonrails.org/", 803 count: 1 804 } } 805 JSON 806 807 assert_equal 'David', params['json']['person']['name'] 808 assert_equal 'http://rubyonrails.org/', params['json']['person']['url'] 809 assert_equal 1, params[:json][:person][:count] 810 end 811 812 def test_json_fail_unless_hash 813 assert_raise Exception do 814 parse_body(<<-JSON) 815 [ { name: "David" }, 816 { name: "Allan" } ] 817 JSON 818 end 819 820 assert_raise Exception do 821 parse_body(%{"value"}) 822 end 823 end 824 825 private 826 def parse_body(body) 827 env = { 'CONTENT_TYPE' => 'application/json', 828 'CONTENT_LENGTH' => body.size.to_s } 829 cgi = ActionController::Integration::Session::MockCGI.new(env, body) 830 ActionController::CgiRequest.new(cgi).request_parameters 831 end 832 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 json = ActiveSupport::JSON.decode(body) 378 raise ActionControllerError, "Can only accept JSON objects, not arrays" unless Hash === json 379 { :json => json }.with_indifferent_access 376 380 when :yaml 377 381 YAML.load(body) 378 382 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.