module GdsApi::TestHelpers::Worldwide
def worldwide_api_has_locations(location_slugs)
This also sets up the individual endpoints for each slug
The stubs are setup to paginate in chunks of 20
Sets up the index endpoints for the given country slugs
def worldwide_api_has_locations(location_slugs) location_slugs.each { |s| worldwide_api_has_location(s) } pages = [] location_slugs.each_slice(20) do |slugs| pages << slugs.map { |s| world_location_details_for_slug(s) } end pages.each_with_index do |page, i| page_details = plural_response_base.merge("results" => page, "total" => location_slugs.size, "pages" => pages.size, "current_page" => i + 1, "page_size" => 20, "start_index" => i * 20 + 1) links = { self: "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 1}" } links[:next] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i + 2}" if pages[i + 1] links[:previous] = "#{WORLDWIDE_API_ENDPOINT}/api/world-locations?page=#{i}" unless i == 0 page_details["_response_info"]["links"] = [] link_headers = [] links.each do |rel, href| page_details["_response_info"]["links"] << { "rel" => rel, "href" => href } link_headers << "<#{href}>; rel=\"#{rel}\"" end stub_request(:get, links[:self]). to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") }) if i == 0 # First page exists at URL with and without page param stub_request(:get, links[:self].sub(/\?page=1/, '')). to_return(status: 200, body: page_details.to_json, headers: { "Link" => link_headers.join(", ") }) end end end