Sha256: 8bc7ce81d7082055df7fb5378d6f2eec4f1bdd01367bea68ab0f34eaa278f7b3

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module ApiRequestHelper
  def stub_api_request(config, path, body = nil)
    options = {
      :query => {
        :api_key => config[:push_api_key],
        :name => config[:name],
        :environment => config.respond_to?(:env) ? config.env : config[:environment],
        :hostname => config[:hostname],
        :gem_version => Appsignal::VERSION
      },
      :headers => {
        "Content-Type" => "application/json; charset=UTF-8"
      }
    }
    body = Appsignal::Utils::JSON.generate(body) if body.is_a? Hash
    options[:body] = body if body
    endpoint = config[:endpoint] || Appsignal::Config::DEFAULT_CONFIG[:endpoint]
    stub_request(:post, "#{endpoint}/1/#{path}").with(options)
  end

  def stub_check_in_request(events:, response: { :status => 200 })
    config = Appsignal.config
    options = {
      :query => {
        :api_key => config[:push_api_key],
        :name => config[:name],
        :environment => config.respond_to?(:env) ? config.env : config[:environment],
        :hostname => config[:hostname],
        :gem_version => Appsignal::VERSION
      },
      :headers => { "Content-Type" => "application/x-ndjson; charset=UTF-8" }
    }

    request_stub =
      stub_request(
        :post,
        "#{config[:logging_endpoint]}/check_ins/json"
      ).with(options) do |request|
        # Parse each line as JSON per the NDJSON format
        payloads = request.body.split("\n").map { |line| JSON.parse(line) }
        formatted_events =
          events.map do |event|
            {
              "identifier" => nil,
              "digest" => kind_of(String),
              "kind" => "start",
              "timestamp" => kind_of(Integer),
              "check_in_type" => "cron"
            }.merge(event)
          end
        expect(payloads).to include(*formatted_events)
      end

    if response.is_a?(Exception)
      request_stub.to_raise(response)
    else
      request_stub.to_return(response)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
appsignal-4.0.6-java spec/support/helpers/api_request_helper.rb
appsignal-4.0.6 spec/support/helpers/api_request_helper.rb