Sha256: e6e6e19348b7a3d1bd8ead2cc53095452518afb3b87d33308c8927fc5650dde9

Contents?: true

Size: 1.87 KB

Versions: 20

Compression:

Stored size: 1.87 KB

Contents

# frozen_string_literal: true
module Excon
  module Middleware
    class Mock < Excon::Middleware::Base
      def self.valid_parameter_keys
        [
          :allow_unstubbed_requests,
          :captures,
          :mock
        ]
      end

      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[:remote_ip] ||= '127.0.0.1'
            datum[:response] = {
              :body       => '',
              :headers    => {},
              :status     => 200,
              :remote_ip  => datum[:remote_ip]
            }

            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

20 entries across 20 versions & 3 rubygems

Version Path
coingecko_client-0.1.2 vendor/bundle/ruby/2.7.0/gems/excon-0.75.0/lib/excon/middlewares/mock.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/excon-0.78.1/lib/excon/middlewares/mock.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/excon-0.78.1/lib/excon/middlewares/mock.rb
excon-0.78.1 lib/excon/middlewares/mock.rb
excon-0.78.0 lib/excon/middlewares/mock.rb
excon-0.76.0 lib/excon/middlewares/mock.rb
coingecko_client-0.1.1 vendor/bundle/ruby/2.7.0/gems/excon-0.75.0/lib/excon/middlewares/mock.rb
coingecko_client-0.1.0 vendor/bundle/ruby/2.7.0/gems/excon-0.75.0/lib/excon/middlewares/mock.rb
excon-0.75.0 lib/excon/middlewares/mock.rb
excon-0.74.0 lib/excon/middlewares/mock.rb
excon-0.73.0 lib/excon/middlewares/mock.rb
excon-0.72.0 lib/excon/middlewares/mock.rb
excon-0.71.1 lib/excon/middlewares/mock.rb
excon-0.71.0 lib/excon/middlewares/mock.rb
excon-0.70.0 lib/excon/middlewares/mock.rb
excon-0.69.1 lib/excon/middlewares/mock.rb
excon-0.69.0 lib/excon/middlewares/mock.rb
excon-0.68.0 lib/excon/middlewares/mock.rb
excon-0.67.0 lib/excon/middlewares/mock.rb
excon-0.66.0 lib/excon/middlewares/mock.rb