<commit_before>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<commit_msg>Make Artefact stubbing permissive on query params<commit_after>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
{'fraction_non_alphanumeric': 0.07320490367775832, 'fraction_numerical': 0.0021015761821366026, 'mean_word_length': 2.0158394931362196, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3627673', 'n_tokens_mistral': 809, 'n_tokens_neox': 816, 'n_words': 189}