Skip to content

Commit

Permalink
Use files.stripe.com only for the file create endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Sep 20, 2018
1 parent f3dd3ab commit 0908d5b
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 58 deletions.
2 changes: 1 addition & 1 deletion lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions lib/stripe/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
63 changes: 40 additions & 23 deletions test/stripe/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 40 additions & 23 deletions test/stripe/file_upload_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 0908d5b

Please sign in to comment.