Sha256: 2571a8ab1a45b410c53bc91eea802cf64afb3c6f9c18f28fa3ea2e643ff83124

Contents?: true

Size: 1.88 KB

Versions: 60

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

RSpec.describe Faraday::Request::Instrumentation do
  class FakeInstrumenter
    attr_reader :instrumentations

    def initialize
      @instrumentations = []
    end

    def instrument(name, env)
      @instrumentations << [name, env]
      yield
    end
  end

  let(:config) { {} }
  let(:options) { Faraday::Request::Instrumentation::Options.from config }
  let(:instrumenter) { FakeInstrumenter.new }
  let(:conn) do
    Faraday.new do |f|
      f.request :instrumentation, config.merge(instrumenter: instrumenter)
      f.adapter :test do |stub|
        stub.get '/' do
          [200, {}, 'ok']
        end
      end
    end
  end

  it { expect(options.name).to eq('request.faraday') }
  it 'defaults to ActiveSupport::Notifications' do
    res = options.instrumenter
  rescue NameError => e
    expect(e.to_s).to match('ActiveSupport')
  else
    expect(res).to eq(ActiveSupport::Notifications)
  end

  it 'instruments with default name' do
    expect(instrumenter.instrumentations.size).to eq(0)

    res = conn.get '/'
    expect(res.body).to eq('ok')
    expect(instrumenter.instrumentations.size).to eq(1)

    name, env = instrumenter.instrumentations.first
    expect(name).to eq('request.faraday')
    expect(env[:url].path).to eq('/')
  end

  context 'with custom name' do
    let(:config) { { name: 'custom' } }

    it { expect(options.name).to eq('custom') }
    it 'instruments with custom name' do
      expect(instrumenter.instrumentations.size).to eq(0)

      res = conn.get '/'
      expect(res.body).to eq('ok')
      expect(instrumenter.instrumentations.size).to eq(1)

      name, env = instrumenter.instrumentations.first
      expect(name).to eq('custom')
      expect(env[:url].path).to eq('/')
    end
  end

  context 'with custom instrumenter' do
    let(:config) { { instrumenter: :custom } }

    it { expect(options.instrumenter).to eq(:custom) }
  end
end

Version data entries

60 entries across 60 versions & 4 rubygems

Version Path
faraday-2.12.1 spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.15 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.14 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
faraday-2.12.0 spec/faraday/request/instrumentation_spec.rb
faraday-2.11.0 spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.13 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.12 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.11 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
faraday-2.10.1 spec/faraday/request/instrumentation_spec.rb
faraday-2.10.0 spec/faraday/request/instrumentation_spec.rb
faraday-2.9.2 spec/faraday/request/instrumentation_spec.rb
faraday-2.9.1 spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.10 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.9 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.6 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.5 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.4 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
moneykit-0.1.3 vendor/bundle/ruby/3.2.0/gems/faraday-2.7.12/spec/faraday/request/instrumentation_spec.rb
faraday-2.9.0 spec/faraday/request/instrumentation_spec.rb
faraday-2.8.1 spec/faraday/request/instrumentation_spec.rb