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

Returns a new instance of UploadedFile.



21
22
23
24
25
26
27
28
29
# File 'lib/rack/test/uploaded_file.rb', line 21

def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil)
  if content.respond_to?(:read)
    initialize_from_io(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:



37
38
39
# File 'lib/rack/test/uploaded_file.rb', line 37

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



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

def content_type
  @content_type
end

#original_filenameObject (readonly)

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



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

def original_filename
  @original_filename
end

#tempfileObject (readonly)

The tempfile



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

def tempfile
  @tempfile
end

Class Method Details

.actually_finalize(file) ⇒ Object



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

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

.finalize(file) ⇒ Object



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

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

Instance Method Details

#pathObject Also known as: local_path



31
32
33
# File 'lib/rack/test/uploaded_file.rb', line 31

def path
  tempfile.path
end

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

:nodoc:

Returns:

  • (Boolean)


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

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