Sha256: 3c9a1a547b475439ebab0facd536beccea8b36cb1bb1cf06e1a649ec3479f4fd

Contents?: true

Size: 1.09 KB

Versions: 5

Compression:

Stored size: 1.09 KB

Contents

require 'rails_helper'

describe LHC do
  context 'interceptor' do
    before(:each) do
      class CacheInterceptor < LHC::Interceptor

        def before_request(_request)
          LHC::Response.new(Typhoeus::Response.new(response_body: 'Im served from cache'), nil)
        end
      end
      described_class.configure { |c| c.interceptors = [CacheInterceptor] }
    end

    it 'can return a response rather then doing a real request' do
      response = described_class.get('http://local.ch')
      expect(response.body).to eq 'Im served from cache'
    end

    context 'misusage' do
      before(:each) do
        class AnotherInterceptor < LHC::Interceptor
          def before_request(_request)
            LHC::Response.new(Typhoeus::Response.new({}), nil)
          end
        end
      end

      it 'raises an exception when two interceptors try to return a response' do
        expect(lambda {
          described_class.get('http://local.ch', interceptors: [CacheInterceptor, AnotherInterceptor])
        }).to raise_error 'Response already set from another interceptor'
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lhc-3.5.4 spec/interceptors/return_response_spec.rb
lhc-3.5.3 spec/interceptors/return_response_spec.rb
lhc-3.5.2 spec/interceptors/return_response_spec.rb
lhc-3.5.1 spec/interceptors/return_response_spec.rb
lhc-3.5.0 spec/interceptors/return_response_spec.rb