Sha256: 37298fe789345d1a6e2839cb1d7a3e0bc8e2d8435330a6208c8b010aec70f0a6

Contents?: true

Size: 1.42 KB

Versions: 12

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'rails_helper'

describe DHS::OptionBlocks do
  let(:status) { 200 }

  before do
    class Record < DHS::Record
      endpoint 'http://records'
    end

    stub_request(:get, 'http://records/?id=1234')
      .with(headers: { 'Tracking-Id' => 1 })
      .to_return(status: status)
  end

  it 'allows to apply options to all requests made within a certain block' do
    DHS.options(headers: { 'Tracking-Id': 1 }) do
      Record.find(1234)
    end
  end

  it 'ensures that option blocks are reset after the block has been executed' do
    expect(DHS::OptionBlocks::CurrentOptionBlock.options).to eq nil
    DHS.options(headers: { 'Tracking-Id': 1 }) do
      Record.find(1234)
    end
    expect(DHS::OptionBlocks::CurrentOptionBlock.options).to eq nil
  end

  context 'failing request' do
    let(:status) { 400 }

    it 'ensures that option blocks are reset when an exception occures in the block' do
      expect(DHS::OptionBlocks::CurrentOptionBlock.options).to eq nil
      DHS.options(headers: { 'Tracking-Id': 1 }) do
        begin
          Record.find(1234)
        rescue DHC::Error
        end
      end
      expect(DHS::OptionBlocks::CurrentOptionBlock.options).to eq nil
    end
  end

  context 'parallel requests' do
    it 'does not fail merging option blocks for parallel requests' do
      DHS.options(headers: { 'Tracking-Id': 1 }) do
        Record.find(1234, 1234)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
dhs-1.6.0 spec/option_blocks/main_spec.rb
dhs-1.5.0 spec/option_blocks/main_spec.rb
dhs-1.4.2 spec/option_blocks/main_spec.rb
dhs-1.4.1 spec/option_blocks/main_spec.rb
dhs-1.4.0 spec/option_blocks/main_spec.rb
dhs-1.3.0 spec/option_blocks/main_spec.rb
dhs-1.2.0 spec/option_blocks/main_spec.rb
dhs-1.1.0 spec/option_blocks/main_spec.rb
dhs-1.0.3 spec/option_blocks/main_spec.rb
dhs-1.0.2 spec/option_blocks/main_spec.rb
dhs-1.0.1 spec/option_blocks/main_spec.rb
dhs-1.0.0 spec/option_blocks/main_spec.rb