Skip to content

Commit

Permalink
Merge pull request #508 from stripe/brandur-fix-file-uploads
Browse files Browse the repository at this point in the history
Properly encode file uploads
  • Loading branch information
brandur-stripe authored Feb 22, 2017
2 parents ea90ddb + 77c8535 commit 083038e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
7 changes: 7 additions & 0 deletions lib/stripe/file_upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ def self.request(method, url, params={}, opts={})
end

def self.create(params={}, opts={})
# rest-client would accept a vanilla `File` for upload, but Faraday does
# not. Support the old API by wrapping a `File` with an `UploadIO` object
# if we're given one.
if params[:file] && params[:file].is_a?(File)
params[:file] = Faraday::UploadIO.new(params[:file], nil)
end

opts = {
:content_type => 'multipart/form-data',
}.merge(Util.normalize_opts(opts))
Expand Down
1 change: 1 addition & 0 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def self.default_conn
# object per thread.
Thread.current[:stripe_client_default_conn] ||= begin
conn = Faraday.new do |c|
c.use Faraday::Request::Multipart
c.use Faraday::Request::UrlEncoded
c.use Faraday::Response::RaiseError
c.adapter Faraday.default_adapter
Expand Down
21 changes: 20 additions & 1 deletion test/stripe/file_upload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,32 @@ class FileUploadTest < Test::Unit::TestCase

should "be creatable" do
stub_request(:post, "#{Stripe.uploads_base}/v1/files").
to_return(body: JSON.generate(FIXTURE))
with(:headers => {
"Content-Type" => %r|\A#{Faraday::Request::Multipart.mime_type}|
}) { |request|
request.body =~ /FileUploadTest/
}.to_return(body: JSON.generate(FIXTURE))

file = Stripe::FileUpload.create(
purpose: "dispute_evidence",
file: File.new(__FILE__),
)
assert file.kind_of?(Stripe::FileUpload)
end

should "be creatable with Faraday::UploadIO" do
stub_request(:post, "#{Stripe.uploads_base}/v1/files").
with(:headers => {
"Content-Type" => %r|\A#{Faraday::Request::Multipart.mime_type}|
}) { |request|
request.body =~ /FileUploadTest/
}.to_return(body: JSON.generate(FIXTURE))

file = Stripe::FileUpload.create(
purpose: "dispute_evidence",
file: Faraday::UploadIO.new(File.new(__FILE__), nil),
)
assert file.kind_of?(Stripe::FileUpload)
end
end
end

0 comments on commit 083038e

Please sign in to comment.