Skip to content

Instantly share code, notes, and snippets.

@AnDyro751
Created December 5, 2025 20:22
Show Gist options
  • Select an option

  • Save AnDyro751/d62007f0e834741b085ade3e348b272a to your computer and use it in GitHub Desktop.

Select an option

Save AnDyro751/d62007f0e834741b085ade3e348b272a to your computer and use it in GitHub Desktop.
require "test_helper"
require "json"
require Rails.root.join("lib/easy_broker_client").to_s
class EasyBrokerClientTest < Minitest::Test
BASE_URL = EasyBrokerClient::API_BASE_URL
def test_fetch_all_properties_combines_pages_and_yields_payloads
responses = [
{ page: 1, next_page: 2, public_ids: %w[EB-1] },
{ page: 2, next_page: nil, public_ids: %w[EB-2 EB-3] }
]
http_client = FakeHttpClient.new(responses.map { |r| fake_payload(**r) })
client = EasyBrokerClient.new(
api_key: "token",
http_client: http_client,
page_size: 2
)
yielded_pages = []
properties = client.fetch_all_properties do |payload|
yielded_pages << payload.dig("pagination", "page")
end
assert_equal %w[EB-1 EB-2 EB-3], properties.map { |p| p["public_id"] }
assert_equal [1, 2], yielded_pages
assert_equal [
"#{BASE_URL}/properties?page=1&limit=2",
"#{BASE_URL}/properties?page=2&limit=2"
], http_client.request_uris
headers = http_client.request_headers.first
assert_equal "token", headers["X-Authorization"]
assert_equal "application/json", headers["accept"]
end
def test_fetch_all_properties_handles_full_url_next_page
responses = [
{ page: 1, next_page: "#{BASE_URL}/properties?page=2&limit=2", public_ids: %w[EB-1] },
{ page: 2, next_page: nil, public_ids: %w[EB-2 EB-3] }
]
http_client = FakeHttpClient.new(responses.map { |r| fake_payload(**r) })
client = EasyBrokerClient.new(
api_key: "token",
http_client: http_client,
page_size: 2
)
properties = client.fetch_all_properties
assert_equal %w[EB-1 EB-2 EB-3], properties.map { |p| p["public_id"] }
assert_equal [
"#{BASE_URL}/properties?page=1&limit=2",
"#{BASE_URL}/properties?page=2&limit=2"
], http_client.request_uris
end
private
def fake_payload(page:, next_page:, public_ids:)
{
"pagination" => { "limit" => 2, "page" => page, "total" => 99, "next_page" => next_page },
"content" => public_ids.map { |id| { "public_id" => id } }
}
end
class FakeHttpClient
attr_reader :request_uris, :request_headers
def initialize(responses)
@responses = responses
@request_uris = []
@request_headers = []
end
def get_json(uri, headers = {})
@request_uris << uri.to_s
@request_headers << headers
raise "No more fake responses" if @responses.empty?
@responses.shift
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment