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

Changeset 6742

Show
Ignore:
Timestamp:
05/15/07 22:10:03 (1 year ago)
Author:
bitsweat
Message:

Move request parameter parsing from CGI to AbstractRequest.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/lib/action_controller/cgi_ext/parameters.rb

    r6740 r6742  
    6161 
    6262          parser.result 
    63         end 
    64  
    65         def parse_formatted_request_parameters(mime_type, body) 
    66           case strategy = ActionController::Base.param_parsers[mime_type] 
    67             when Proc 
    68               strategy.call(body) 
    69             when :xml_simple, :xml_node 
    70               body.blank? ? {} : Hash.from_xml(body).with_indifferent_access 
    71             when :yaml 
    72               YAML.load(body) 
    73           end 
    74         rescue Exception => e # YAML, XML or Ruby code block errors 
    75           { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, 
    76             "body" => body, "format" => mime_type } 
    7763        end 
    7864 
  • trunk/actionpack/lib/action_controller/cgi_process.rb

    r6740 r6742  
    7777      @request_parameters ||= 
    7878        if ActionController::Base.param_parsers.has_key?(content_type) 
    79           CGI.parse_formatted_request_parameters(content_type, body.read) 
     79          self.class.parse_formatted_request_parameters(content_type, body.read) 
    8080        else 
    8181          CGI.parse_request_parameters(@cgi.params) 
  • trunk/actionpack/lib/action_controller/request.rb

    r6741 r6742  
    296296    def reset_session #:nodoc: 
    297297    end 
     298 
     299 
     300    def self.parse_formatted_request_parameters(mime_type, body) 
     301      case strategy = ActionController::Base.param_parsers[mime_type] 
     302        when Proc 
     303          strategy.call(body) 
     304        when :xml_simple, :xml_node 
     305          body.blank? ? {} : Hash.from_xml(body).with_indifferent_access 
     306        when :yaml 
     307          YAML.load(body) 
     308        else 
     309          {} 
     310      end 
     311    rescue Exception => e # YAML, XML or Ruby code block errors 
     312      { "exception" => "#{e.message} (#{e.class})", "backtrace" => e.backtrace, 
     313        "body" => body, "format" => mime_type } 
     314    end 
    298315  end 
    299316end 
  • trunk/actionpack/test/controller/cgi_test.rb

    r6733 r6742  
    275275 
    276276 
    277 class XmlCGITest < Test::Unit::TestCase 
    278   def test_single_file 
    279     raw_post_data =  
    280       "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{Base64.encode64('ABC')}</avatar></person>" 
    281     person = CGI.parse_formatted_request_parameters(Mime::XML, raw_post_data) 
    282     assert_equal "image/jpg", person['person']['avatar'].content_type 
    283     assert_equal "me.jpg", person['person']['avatar'].original_filename 
    284     assert_equal "ABC", person['person']['avatar'].read 
    285   end 
    286  
    287   def test_multiple_files 
    288     raw_post_data =  
    289       "<person><name>David</name><avatars>" + 
    290       "<avatar type='file' name='me.jpg' content_type='image/jpg'>#{Base64.encode64('ABC')}</avatar>" + 
    291       "<avatar type='file' name='you.gif' content_type='image/gif'>#{Base64.encode64('DEF')}</avatar>" + 
    292       "</avatars></person>" 
    293     person = CGI.parse_formatted_request_parameters(Mime::XML, raw_post_data) 
    294  
    295     assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type 
    296     assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename 
    297     assert_equal "ABC", person['person']['avatars']['avatar'].first.read 
    298  
    299     assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type 
    300     assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename 
    301     assert_equal "DEF", person['person']['avatars']['avatar'].last.read 
    302   end 
    303 end 
    304  
    305  
    306277class MultipartCGITest < Test::Unit::TestCase 
    307278  FIXTURE_PATH = File.dirname(__FILE__) + '/../fixtures/multipart' 
     
    380351  private 
    381352    def process(name) 
    382       old_stdin = $stdin 
    383353      File.open(File.join(FIXTURE_PATH, name), 'rb') do |file| 
    384354        ENV['CONTENT_LENGTH'] = file.stat.size.to_s 
    385         $stdin = file 
    386         @cgi = CGI.new 
     355        @cgi = CGI.new('query', file) 
    387356        CGI.parse_request_parameters @cgi.params 
    388357      end 
    389     ensure 
    390       $stdin = old_stdin 
    391358    end 
    392359end 
  • trunk/actionpack/test/controller/request_test.rb

    r6517 r6742  
    356356    end 
    357357end 
     358 
     359 
     360class RequestParameterParsingTest < Test::Unit::TestCase 
     361  def test_xml_with_single_file 
     362    body = "<person><name>David</name><avatar type='file' name='me.jpg' content_type='image/jpg'>#{Base64.encode64('ABC')}</avatar></person>" 
     363 
     364    person = ActionController::AbstractRequest.parse_formatted_request_parameters(Mime::XML, body) 
     365 
     366    assert_equal "image/jpg", person['person']['avatar'].content_type 
     367    assert_equal "me.jpg", person['person']['avatar'].original_filename 
     368    assert_equal "ABC", person['person']['avatar'].read 
     369  end 
     370 
     371  def test_xml_with_multiple_files 
     372    body = <<-end_body 
     373      <person> 
     374        <name>David</name> 
     375        <avatars> 
     376          <avatar type='file' name='me.jpg' content_type='image/jpg'>#{Base64.encode64('ABC')}</avatar> 
     377          <avatar type='file' name='you.gif' content_type='image/gif'>#{Base64.encode64('DEF')}</avatar> 
     378        </avatars> 
     379      </person> 
     380    end_body 
     381 
     382    person = ActionController::AbstractRequest.parse_formatted_request_parameters(Mime::XML, body) 
     383 
     384    assert_equal "image/jpg", person['person']['avatars']['avatar'].first.content_type 
     385    assert_equal "me.jpg", person['person']['avatars']['avatar'].first.original_filename 
     386    assert_equal "ABC", person['person']['avatars']['avatar'].first.read 
     387 
     388    assert_equal "image/gif", person['person']['avatars']['avatar'].last.content_type 
     389    assert_equal "you.gif", person['person']['avatars']['avatar'].last.original_filename 
     390    assert_equal "DEF", person['person']['avatars']['avatar'].last.read 
     391  end 
     392end 
     393