Compare commits

..

No commits in common. "b063187c61d3a68d2facabdb66ee36e2d44f91d4" and "3afb586f3000ef10641c75b3437047d537673a02" have entirely different histories.

7 changed files with 104 additions and 137 deletions

View File

@ -1 +1 @@
v309
v305

View File

@ -122,7 +122,7 @@ module Stripe
class Preview
def self._get_default_opts(opts)
{ api_mode: :preview }.merge(opts)
{ stripe_version: ApiVersion::PREVIEW, api_mode: :preview }.merge(opts)
end
def self.get(url, opts = {})

View File

@ -462,6 +462,8 @@ module Stripe
headers = request_headers(api_key, method, api_mode)
.update(Util.normalize_headers(headers))
headers.delete("Content-Type") if api_mode == :preview && body_params.nil?
url = api_url(path, api_base)
# Merge given query parameters with any already encoded in the path.
@ -888,12 +890,9 @@ module Stripe
headers = {
"User-Agent" => user_agent,
"Authorization" => "Bearer #{api_key}",
}
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
"Content-Type" => "application/x-www-form-urlencoded",
}
if config.enable_telemetry? && !@last_request_metrics.nil?
headers["X-Stripe-Client-Telemetry"] = JSON.generate(

View File

@ -1365,17 +1365,6 @@ module Stripe
)
assert_requested :post, "#{Stripe.api_base}/v1/plans"
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
context "Plan.delete" do
should "support requests with args: id" do

View File

@ -14,7 +14,7 @@ class PreviewTest < Test::Unit::TestCase
resp = Stripe::Preview.get("/v2/accounts/acc_123")
assert_equal nil, req.headers["Content-Type"]
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
assert_equal expected_body, resp.http_body
end
@ -45,7 +45,7 @@ class PreviewTest < Test::Unit::TestCase
resp = Stripe::Preview.delete("/v2/accounts/acc_123")
assert_equal nil, req.headers["Content-Type"]
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
assert_equal Stripe::ApiVersion::PREVIEW, req.headers["Stripe-Version"]
assert_equal expected_body, resp.http_body
end
@ -61,7 +61,7 @@ class PreviewTest < Test::Unit::TestCase
resp = Stripe::Preview.post("/v2/accounts", {}, { stripe_version: stripe_version_override })
assert_equal "application/json", req.headers["Content-Type"]
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
assert_equal stripe_version_override, req.headers["Stripe-Version"]
assert_equal expected_body, resp.http_body
end
@ -77,7 +77,7 @@ class PreviewTest < Test::Unit::TestCase
Stripe::Preview.post("/v2/accounts", {}, { stripe_context: stripe_context })
assert_equal "application/json", req.headers["Content-Type"]
assert_not_equal "application/x-www-form-urlencoded", req.headers["Content-Type"]
assert_equal stripe_context, req.headers["Stripe-Context"]
end
end

View File

@ -1,112 +0,0 @@
# 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

View File

@ -94,9 +94,14 @@ class StripeTest < Test::Unit::TestCase
end
should "allow logger to be configured" do
logger = Object.new
Stripe.logger = logger
assert_equal logger, Stripe.logger
old_logger = Stripe.logger
begin
logger = Object.new
Stripe.logger = logger
assert_equal logger, Stripe.logger
ensure
Stripe.logger = old_logger
end
end
should "allow proxy to be configured" do
@ -135,6 +140,92 @@ class StripeTest < Test::Unit::TestCase
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
should "deserializes string into known object" do
expected_body = "{\"id\": \"acc_123\", \"object\": \"account\"}"