From 0908d5b62f8f2e5b554f7d3f8defbfd8da008dc0 Mon Sep 17 00:00:00 2001 From: Olivier Bellone Date: Thu, 20 Sep 2018 22:45:24 +0200 Subject: [PATCH] Use files.stripe.com only for the file create endpoint --- lib/stripe.rb | 2 +- lib/stripe/file.rb | 8 +---- test/stripe/file_test.rb | 63 +++++++++++++++++++++------------ test/stripe/file_upload_test.rb | 63 +++++++++++++++++++++------------ test/test_helper.rb | 4 --- 5 files changed, 82 insertions(+), 58 deletions(-) diff --git a/lib/stripe.rb b/lib/stripe.rb index c6d5a2993..73a186e79 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -103,7 +103,7 @@ module Stripe @api_base = "https://api.stripe.com" @connect_base = "https://connect.stripe.com" - @uploads_base = "https://uploads.stripe.com" + @uploads_base = "https://files.stripe.com" @log_level = nil @logger = nil diff --git a/lib/stripe/file.rb b/lib/stripe/file.rb index b4cd50350..eef999fac 100644 --- a/lib/stripe/file.rb +++ b/lib/stripe/file.rb @@ -16,13 +16,6 @@ def self.resource_url "/v1/files" end - def self.request(method, url, params = {}, opts = {}) - opts = { - api_base: Stripe.uploads_base, - }.merge(Util.normalize_opts(opts)) - super - 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`-like object with an @@ -32,6 +25,7 @@ def self.create(params = {}, opts = {}) end opts = { + api_base: Stripe.uploads_base, content_type: "multipart/form-data", }.merge(Util.normalize_opts(opts)) super diff --git a/test/stripe/file_test.rb b/test/stripe/file_test.rb index 1a5393028..858e1ae4d 100644 --- a/test/stripe/file_test.rb +++ b/test/stripe/file_test.rb @@ -6,41 +6,58 @@ module Stripe class FileTest < Test::Unit::TestCase should "be listable" do files = Stripe::File.list + assert_requested :get, "#{Stripe.api_base}/v1/files" assert files.data.is_a?(Array) assert files.data[0].is_a?(Stripe::File) end should "be retrievable" do file = Stripe::File.retrieve("file_123") + assert_requested :get, "#{Stripe.api_base}/v1/files/file_123" assert file.is_a?(Stripe::File) end - should "be creatable with a File" do - file = Stripe::File.create( - purpose: "dispute_evidence", - file: ::File.new(__FILE__) - ) - assert file.is_a?(Stripe::File) - end + context ".create" do + setup do + # We don't point to the same host for the API and uploads in + # production, but `stripe-mock` supports both APIs. + Stripe.uploads_base = Stripe.api_base - should "be creatable with a Tempfile" do - tempfile = Tempfile.new("foo") - tempfile.write("Hello world") - tempfile.rewind + # Set `api_base` to `nil` to ensure that these requests are _not_ sent + # to the default API hostname. + Stripe.api_base = nil + end - file = Stripe::File.create( - purpose: "dispute_evidence", - file: tempfile - ) - assert file.is_a?(Stripe::File) - end + should "be creatable with a File" do + file = Stripe::File.create( + purpose: "dispute_evidence", + file: ::File.new(__FILE__) + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::File) + end - should "be creatable with Faraday::UploadIO" do - file = Stripe::File.create( - purpose: "dispute_evidence", - file: Faraday::UploadIO.new(::File.new(__FILE__), nil) - ) - assert file.is_a?(Stripe::File) + should "be creatable with a Tempfile" do + tempfile = Tempfile.new("foo") + tempfile.write("Hello world") + tempfile.rewind + + file = Stripe::File.create( + purpose: "dispute_evidence", + file: tempfile + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::File) + end + + should "be creatable with Faraday::UploadIO" do + file = Stripe::File.create( + purpose: "dispute_evidence", + file: Faraday::UploadIO.new(::File.new(__FILE__), nil) + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::File) + end end should "be deserializable when `object=file`" do diff --git a/test/stripe/file_upload_test.rb b/test/stripe/file_upload_test.rb index 42556b18e..5786309d2 100644 --- a/test/stripe/file_upload_test.rb +++ b/test/stripe/file_upload_test.rb @@ -6,41 +6,58 @@ module Stripe class FileUploadTest < Test::Unit::TestCase should "be listable" do files = Stripe::FileUpload.list + assert_requested :get, "#{Stripe.api_base}/v1/files" assert files.data.is_a?(Array) assert files.data[0].is_a?(Stripe::FileUpload) end should "be retrievable" do file = Stripe::FileUpload.retrieve("file_123") + assert_requested :get, "#{Stripe.api_base}/v1/files/file_123" assert file.is_a?(Stripe::FileUpload) end - should "be creatable with a File" do - file = Stripe::FileUpload.create( - purpose: "dispute_evidence", - file: ::File.new(__FILE__) - ) - assert file.is_a?(Stripe::FileUpload) - end + context ".create" do + setup do + # We don't point to the same host for the API and uploads in + # production, but `stripe-mock` supports both APIs. + Stripe.uploads_base = Stripe.api_base - should "be creatable with a Tempfile" do - tempfile = Tempfile.new("foo") - tempfile.write("Hello world") - tempfile.rewind + # Set `api_base` to `nil` to ensure that these requests are _not_ sent + # to the default API hostname. + Stripe.api_base = nil + end - file = Stripe::FileUpload.create( - purpose: "dispute_evidence", - file: tempfile - ) - assert file.is_a?(Stripe::FileUpload) - end + should "be creatable with a File" do + file = Stripe::FileUpload.create( + purpose: "dispute_evidence", + file: ::File.new(__FILE__) + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::FileUpload) + end - should "be creatable with Faraday::UploadIO" do - file = Stripe::FileUpload.create( - purpose: "dispute_evidence", - file: Faraday::UploadIO.new(::File.new(__FILE__), nil) - ) - assert file.is_a?(Stripe::FileUpload) + should "be creatable with a Tempfile" do + tempfile = Tempfile.new("foo") + tempfile.write("Hello world") + tempfile.rewind + + file = Stripe::FileUpload.create( + purpose: "dispute_evidence", + file: tempfile + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::FileUpload) + end + + should "be creatable with Faraday::UploadIO" do + file = Stripe::FileUpload.create( + purpose: "dispute_evidence", + file: Faraday::UploadIO.new(::File.new(__FILE__), nil) + ) + assert_requested :post, "#{Stripe.uploads_base}/v1/files" + assert file.is_a?(Stripe::FileUpload) + end end should "be deserializable when `object=file`" do diff --git a/test/test_helper.rb b/test/test_helper.rb index 71bbe09f3..086ad9bd5 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -50,10 +50,6 @@ class TestCase Stripe.api_key = "sk_test_123" Stripe.api_base = "http://localhost:#{MOCK_PORT}" - # We don't point to the same host for the API and uploads in - # production, but `stripe-mock` supports both APIs. - Stripe.uploads_base = Stripe.api_base - stub_connect end