diff --git a/lib/stripe/stripe_service.rb b/lib/stripe/stripe_service.rb index 17b2e045c..260eb7b76 100644 --- a/lib/stripe/stripe_service.rb +++ b/lib/stripe/stripe_service.rb @@ -10,15 +10,6 @@ def initialize(requestor) def request(method:, path:, base_address:, params: {}, opts: {}) params = params.to_h if params.is_a?(RequestParams) - if method == :post && - path.split("?", 2).first.start_with?("/v2/") && - path.split("?", 2).first.end_with?("/search") && - (params.key?(:limit) || params.key?("limit")) - params = params.dup - limit = params.delete(:limit) || params.delete("limit") - separator = path.include?("?") ? "&" : "?" - path = "#{path}#{separator}limit=#{URI.encode_www_form_component(limit)}" - end @requestor.execute_request( method, path, @@ -29,6 +20,19 @@ def request(method:, path:, base_address:, params: {}, opts: {}) ) end + # V2 search uses POST but sends limit in the URL, not the JSON body. Rebuild + # the initial query from params; pagination uses the ordinary request path. + def request_v2_search(method:, path:, base_address:, params: {}, opts: {}) + params = params.to_h if params.is_a?(RequestParams) + params = params.dup + limit = params.delete(:limit) + string_limit = params.delete("limit") + limit ||= string_limit + path = path.split("?", 2).first + path = "#{path}?limit=#{URI.encode_www_form_component(limit)}" unless limit.nil? + request(method: method, path: path, base_address: base_address, params: params, opts: opts) + end + def request_stream(method:, path:, base_address:, params: {}, opts: {}, &read_body_chunk_block) @requestor.execute_request_stream( method, diff --git a/test/stripe/stripe_service_test.rb b/test/stripe/stripe_service_test.rb index 888e6766f..3cc41c184 100644 --- a/test/stripe/stripe_service_test.rb +++ b/test/stripe/stripe_service_test.rb @@ -84,12 +84,48 @@ class TestV2SearchParams < RequestParams has_entries(params: { query: "widgets" })) .returns([{ object: "v2.search_result", data: [] }, nil]) - service.request( + service.request_v2_search( method: :post, path: "/v2/widgets/search", base_address: :api, params: params ) + assert_equal 2, params.limit + assert_equal "widgets", params.query + end + end + + context "v2 search dispatch" do + [ + ["/v2/widgets/search", { limit: 2, query: "widgets" }, "/v2/widgets/search?limit=2", { query: "widgets" }], + ["/v2/widgets/search", { "limit" => 2, "query" => "widgets" }, "/v2/widgets/search?limit=2", { "query" => "widgets" }], + ["/v2/widgets/search?page=2&limit=2", { limit: 9, query: "widgets" }, "/v2/widgets/search?limit=9", { query: "widgets" }], + ["/v2/widgets/search?page=2", { limit: 9, query: "widgets" }, "/v2/widgets/search?limit=9", { query: "widgets" }], + ["/v2/widgets/search?limit=2&unused=value", { query: "widgets" }, "/v2/widgets/search", { query: "widgets" }], + ["/v2/widgets/search", { query: "widgets" }, "/v2/widgets/search", { query: "widgets" }], + ].each_with_index do |(path, params, expected_path, body), index| + should "prepare and delegate search once for case #{index}" do + service = StripeService.new(nil) + original = params.dup + opts = { stripe_account: "acct_123" } + service.expects(:request).once.with(method: :post, path: expected_path, + base_address: :meter_events, params: body, opts: opts) + service.request_v2_search(method: :post, path: path, params: params, + base_address: :meter_events, opts: opts) + assert_equal original, params + end + end + + should "keep custom search limit in the body and preserve options and usage" do + requestor = APIRequestor.new("sk_test_123") + service = StripeService.new(requestor) + params = { limit: 2, query: "widgets" }.freeze + requestor.expects(:execute_request).once + .with(:post, "/v2/data/operations/search", :api, + has_entries(params: params, opts: has_entries(stripe_account: "acct_123"), + usage: ["stripe_client"])) + service.request(method: :post, path: "/v2/data/operations/search", base_address: :api, + params: params, opts: { stripe_account: "acct_123" }) end end