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.

Arguments:

content

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

content_type

MIME type of the file

binary

Whether the file should be set to binmode (content treated as binary).

original_filename

The filename to use for the file. Required if content is StringIO, optional override if not



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rack/test/uploaded_file.rb', line 31

def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil)
  @content_type = content_type
  @original_filename = original_filename

  case content
  when StringIO
    initialize_from_stringio(content)
  else
    initialize_from_file_path(content)
  end

  @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

Delegate all methods not handled to the tempfile.



52
53
54
# File 'lib/rack/test/uploaded_file.rb', line 52

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

Instance Attribute Details

#content_typeObject

The content type of the “uploaded” file



22
23
24
# File 'lib/rack/test/uploaded_file.rb', line 22

def content_type
  @content_type
end

#original_filenameObject (readonly)

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



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

def original_filename
  @original_filename
end

#tempfileObject (readonly)

The tempfile



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

def tempfile
  @tempfile
end

Class Method Details

.actually_finalize(file) ⇒ Object

Close and unlink the given file, used as a finalizer for the tempfile, if the tempfile is backed by a file in the filesystem.



82
83
84
85
# File 'lib/rack/test/uploaded_file.rb', line 82

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

.finalize(file) ⇒ Object

A proc that can be used as a finalizer to close and unlink the tempfile.



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

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.



60
61
62
63
64
65
66
67
68
69
# File 'lib/rack/test/uploaded_file.rb', line 60

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

The path to the tempfile. Will not work if the receiver’s content is from a StringIO.



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

def path
  tempfile.path
end

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

:nodoc:

Returns:

  • (Boolean)


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

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