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

Ticket #11091: integ_upload_post_11163.diff

File integ_upload_post_11163.diff, 4.5 kB (added by RubyRedRick, 2 years ago)
  • actionpack/test/controller/integration_upload_test.rb

    old new  
     1require 'abstract_unit' 
     2require 'action_controller/integration' 
     3require 'action_controller/routing' 
     4 
     5unless defined? ApplicationController 
     6  class ApplicationController < ActionController::Base 
     7  end 
     8end 
     9 
     10class UploadTestController < ActionController::Base 
     11  session :off 
     12   
     13  def update 
     14    SessionUploadTest.last_request_type = ActionController::Base.param_parsers[request.content_type] 
     15    render :text => "got here" 
     16  end  
     17end 
     18 
     19class SessionUploadTest < ActionController::IntegrationTest 
     20  FILES_DIR = File.dirname(__FILE__) + '/../fixtures/multipart' 
     21 
     22  class << self 
     23    attr_accessor :last_request_type 
     24  end 
     25   
     26 
     27  # def setup 
     28  #   @session = ActionController::Integration::Session.new 
     29  # end 
     30  #  
     31  def test_post_with_upload 
     32    uses_mocha "test_post_with_upload" do 
     33      Dependencies.stubs(:load?).returns(false)       
     34      with_routing do |set| 
     35        set.draw do |map| 
     36          map.update 'update', :controller => "upload_test", :action => "update", :method => :post 
     37        end 
     38        path = "/update" 
     39        params = {:uploaded_data => fixture_file_upload(FILES_DIR + "/mona_lisa.jpg", "image/jpg")} 
     40        headers = {:location => 'blah' } 
     41        post(path,params,headers) 
     42        assert_equal(:multipart_form, SessionUploadTest.last_request_type) 
     43      end 
     44    end 
     45     
     46   end 
     47end 
  • actionpack/lib/action_controller/integration.rb

    old new  
    5353      attr_reader :response 
    5454 
    5555      # A running counter of the number of requests processed. 
    56       attr_accessor :request_count 
     56      attr_accessor :request_count   
     57       
     58      class MultiPartNeededException < Exception 
     59      end 
    5760 
    5861      # Create and initialize a new +Session+ instance. 
    5962      def initialize 
     
    294297 
    295298          parse_result 
    296299          return status 
     300        rescue MultiPartNeededException 
     301          boundary = "----------XnJLe9ZIbbGUYtzPQJ16u1" 
     302          status = process(method, path, multipart_body(parameters, boundary), (headers || {}).merge({"CONTENT_TYPE" => "multipart/form-data; boundary=#{boundary}"})) 
     303          return status 
    297304        end 
    298305 
    299306        # Parses the result of the response and extracts the various values, 
     
    342349        # Convert the given parameters to a request string. The parameters may 
    343350        # be a string, +nil+, or a Hash. 
    344351        def requestify(parameters, prefix=nil) 
    345           if Hash === parameters 
     352          if TestUploadedFile === parameters 
     353            raise MultiPartNeededException 
     354          elsif Hash === parameters 
    346355            return nil if parameters.empty? 
    347356            parameters.map { |k,v| requestify(v, name_with_prefix(prefix, k)) }.join("&") 
    348357          elsif Array === parameters 
     
    353362            "#{CGI.escape(prefix)}=#{CGI.escape(parameters.to_s)}" 
    354363          end 
    355364        end 
     365 
     366        def multipart_requestify(params, first=true) 
     367          returning p = {} do 
     368            params.each do |key, value| 
     369              k = first ? CGI.escape(key.to_s) : "[#{CGI.escape(key.to_s)}]" 
     370              if Hash === value 
     371                multipart_requestify(value, false).each do |subkey, subvalue| 
     372                  p[k + subkey] = subvalue 
     373                end 
     374              else 
     375                p[k] = value 
     376              end 
     377            end 
     378          end 
     379        end 
     380 
     381        def multipart_body(params, boundary) 
     382          multipart_requestify(params).map do |key, value| 
     383            if value.respond_to?(:original_filename) 
     384              File.open(value.path) do |f| 
     385                <<-EOF 
     386--#{boundary}\r 
     387Content-Disposition: form-data; name="#{key}"; filename="#{CGI.escape(value.original_filename)}"\r 
     388Content-Type: #{value.content_type}\r 
     389Content-Length: #{File.stat(value.path).size}\r 
     390\r 
     391#{f.read}\r 
     392EOF 
     393              end 
     394            else 
     395<<-EOF 
     396--#{boundary}\r 
     397Content-Disposition: form-data; name="#{key}"\r 
     398\r 
     399#{value}\r 
     400EOF 
     401            end 
     402          end.join("")+"--#{boundary}--\r" 
     403        end 
    356404    end 
    357405 
    358406    # A module used to extend ActionController::Base, so that integration tests