Sha256: 07b9fc51f6f600cde9b686f5437bb8b1c8470f5a228e94c9e9a56707403909fd

Contents?: true

Size: 1.37 KB

Versions: 152

Compression:

Stored size: 1.37 KB

Contents

require "tempfile"
require "fileutils"

module Rack
  module Test

    # Wraps a Tempfile with a content type. Including one or more UploadedFile's
    # in the params causes Rack::Test to build and issue a multipart request.
    #
    # Example:
    #   post "/photos", "file" => Rack::Test::UploadedFile.new("me.jpg", "image/jpeg")
    class UploadedFile

      # The filename, *not* including the path, of the "uploaded" file
      attr_reader :original_filename

      # The content type of the "uploaded" file
      attr_accessor :content_type

      def initialize(path, content_type = "text/plain", binary = false)
        raise "#{path} file does not exist" unless ::File.exist?(path)

        @content_type = content_type
        @original_filename = ::File.basename(path)

        @tempfile = Tempfile.new(@original_filename)
        @tempfile.set_encoding(Encoding::BINARY) if @tempfile.respond_to?(:set_encoding)
        @tempfile.binmode if binary

        FileUtils.copy_file(path, @tempfile.path)
      end

      def path
        @tempfile.path
      end

      alias_method :local_path, :path

      def method_missing(method_name, *args, &block) #:nodoc:
        @tempfile.__send__(method_name, *args, &block)
      end

      def respond_to?(method_name, include_private = false) #:nodoc:
        @tempfile.respond_to?(method_name, include_private) || super
      end

    end

  end
end

Version data entries

152 entries across 127 versions & 26 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/rack-test-0.6.1/lib/rack/test/uploaded_file.rb
classiccms-0.7.4 vendor/bundle/gems/rack-test-0.6.1/lib/rack/test/uploaded_file.rb
classiccms-0.7.3 vendor/bundle/gems/rack-test-0.6.1/lib/rack/test/uploaded_file.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
nanumfont-rails-0.1 vendor/bundle/ruby/2.1.0/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
mango-0.8.0 vendor/bundler/ruby/2.1.0/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
challah-1.0.0 vendor/bundle/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
classiccms-0.7.2 vendor/bundle/gems/rack-test-0.6.1/lib/rack/test/uploaded_file.rb
classiccms-0.7.1 vendor/bundle/gems/rack-test-0.6.1/lib/rack/test/uploaded_file.rb
swipe-rails-0.0.5 vendor/bundle/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
active_mailer-0.0.9 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
active_mailer-0.0.8 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
active_mailer-0.0.7 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
active_mailer-0.0.6 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/rack-test-0.6.2/lib/rack/test/uploaded_file.rb
classiccms-0.7.0 vendor/bundle/gems/rack-test-0.6.1/lib/rack/test/uploaded_file.rb