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

Ticket #9935: json_request_parsing.2.diff

File json_request_parsing.2.diff, 3.3 kB (added by Assaf, 1 year ago)

Map JSON object from request to :json parameter

  • 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    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 
     832end 
  • 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            json = ActiveSupport::JSON.decode(body) 
     378            raise ActionControllerError, "Can only accept JSON objects, not arrays" unless Hash === json 
     379            { :json => json }.with_indifferent_access 
    376380          when :yaml 
    377381            YAML.load(body) 
    378382          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.