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.


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

def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil)
  case content
  when StringIO
    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:


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

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


74
75
76
77
# File 'lib/rack/test/uploaded_file.rb', line 74

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

.finalize(file) ⇒ Object


70
71
72
# File 'lib/rack/test/uploaded_file.rb', line 70

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

Instance Method Details

#append_to(buffer) ⇒ Object

Append to given buffer in 64K chunks to avoid multiple large copies of file data in memory. Rewind tempfile before and after to make sure all data in tempfile is appended to the buffer.


55
56
57
58
59
60
61
62
63
64
# File 'lib/rack/test/uploaded_file.rb', line 55

def append_to(buffer)
  tempfile.rewind

  buf = String.new
  buffer << tempfile.readpartial(65_536, buf) until tempfile.eof?

  tempfile.rewind

  nil
end

#pathObject Also known as: local_path


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

def path
  tempfile.path
end

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

:nodoc:

Returns:

  • (Boolean)

66
67
68
# File 'lib/rack/test/uploaded_file.rb', line 66

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