Skip to content

Commit

Permalink
Officially support Pathname in FormData::File.new
Browse files Browse the repository at this point in the history
Previously Pathname was implicitly supported, though extracting filename
wasn't working. With the recent refactoring this stopped working, so we
make the Pathname support explicit.
  • Loading branch information
janko authored and ixti committed May 8, 2017
1 parent 8fd6910 commit bbcdbd8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/http/form_data/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,26 @@ class File < Part
alias mime_type content_type

# @see DEFAULT_MIME
# @param [String, IO] file_or_io Filename or IO instance.
# @param [String, Pathname, IO] file_or_io Filename or IO instance.
# @param [#to_h] opts
# @option opts [#to_s] :content_type (DEFAULT_MIME)
# Value of Content-Type header
# @option opts [#to_s] :filename
# When `file` is a String, defaults to basename of `file`.
# When `file` is a File, defaults to basename of `file`.
# When `file` is a StringIO, defaults to `"stream-{object_id}"`
def initialize(file_or_io, opts = {})
def initialize(path_or_io, opts = {})
opts = FormData.ensure_hash(opts)

if opts.key? :mime_type
warn "[DEPRECATED] :mime_type option deprecated, use :content_type"
opts[:content_type] = opts[:mime_type]
end

if file_or_io.is_a?(String)
@io = ::File.open(file_or_io, binmode: true)
if path_or_io.is_a?(String) || defined?(Pathname) && path_or_io.is_a?(Pathname)
@io = ::File.open(path_or_io, binmode: true)
else
@io = file_or_io
@io = path_or_io
end

@content_type = opts.fetch(:content_type, DEFAULT_MIME).to_s
Expand Down
21 changes: 21 additions & 0 deletions spec/lib/http/form_data/file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
it { is_expected.to eq fixture("the-http-gem.info").size }
end

context "when file given as a Pathname" do
let(:file) { fixture("the-http-gem.info") }
it { is_expected.to eq fixture("the-http-gem.info").size }
end

context "when file given as File" do
let(:file) { fixture("the-http-gem.info").open }
after { file.close }
Expand All @@ -32,6 +37,11 @@
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
end

context "when file given as a Pathname" do
let(:file) { fixture("the-http-gem.info") }
it { is_expected.to eq fixture("the-http-gem.info").read(:mode => "rb") }
end

context "when file given as File" do
let(:file) { fixture("the-http-gem.info").open("rb") }
after { file.close }
Expand All @@ -58,6 +68,17 @@
end
end

context "when file given as a Pathname" do
let(:file) { fixture("the-http-gem.info") }

it { is_expected.to eq ::File.basename file }

context "and filename given with options" do
let(:opts) { { :filename => "foobar.txt" } }
it { is_expected.to eq "foobar.txt" }
end
end

context "when file given as File" do
let(:file) { fixture("the-http-gem.info").open }
after { file.close }
Expand Down

0 comments on commit bbcdbd8

Please sign in to comment.