Sha256: 677d949fb9c93a1317b5b01b3834899099b36df1bff90ea80ad2e28716cb5cde

Contents?: true

Size: 1.68 KB

Versions: 15

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true
module Excon
  module Middleware
    class Mock < Excon::Middleware::Base
      def request_call(datum)
        if datum[:mock]
          # convert File/Tempfile body to string before matching:
          if datum[:body].respond_to?(:read)
           if datum[:body].respond_to?(:binmode)
             datum[:body].binmode
           end
           if datum[:body].respond_to?(:rewind)
             datum[:body].rewind
           end
           datum[:body] = datum[:body].read
          elsif !datum[:body].nil? && !datum[:body].is_a?(String)
            raise Excon::Errors::InvalidStub.new("Request body should be a string or an IO object. #{datum[:body].class} provided")
          end

          if stub = Excon.stub_for(datum)
            datum[:response] = {
              :body       => '',
              :headers    => {},
              :status     => 200,
              :remote_ip  => '127.0.0.1'
            }

            stub_datum = case stub.last
            when Proc
              stub.last.call(datum)
            else
              stub.last
            end

            datum[:response].merge!(stub_datum.reject {|key,value| key == :headers})
            if stub_datum.has_key?(:headers)
              datum[:response][:headers].merge!(stub_datum[:headers])
            end
          elsif datum[:allow_unstubbed_requests] != true
            # if we reach here no stubs matched
            message = StringIO.new
            message.puts('no stubs matched')
            Excon::PrettyPrinter.pp(message, datum)
            raise(Excon::Errors::StubNotFound.new(message.string))
          end
        end

        @stack.request_call(datum)
      end
    end
  end
end

Version data entries

15 entries across 13 versions & 2 rubygems

Version Path
vagrant-packet-0.1.1 vendor/bundle/ruby/2.3.0/gems/excon-0.62.0/lib/excon/middlewares/mock.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.5.0/gems/excon-0.62.0/lib/excon/middlewares/mock.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/excon-0.62.0/lib/excon/middlewares/mock.rb
excon-0.62.0 lib/excon/middlewares/mock.rb
excon-0.61.0 lib/excon/middlewares/mock.rb
excon-0.60.0 lib/excon/middlewares/mock.rb
excon-0.59.0 lib/excon/middlewares/mock.rb
excon-0.58.0 lib/excon/middlewares/mock.rb
excon-0.57.1 lib/excon/middlewares/mock.rb
excon-0.57.0 lib/excon/middlewares/mock.rb
excon-0.56.0 lib/excon/middlewares/mock.rb
excon-0.55.0 lib/excon/middlewares/mock.rb
excon-0.54.0 lib/excon/middlewares/mock.rb
excon-0.53.0 lib/excon/middlewares/mock.rb
excon-0.52.0 lib/excon/middlewares/mock.rb