Class: Rack::Test::UploadedFile

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/test/uploaded_file.rb

Overview

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")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, content_type = 'text/plain', binary = false, original_filename: nil) ⇒ UploadedFile

Creates a new UploadedFile instance.

Parameters:

  • content (IO, Pathname, String, StringIO)

    a path to a file, or an IO or StringIO object representing the file.

  • content_type (String) (defaults to: 'text/plain')
  • binary (Boolean) (defaults to: false)

    an optional flag that indicates whether the file should be open in binary mode or not.

  • original_filename (String) (defaults to: nil)

    an optional parameter that provides the original filename if ‘content` is a StringIO object. Not used for other kind of `content` objects.



30
31
32
33
34
35
36
37
38
# File 'lib/rack/test/uploaded_file.rb', line 30

def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil)
  if original_filename
    initialize_from_stringio(content, original_filename)
  else
    initialize_from_file_path(content)
  end
  @content_type = content_type
  @tempfile.binmode if binary
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

:nodoc:



46
47
48
# File 'lib/rack/test/uploaded_file.rb', line 46

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

Instance Attribute Details

#content_typeObject

The content type of the “uploaded” file



20
21
22
# File 'lib/rack/test/uploaded_file.rb', line 20

def content_type
  @content_type
end

#original_filenameObject (readonly)

The filename, not including the path, of the “uploaded” file



14
15
16
# File 'lib/rack/test/uploaded_file.rb', line 14

def original_filename
  @original_filename
end

#tempfileObject (readonly)

The tempfile



17
18
19
# File 'lib/rack/test/uploaded_file.rb', line 17

def tempfile
  @tempfile
end

Class Method Details

.actually_finalize(file) ⇒ Object



58
59
60
61
# File 'lib/rack/test/uploaded_file.rb', line 58

def self.actually_finalize(file)
  file.close
  file.unlink
end

.finalize(file) ⇒ Object



54
55
56
# File 'lib/rack/test/uploaded_file.rb', line 54

def self.finalize(file)
  proc { actually_finalize file }
end

Instance Method Details

#pathObject Also known as: local_path



40
41
42
# File 'lib/rack/test/uploaded_file.rb', line 40

def path
  tempfile.path
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


50
51
52
# File 'lib/rack/test/uploaded_file.rb', line 50

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