module GdsApi
  module TestHelpers
    module ContentApi
      class ArtefactStub
        include WebMock::API
        # This is ugly, but the nicest way we found to get access to artefact_for_slug
        include GdsApi::TestHelpers::ContentApi

        attr_accessor :slug, :query_parameters, :response_body, :response_status

        def initialize(slug)
          @slug = slug
          @query_parameters = {}
          @response_body = artefact_for_slug(slug)
          @response_status = 200
        end
        
        def with_query_parameters(hash)
          @query_parameters = hash
          self
        end

        def with_response_body(response_body)
          @response_body = response_body
          self
        end

        def with_response_status(response_status)
          @response_status = response_status
          self
        end

        # Nothing is stubbed until this is called
        def stub
          stub_request(:get, url).to_return(status: @response_status, body: @response_body.to_json)
        end
        
        def url
          result = "#{CONTENT_API_ENDPOINT}/#{slug}.json"
          if @query_parameters.any?
            params = @query_parameters.map { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join("&")
            result += "?#{params}"
          end
          result
        end
      end
    end
  end
end
module GdsApi
  module TestHelpers
    module ContentApi
      class ArtefactStub
        include WebMock::API
        # This is ugly, but the nicest way we found to get access to artefact_for_slug
        include GdsApi::TestHelpers::ContentApi

        attr_accessor :slug, :query_parameters, :response_body, :response_status

        def initialize(slug)
          @slug = slug
          @query_parameters = {}
          @response_body = artefact_for_slug(slug)
          @response_status = 200
        end
        
        def with_query_parameters(hash)
          @query_parameters = hash
          self
        end

        def with_response_body(response_body)
          @response_body = response_body
          self
        end

        def with_response_status(response_status)
          @response_status = response_status
          self
        end

        # Nothing is stubbed until this is called
        def stub
          comparable_query_params = @query_parameters.each_with_object({}) { |(k,v),hash| hash[k.to_s] = v.nil? ? v : v.to_s }
          stub_request(:get, url_without_query)
              .with(query: hash_including(comparable_query_params))
              .to_return(status: @response_status, body: @response_body.to_json)
        end
        
        private
          def url_without_query
            "#{CONTENT_API_ENDPOINT}/#{slug}.json"
          end
      end
    end
  end
end
Make Artefact stubbing permissive on query params
Make Artefact stubbing permissive on query params

Previously, requests had to have exactly the specified query params,
now they just have to have the params you specify.

Specific motivation is that lots of requests in frontend will soon
include latitude and longitude, but it's not something that matters
in most of the tests, so it's nicer if the stub doesn't care.
