Skip to content

Commit

Permalink
Fix bug where get requests still had form content-type header even wi…
Browse files Browse the repository at this point in the history
…th JSON encoding specified
  • Loading branch information
anniel-stripe committed May 9, 2023
1 parent a0b5b50 commit 6cb8102
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,9 @@ def self.maybe_gc_connection_managers

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.
Expand Down Expand Up @@ -885,7 +888,7 @@ def self.maybe_gc_connection_managers
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?
Expand Down
23 changes: 17 additions & 6 deletions test/stripe_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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\"}"
Expand All @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 6cb8102

Please sign in to comment.