mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-08-14 00:02:11 -04:00
Compare commits
5 Commits
3afb586f30
...
b063187c61
Author | SHA1 | Date | |
---|---|---|---|
|
b063187c61 | ||
|
af07fda46d | ||
|
7f0315e4d6 | ||
|
5ae2124628 | ||
|
adc0ccf8e7 |
@ -1 +1 @@
|
|||||||
v305
|
v309
|
@ -122,7 +122,7 @@ module Stripe
|
|||||||
|
|
||||||
class Preview
|
class Preview
|
||||||
def self._get_default_opts(opts)
|
def self._get_default_opts(opts)
|
||||||
{ stripe_version: ApiVersion::PREVIEW, api_mode: :preview }.merge(opts)
|
{ api_mode: :preview }.merge(opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get(url, opts = {})
|
def self.get(url, opts = {})
|
||||||
|
@ -462,8 +462,6 @@ module Stripe
|
|||||||
headers = request_headers(api_key, method, api_mode)
|
headers = request_headers(api_key, method, api_mode)
|
||||||
.update(Util.normalize_headers(headers))
|
.update(Util.normalize_headers(headers))
|
||||||
|
|
||||||
headers.delete("Content-Type") if api_mode == :preview && body_params.nil?
|
|
||||||
|
|
||||||
url = api_url(path, api_base)
|
url = api_url(path, api_base)
|
||||||
|
|
||||||
# Merge given query parameters with any already encoded in the path.
|
# Merge given query parameters with any already encoded in the path.
|
||||||
@ -890,10 +888,13 @@ module Stripe
|
|||||||
headers = {
|
headers = {
|
||||||
"User-Agent" => user_agent,
|
"User-Agent" => user_agent,
|
||||||
"Authorization" => "Bearer #{api_key}",
|
"Authorization" => "Bearer #{api_key}",
|
||||||
# TODO: (major) don't set Content-Type if method is not post
|
|
||||||
"Content-Type" => "application/x-www-form-urlencoded",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if api_mode != :preview
|
||||||
|
# TODO: (major) don't set Content-Type if method is not post
|
||||||
|
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||||
|
end
|
||||||
|
|
||||||
if config.enable_telemetry? && !@last_request_metrics.nil?
|
if config.enable_telemetry? && !@last_request_metrics.nil?
|
||||||
headers["X-Stripe-Client-Telemetry"] = JSON.generate(
|
headers["X-Stripe-Client-Telemetry"] = JSON.generate(
|
||||||
last_request_metrics: @last_request_metrics.payload
|
last_request_metrics: @last_request_metrics.payload
|
||||||
|
@ -1365,6 +1365,17 @@ module Stripe
|
|||||||
)
|
)
|
||||||
assert_requested :post, "#{Stripe.api_base}/v1/plans"
|
assert_requested :post, "#{Stripe.api_base}/v1/plans"
|
||||||
end
|
end
|
||||||
|
should "support requests with args: amount, currency, interval, product2" do
|
||||||
|
Stripe::Plan.create(
|
||||||
|
{
|
||||||
|
amount: 2000,
|
||||||
|
currency: "usd",
|
||||||
|
interval: "month",
|
||||||
|
product: { name: "My product" },
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assert_requested :post, "#{Stripe.api_base}/v1/plans"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
context "Plan.delete" do
|
context "Plan.delete" do
|
||||||
should "support requests with args: id" do
|
should "support requests with args: id" do
|
||||||
|
@ -14,7 +14,7 @@ class PreviewTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
resp = Stripe::Preview.get("/v2/accounts/acc_123")
|
resp = Stripe::Preview.get("/v2/accounts/acc_123")
|
||||||
|
|
||||||
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
|
assert_equal nil, req.headers["Content-Type"]
|
||||||
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
|
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
|
||||||
assert_equal expected_body, resp.http_body
|
assert_equal expected_body, resp.http_body
|
||||||
end
|
end
|
||||||
@ -45,7 +45,7 @@ class PreviewTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
resp = Stripe::Preview.delete("/v2/accounts/acc_123")
|
resp = Stripe::Preview.delete("/v2/accounts/acc_123")
|
||||||
|
|
||||||
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
|
assert_equal nil, req.headers["Content-Type"]
|
||||||
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
|
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
|
||||||
assert_equal expected_body, resp.http_body
|
assert_equal expected_body, resp.http_body
|
||||||
end
|
end
|
||||||
@ -61,7 +61,7 @@ class PreviewTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
resp = Stripe::Preview.post("/v2/accounts", {}, { stripe_version: stripe_version_override })
|
resp = Stripe::Preview.post("/v2/accounts", {}, { stripe_version: stripe_version_override })
|
||||||
|
|
||||||
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
|
assert_equal "application/json", req.headers["Content-Type"]
|
||||||
assert_equal stripe_version_override, req.headers["Stripe-Version"]
|
assert_equal stripe_version_override, req.headers["Stripe-Version"]
|
||||||
assert_equal expected_body, resp.http_body
|
assert_equal expected_body, resp.http_body
|
||||||
end
|
end
|
||||||
@ -77,7 +77,7 @@ class PreviewTest < Test::Unit::TestCase
|
|||||||
|
|
||||||
Stripe::Preview.post("/v2/accounts", {}, { stripe_context: stripe_context })
|
Stripe::Preview.post("/v2/accounts", {}, { stripe_context: stripe_context })
|
||||||
|
|
||||||
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
|
assert_equal "application/json", req.headers["Content-Type"]
|
||||||
assert_equal stripe_context, req.headers["Stripe-Context"]
|
assert_equal stripe_context, req.headers["Stripe-Context"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
112
test/stripe/raw_request_test.rb
Normal file
112
test/stripe/raw_request_test.rb
Normal file
@ -0,0 +1,112 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require ::File.expand_path("../test_helper", __dir__)
|
||||||
|
|
||||||
|
class RawRequestTest < Test::Unit::TestCase
|
||||||
|
context "raw_request" do
|
||||||
|
should "send get request and return a response" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
req = nil
|
||||||
|
|
||||||
|
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with { |request| req = request }
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
resp = Stripe.raw_request(:get, "/v1/accounts/acc_123")
|
||||||
|
|
||||||
|
assert_equal expected_body, resp.http_body
|
||||||
|
assert_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
|
||||||
|
assert_equal Stripe::ApiVersion::CURRENT, req.headers["Stripe-Version"]
|
||||||
|
end
|
||||||
|
|
||||||
|
should "send post request with body and return a response" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
req = nil
|
||||||
|
|
||||||
|
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with(body: "p1=1&p2=string")
|
||||||
|
.with { |request| req = request }
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" })
|
||||||
|
|
||||||
|
assert_equal expected_body, resp.http_body
|
||||||
|
assert_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
|
||||||
|
assert_equal Stripe::ApiVersion::CURRENT, req.headers["Stripe-Version"]
|
||||||
|
end
|
||||||
|
|
||||||
|
should "send post request with json body and return a response" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
req = nil
|
||||||
|
|
||||||
|
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with(body: "{\"p1\":1,\"p2\":\"string\"}")
|
||||||
|
.with { |request| req = request }
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" }, { api_mode: :preview })
|
||||||
|
|
||||||
|
assert_equal expected_body, resp.http_body
|
||||||
|
assert_equal "application/json", req.headers["Content-Type"]
|
||||||
|
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
|
||||||
|
end
|
||||||
|
|
||||||
|
should "send post request with json body and headers and return a response" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
req = nil
|
||||||
|
|
||||||
|
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with(body: "{\"p1\":1,\"p2\":\"string\"}")
|
||||||
|
.with { |request| req = request }
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" }, { api_mode: :preview, "Stripe-Context": "bar" })
|
||||||
|
|
||||||
|
assert_equal expected_body, resp.http_body
|
||||||
|
assert_equal "application/json", req.headers["Content-Type"]
|
||||||
|
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
|
||||||
|
assert_equal "bar", req.headers["Stripe-Context"]
|
||||||
|
end
|
||||||
|
|
||||||
|
should "send get request with json body and headers and return a response" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
req = nil
|
||||||
|
|
||||||
|
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with { |request| req = request }
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
resp = Stripe.raw_request(:get, "/v1/accounts/acc_123", {}, { api_mode: :preview, "Stripe-Account": "bar" })
|
||||||
|
|
||||||
|
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
|
||||||
|
assert_equal expected_body, resp.http_body
|
||||||
|
end
|
||||||
|
|
||||||
|
should "set default preview version when api_mode is preview and stripe_version not specified" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
req = nil
|
||||||
|
|
||||||
|
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with { |request| req = request }
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
Stripe.raw_request(:get, "/v1/accounts/acc_123", {}, { api_mode: :preview })
|
||||||
|
|
||||||
|
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
|
||||||
|
end
|
||||||
|
|
||||||
|
should "allow overriding stripe version when api_mode is preview" do
|
||||||
|
expected_body = "{\"id\": \"acc_123\"}"
|
||||||
|
req = nil
|
||||||
|
|
||||||
|
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
||||||
|
.with { |request| req = request }
|
||||||
|
.to_return(body: expected_body)
|
||||||
|
|
||||||
|
stripe_version_override = "2023-05-15.preview"
|
||||||
|
Stripe.raw_request(:get, "/v1/accounts/acc_123", {}, { api_mode: :preview, stripe_version: stripe_version_override })
|
||||||
|
|
||||||
|
assert_equal stripe_version_override, req.headers["Stripe-Version"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -94,14 +94,9 @@ class StripeTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
|
|
||||||
should "allow logger to be configured" do
|
should "allow logger to be configured" do
|
||||||
old_logger = Stripe.logger
|
|
||||||
begin
|
|
||||||
logger = Object.new
|
logger = Object.new
|
||||||
Stripe.logger = logger
|
Stripe.logger = logger
|
||||||
assert_equal logger, Stripe.logger
|
assert_equal logger, Stripe.logger
|
||||||
ensure
|
|
||||||
Stripe.logger = old_logger
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
should "allow proxy to be configured" do
|
should "allow proxy to be configured" do
|
||||||
@ -140,92 +135,6 @@ class StripeTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "raw_request" do
|
|
||||||
should "send get request and return a response" do
|
|
||||||
expected_body = "{\"id\": \"acc_123\"}"
|
|
||||||
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
|
||||||
.to_return(body: expected_body)
|
|
||||||
|
|
||||||
resp = Stripe.raw_request(:get, "/v1/accounts/acc_123")
|
|
||||||
|
|
||||||
assert_equal expected_body, resp.http_body
|
|
||||||
end
|
|
||||||
|
|
||||||
should "send post request with body and return a response" do
|
|
||||||
expected_body = "{\"id\": \"acc_123\"}"
|
|
||||||
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
|
||||||
.with(body: "p1=1&p2=string")
|
|
||||||
.to_return(body: expected_body)
|
|
||||||
|
|
||||||
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" })
|
|
||||||
|
|
||||||
assert_equal expected_body, resp.http_body
|
|
||||||
end
|
|
||||||
|
|
||||||
should "send post request with json body and return a response" do
|
|
||||||
expected_body = "{\"id\": \"acc_123\"}"
|
|
||||||
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
|
||||||
.with(body: "{\"p1\":1,\"p2\":\"string\"}")
|
|
||||||
.to_return(body: expected_body)
|
|
||||||
|
|
||||||
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" }, { api_mode: :preview })
|
|
||||||
|
|
||||||
assert_equal expected_body, resp.http_body
|
|
||||||
end
|
|
||||||
|
|
||||||
should "send post request with json body and headers and return a response" do
|
|
||||||
expected_body = "{\"id\": \"acc_123\"}"
|
|
||||||
stub_request(:post, "#{Stripe.api_base}/v1/accounts/acc_123")
|
|
||||||
.with(body: "{\"p1\":1,\"p2\":\"string\"}", headers: { "Stripe-Account" => "bar", "Content-Type" => "application/json" })
|
|
||||||
.to_return(body: expected_body)
|
|
||||||
|
|
||||||
resp = Stripe.raw_request(:post, "/v1/accounts/acc_123", { p1: 1, p2: "string" }, { api_mode: :preview, "Stripe-Account": "bar" })
|
|
||||||
|
|
||||||
assert_equal expected_body, resp.http_body
|
|
||||||
end
|
|
||||||
|
|
||||||
should "send get request with json body and headers and return a response" do
|
|
||||||
expected_body = "{\"id\": \"acc_123\"}"
|
|
||||||
req = nil
|
|
||||||
|
|
||||||
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
|
||||||
.with { |request| req = request }
|
|
||||||
.to_return(body: expected_body)
|
|
||||||
|
|
||||||
resp = Stripe.raw_request(:get, "/v1/accounts/acc_123", {}, { api_mode: :preview, "Stripe-Account": "bar" })
|
|
||||||
|
|
||||||
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
|
|
||||||
assert_equal expected_body, resp.http_body
|
|
||||||
end
|
|
||||||
|
|
||||||
should "set default preview version when api_mode is preview and stripe_version not specified" do
|
|
||||||
expected_body = "{\"id\": \"acc_123\"}"
|
|
||||||
req = nil
|
|
||||||
|
|
||||||
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
|
||||||
.with { |request| req = request }
|
|
||||||
.to_return(body: expected_body)
|
|
||||||
|
|
||||||
Stripe.raw_request(:get, "/v1/accounts/acc_123", {}, { api_mode: :preview })
|
|
||||||
|
|
||||||
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
|
|
||||||
end
|
|
||||||
|
|
||||||
should "allow overriding stripe version when api_mode is preview" do
|
|
||||||
expected_body = "{\"id\": \"acc_123\"}"
|
|
||||||
req = nil
|
|
||||||
|
|
||||||
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
|
|
||||||
.with { |request| req = request }
|
|
||||||
.to_return(body: expected_body)
|
|
||||||
|
|
||||||
stripe_version_override = "2023-05-15.preview"
|
|
||||||
Stripe.raw_request(:get, "/v1/accounts/acc_123", {}, { api_mode: :preview, stripe_version: stripe_version_override })
|
|
||||||
|
|
||||||
assert_equal stripe_version_override, req.headers["Stripe-Version"]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "deserialize" do
|
context "deserialize" do
|
||||||
should "deserializes string into known object" do
|
should "deserializes string into known object" do
|
||||||
expected_body = "{\"id\": \"acc_123\", \"object\": \"account\"}"
|
expected_body = "{\"id\": \"acc_123\", \"object\": \"account\"}"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user