Skip to content

Commit

Permalink
UploadedFile: Handle content being a Pathname
Browse files Browse the repository at this point in the history
Dynamic languages... Can't live with them, can't live without them.

In #149, we added support for `UploadedFile` being an `StringIO `object, by means of the `if content.respond_to?(:read)` logic. _However_, that had the unintended consequence of breaking the use case when a `Pathname` is provided instead of a string as the `content` paramer, since a `Pathname` _also_ responds to the `read` message...

This PR works around that, by adding some extra checking. As an added bonus, I also added some YARD comments to make it more clear what parameter types this method expects/accepts.

Fixes #207.
  • Loading branch information
perlun authored Nov 20, 2017
1 parent ea79a15 commit c767e6a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/rack/test/uploaded_file.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'tempfile'
require 'fileutils'
require 'pathname'
require 'tempfile'

module Rack
module Test
Expand All @@ -18,8 +19,16 @@ class UploadedFile
# The content type of the "uploaded" file
attr_accessor :content_type

# Creates a new UploadedFile instance.
#
# @param content [IO, Pathname, String, StringIO] a path to a file, or an {IO} or {StringIO} object representing the
# file.
# @param content_type [String]
# @param binary [Boolean] an optional flag that indicates whether the file should be open in binary mode or not.
# @param original_filename [String] an optional parameter that provides the original filename if `content` is an IO
# object.
def initialize(content, content_type = 'text/plain', binary = false, original_filename: nil)
if content.respond_to?(:read)
if content.respond_to?(:read) && !content.is_a?(Pathname)
initialize_from_io(content, original_filename)
else
initialize_from_file_path(content)
Expand Down

0 comments on commit c767e6a

Please sign in to comment.