From 6cb8102c4f11f2f40de9c3ac90a5cd67befa69cc Mon Sep 17 00:00:00 2001 From: Annie Li Date: Tue, 9 May 2023 16:08:22 -0700 Subject: [PATCH] Fix bug where get requests still had form content-type header even with JSON encoding specified --- lib/stripe/stripe_client.rb | 5 ++++- test/stripe_test.rb | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/stripe/stripe_client.rb b/lib/stripe/stripe_client.rb index fd2cdcc4e..335653610 100644 --- a/lib/stripe/stripe_client.rb +++ b/lib/stripe/stripe_client.rb @@ -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. @@ -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? diff --git a/test/stripe_test.rb b/test/stripe_test.rb index d0323dba1..c26d329be 100644 --- a/test/stripe_test.rb +++ b/test/stripe_test.rb @@ -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)