<commit_before>require 'gds_api/test_helpers/json_client_helper'
require 'gds_api/test_helpers/content_item_helpers'
require 'json'

module GdsApi
  module TestHelpers
    module ContentStore
      include ContentItemHelpers

      CONTENT_STORE_ENDPOINT = Plek.current.find('content-store')

      # Stubs a content item in the content store.
      # The following options can be passed in:
      #
      #   :max_age  will set the max-age of the Cache-Control header in the response. Defaults to 900
      #   :private  if true, the Cache-Control header will include the "private" directive. By default it
      #             will include "public"
      def content_store_has_item(base_path, body = content_item_for_base_path(base_path), options = {})
        max_age = options.fetch(:max_age, 900)
        visibility = options[:private] ? "private" : "public"
        url = CONTENT_STORE_ENDPOINT + "/content" + base_path
        body = body.to_json unless body.is_a?(String)

        stub_request(:get, url).to_return(
          status: 200,
          body: body,
          headers: {
            cache_control: "#{visibility}, max-age=#{max_age}",
            date: Time.now.httpdate
          }
        )
      end

      def content_store_does_not_have_item(base_path)
        url = CONTENT_STORE_ENDPOINT + "/content" + base_path
        stub_request(:get, url).to_return(status: 404, headers: {})

        url = CONTENT_STORE_ENDPOINT + "/incoming-links" + base_path
        stub_request(:get, url).to_return(status: 404, headers: {})
      end

      def content_store_isnt_available
        stub_request(:any, /#{CONTENT_STORE_ENDPOINT}\/.*/).to_return(:status => 503)
      end

      def content_item_for_base_path(base_path)
        super.merge({ "base_path" => base_path })
      end

      def content_store_has_incoming_links(base_path, links)
        url = CONTENT_STORE_ENDPOINT + "/incoming-links" + base_path
        body = links.to_json

        stub_request(:get, url).to_return(body: body)
      end
    end
  end
end<commit_msg>Enable Draft Content Store testing<commit_after>require 'gds_api/test_helpers/json_client_helper'
require 'gds_api/test_helpers/content_item_helpers'
require 'json'

module GdsApi
  module TestHelpers
    module ContentStore
      include ContentItemHelpers

      # Stubs a content item in the content store.
      # The following options can be passed in:
      #
      #   :max_age  will set the max-age of the Cache-Control header in the response. Defaults to 900
      #   :private  if true, the Cache-Control header will include the "private" directive. By default it
      #             will include "public"
      #   :draft    will point to the draft content store if set to true

      def content_store_endpoint(draft=false)
        draft ? Plek.current.find('draft-content-store') : Plek.current.find('content-store')
      end

      def content_store_has_item(base_path, body = content_item_for_base_path(base_path), options = {})
        max_age = options.fetch(:max_age, 900)
        visibility = options[:private] ? "private" : "public"
        url = content_store_endpoint(options[:draft]) + "/content" + base_path
        body = body.to_json unless body.is_a?(String)

        stub_request(:get, url).to_return(
          status: 200,
          body: body,
          headers: {
            cache_control: "#{visibility}, max-age=#{max_age}",
            date: Time.now.httpdate
          }
        )
      end

      def content_store_does_not_have_item(base_path, options = {})
        url = content_store_endpoint(options[:draft]) + "/content" + base_path
        stub_request(:get, url).to_return(status: 404, headers: {})

        url = content_store_endpoint(options[:draft]) + "/incoming-links" + base_path
        stub_request(:get, url).to_return(status: 404, headers: {})
      end

      def content_store_isnt_available
        stub_request(:any, /#{content_store_endpoint}\/.*/).to_return(:status => 503)
      end

      def content_item_for_base_path(base_path)
        super.merge({ "base_path" => base_path })
      end

      def content_store_has_incoming_links(base_path, links)
        url = content_store_endpoint + "/incoming-links" + base_path
        body = links.to_json

        stub_request(:get, url).to_return(body: body)
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09862068965517241, 'fraction_numerical': 0.008275862068965517, 'mean_word_length': 2.783478260869565, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5438577', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1329, 'n_words': 306}