Sha256: 4d067d377eda3943a2a77562340cce7dd757728df43147320d1bb925fff3780a

Contents?: true

Size: 1.61 KB

Versions: 30

Compression:

Stored size: 1.61 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
            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

30 entries across 30 versions & 4 rubygems

Version Path
able-neo4j-1.0.0 vendor/bundle/jruby/1.9/gems/excon-0.45.4/lib/excon/middlewares/mock.rb
excon-0.49.0 lib/excon/middlewares/mock.rb
excon-0.48.0 lib/excon/middlewares/mock.rb
excon-0.47.0 lib/excon/middlewares/mock.rb
excon-0.46.0 lib/excon/middlewares/mock.rb
framed_rails-0.2.0 vendor/gems/excon-0.45.3/lib/excon/middlewares/mock.rb
framed_rails-0.1.7 vendor/gems/excon-0.45.3/lib/excon/middlewares/mock.rb
vagrant-cloudstack-1.2.0 vendor/bundle/gems/excon-0.45.4/lib/excon/middlewares/mock.rb
framed_rails-0.1.5 vendor/gems/excon-0.45.3/lib/excon/middlewares/mock.rb
framed_rails-0.1.4 vendor/gems/excon-0.45.3/lib/excon/middlewares/mock.rb
framed_rails-0.1.3 vendor/gems/excon-0.45.3/lib/excon/middlewares/mock.rb
framed_rails-0.1.2 vendor/gems/excon-0.45.3/lib/excon/middlewares/mock.rb
framed_rails-0.1.0 vendor/gems/excon-0.45.3/lib/excon/middlewares/mock.rb
excon-0.45.4 lib/excon/middlewares/mock.rb
vagrant-cloudstack-1.1.0 vendor/bundle/gems/excon-0.45.3/lib/excon/middlewares/mock.rb
excon-0.45.3 lib/excon/middlewares/mock.rb
excon-0.45.2 lib/excon/middlewares/mock.rb
excon-0.45.1 lib/excon/middlewares/mock.rb
excon-0.45.0 lib/excon/middlewares/mock.rb
excon-0.44.4 lib/excon/middlewares/mock.rb