Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

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  
    327327    assert_equal Mime::TEXT, @request.format 
    328328  end 
    329329 
     330  def test_json_format 
     331    @request.instance_eval { @parameters = { :format => 'json' } } 
     332    assert_equal Mime::JSON, @request.format 
     333  end 
     334 
    330335  def test_nil_format 
    331336    @request.instance_eval { @parameters = { :format => nil } } 
    332337    @request.env["HTTP_ACCEPT"] = "text/javascript" 
     
    352357    assert_equal Mime::XML, @request.content_type 
    353358  end 
    354359   
     360  def test_content_type_json 
     361    @request.env["CONTENT_TYPE"] = "application/json" 
     362    assert_equal Mime::JSON, @request.content_type 
     363  end 
     364 
    355365  def test_user_agent 
    356366    assert_not_nil @request.user_agent 
    357367  end 
     
    782792      ActionController::CgiRequest.new(cgi).request_parameters 
    783793    end 
    784794end 
     795 
     796 
     797class 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 
     829end 
  • lib/action_controller/request.rb

    old new  
    373373            self.class.parse_multipart_form_parameters(body, boundary, content_length, env) 
    374374          when :xml_simple, :xml_node 
    375375            body.blank? ? {} : Hash.from_xml(body).with_indifferent_access 
     376          when :json 
     377            body.blank? ? {} : ActiveSupport::JSON.decode(body) 
    376378          when :yaml 
    377379            YAML.load(body) 
    378380          else 
  • lib/action_controller/base.rb

    old new  
    306306    #   ActionController::Base.param_parsers[Mime::YAML] = :yaml 
    307307    @@param_parsers = { Mime::MULTIPART_FORM => :multipart_form, 
    308308                        Mime::URL_ENCODED_FORM => :url_encoded_form, 
    309                         Mime::XML => :xml_simple } 
     309                        Mime::XML => :xml_simple, 
     310                        Mime::JSON => :json } 
    310311    cattr_accessor :param_parsers 
    311312 
    312313    # Controls the default charset for all renders.