Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

ensure potential FilePart responds to original_filename #20

Merged
merged 3 commits into from

3 participants

Steven Davidovitz Nick Sieger risk danger olson
Steven Davidovitz

Some gems modify String to include a content_type method. They generally
do not add an original_filename, however, and this causes problems when
creating a new FilePart

Steven Davidovitz steved555 ensure potential FilePart responds to original_filename
Some gems modify String to include a content_type method. They generally
do not add an original_filename, however, and this causes problems when
creating a new FilePart
f5bc945
lib/parts.rb
@@ -7,7 +7,7 @@
module Parts
module Part #:nodoc:
def self.new(boundary, name, value)
- if value.respond_to? :content_type
+ if value.respond_to?(:content_type) && value.respond_to?(:original_filename)
FilePart.new(boundary, name, value)

Any thoughts on making this a class method like:

Parts::Part.file?(value)

Then the line looks like:

if file?(value)

Swap file? out with a better name if you so desire.

Sounds good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Steven Davidovitz

Bump?

Nick Sieger nicksieger merged commit 57c6699 into from
Nick Sieger
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 6, 2012
  1. Steven Davidovitz

    ensure potential FilePart responds to original_filename

    steved555 authored
    Some gems modify String to include a content_type method. They generally
    do not add an original_filename, however, and this causes problems when
    creating a new FilePart
  2. Steven Davidovitz
  3. Steven Davidovitz

    refute -> assert !

    steved555 authored
This page is out of date. Refresh to see the latest.
Showing with 27 additions and 2 deletions.
  1. +1 −1  Gemfile.lock
  2. +5 −1 lib/parts.rb
  3. +21 −0 test/test_parts.rb
2  Gemfile.lock
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- multipart-post (1.1.4)
+ multipart-post (1.1.5)
GEM
remote: http://rubygems.org/
6 lib/parts.rb
View
@@ -7,13 +7,17 @@
module Parts
module Part #:nodoc:
def self.new(boundary, name, value)
- if value.respond_to? :content_type
+ if file?(value)
FilePart.new(boundary, name, value)
else
ParamPart.new(boundary, name, value)
end
end
+ def self.file?(value)
+ value.respond_to?(:content_type) && value.respond_to?(:original_filename)
+ end
+
def length
@part.length
end
21 test/test_parts.rb
View
@@ -22,6 +22,27 @@ def assert_part_length(part)
end
end
+class PartTest < Test::Unit::TestCase
+ def setup
+ @string_with_content_type = Class.new(String) do
+ def content_type; 'application/data'; end
+ end
+ end
+
+ def test_file_with_upload_io
+ assert Parts::Part.file?(UploadIO.new(__FILE__, "text/plain"))
+ end
+
+ def test_file_with_modified_string
+ assert !Parts::Part.file?(@string_with_content_type.new("Hello"))
+ end
+
+ def test_new_with_modified_string
+ assert_kind_of Parts::ParamPart,
+ Parts::Part.new("boundary", "multibyte", @string_with_content_type.new("Hello"))
+ end
+end
+
class FilePartTest < Test::Unit::TestCase
include AssertPartLength
Something went wrong with that request. Please try again.