Compare commits

...

2 Commits

3 changed files with 42 additions and 9 deletions

View File

@ -120,16 +120,35 @@ module Stripe
}
end
class Preview
PREVIEW_API_VERSION = "20230509T165653"
def self._get_default_opts(opts)
{ stripe_version: PREVIEW_API_VERSION, encoding: :json }.merge(opts)
end
def self.get(url, opts = {})
Stripe.raw_request(:get, url, {}, _get_default_opts(opts))
end
def self.post(url, params = {}, opts = {})
Stripe.raw_request(:post, url, params, _get_default_opts(opts))
end
def self.delete(url, opts = {})
Stripe.raw_request(:delete, url, {}, _get_default_opts(opts))
end
end
class RawRequest
include Stripe::APIOperations::Request
def initialize()
def initialize
@opts = {}
end
def execute(method, url, params = {}, opts = {})
resp, _ = execute_resource_request(method, url, params, opts)
resp, = execute_resource_request(method, url, params, opts)
resp
end

View File

@ -459,6 +459,9 @@ module Stripe
headers = request_headers(api_key, method)
.update(Util.normalize_headers(headers))
headers.delete("Content-Type") if encoding == :json && body_params.nil?
url = api_url(path, api_base)
# Merge given query parameters with any already encoded in the path.
@ -885,7 +888,7 @@ module Stripe
headers = {
"User-Agent" => user_agent,
"Authorization" => "Bearer #{api_key}",
"Content-Type" => "application/x-www-form-urlencoded",
"Content-Type" => "application/x-www-form-urlencoded", # TODO: (major) don't set if method is not post
}
if config.enable_telemetry? && !@last_request_metrics.nil?

View File

@ -146,7 +146,6 @@ class StripeTest < Test::Unit::TestCase
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 resp.http_body, expected_body
@ -163,7 +162,6 @@ class StripeTest < Test::Unit::TestCase
assert_equal resp.http_body, expected_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")
@ -178,16 +176,29 @@ class StripeTest < Test::Unit::TestCase
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" })
.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" }, { encoding: :json, "Stripe-Account": "bar" })
assert_equal expected_body, resp.http_body
end
should "send get request with json body" do
expected_body = "{\"id\": \"acc_123\"}"
req = nil
stub_request(:get, "#{Stripe.api_base}/v1/accounts/acc_123")
.with { |request| req = request; true }
.to_return(body: expected_body)
resp = Stripe.raw_request(:get, "/v1/accounts/acc_123", {}, { encoding: :json, "Stripe-Account": "bar" })
assert_not_equal req.headers["Content-Type"], "application/x-www-form-urlencoded"
assert_equal resp.http_body, expected_body
end
end
context "deserialize" do
should "deserializes string into known object" do
expected_body = "{\"id\": \"acc_123\", \"object\": \"account\"}"
@ -208,7 +219,7 @@ class StripeTest < Test::Unit::TestCase
end
should "deserializes hash into known object" do
expected_body = {"id" => "acc_123", "object" => "account"}
expected_body = { "id" => "acc_123", "object" => "account" }
obj = Stripe.deserialize(expected_body)
@ -217,7 +228,7 @@ class StripeTest < Test::Unit::TestCase
end
should "deserializes hash into unknown object" do
expected_body = {"id" => "acc_123", "object" => "unknown"}
expected_body = { "id" => "acc_123", "object" => "unknown" }
obj = Stripe.deserialize(expected_body)