Sha256: 2d8abb70efb586dad2ff328707e1ce24cd0b1d914b6433cf8cd9eeea3e0e4484

Contents?: true

Size: 1.5 KB

Versions: 7

Compression:

Stored size: 1.5 KB

Contents

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
          else
            # if we reach here no stubs matched
            raise(Excon::Errors::StubNotFound.new('no stubs matched ' << datum.inspect))
          end
        end

        @stack.request_call(datum)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
excon-0.39.5 lib/excon/middlewares/mock.rb
excon-0.39.4 lib/excon/middlewares/mock.rb
excon-0.39.3 lib/excon/middlewares/mock.rb
excon-0.39.2 lib/excon/middlewares/mock.rb
excon-0.39.1 lib/excon/middlewares/mock.rb
excon-0.39.0 lib/excon/middlewares/mock.rb
excon-0.38.0 lib/excon/middlewares/mock.rb