Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge query parameters coming from path with params argument #647

Merged
merged 1 commit into from
May 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@

# Offense count: 19
Metrics/AbcSize:
Max: 45
Max: 52

# Offense count: 27
# Configuration parameters: CountComments, ExcludedMethods.
Metrics/BlockLength:
Max: 469
Max: 496

# Offense count: 8
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 597
Max: 624

# Offense count: 11
Metrics/CyclomaticComplexity:
Expand All @@ -33,7 +33,7 @@ Metrics/LineLength:
# Offense count: 32
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 46
Max: 48

# Offense count: 1
# Configuration parameters: CountComments.
Expand Down
17 changes: 17 additions & 0 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,23 @@ def execute_request(method, path,
end
end

# This works around an edge case where we end up with both query
# parameters in `query_params` and query parameters that are appended
# onto the end of the given path. In this case, Faraday will silently
# discard the URL's parameters which may break a request.
#
# Here we decode any parameters that were added onto the end of a path
# and add them to `query_params` so that all parameters end up in one
# place and all of them are correctly included in the final request.
u = URI.parse(path)
unless u.query.nil?
query_params ||= {}
query_params = Hash[URI.decode_www_form(u.query)].merge(query_params)

# Reset the path minus any query parameters that were specified.
path = u.path
end

headers = request_headers(api_key, method)
.update(Util.normalize_headers(headers))

Expand Down
29 changes: 29 additions & 0 deletions test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,35 @@ class StripeClientTest < Test::Unit::TestCase
}
)
end

should "merge query parameters in URL and params" do
client = StripeClient.new
client.execute_request(:get, "/v1/invoices/upcoming?coupon=25OFF", params: {
customer: "cus_123",
})
assert_requested(
:get,
"#{Stripe.api_base}/v1/invoices/upcoming?",
query: {
coupon: "25OFF",
customer: "cus_123",
}
)
end

should "prefer query parameters in params when specified in URL as well" do
client = StripeClient.new
client.execute_request(:get, "/v1/invoices/upcoming?customer=cus_query", params: {
customer: "cus_param",
})
assert_requested(
:get,
"#{Stripe.api_base}/v1/invoices/upcoming?",
query: {
customer: "cus_param",
}
)
end
end
end

Expand Down