Sha256: dc1fad14247c14127c2094600fe47598e8c9145d56c94819056f77ef3b1c3939

Contents?: true

Size: 1.77 KB

Versions: 2

Compression:

Stored size: 1.77 KB

Contents

module Faraday
  module Adapter
    module MockRequest
      extend Faraday::Connection::Options
      def self.loaded?() false end

      include Faraday::Error # ConnectionFailed

      class Stubs
        def initialize
          # {:get => [Stub, Stub]}
          @stack = {}
          yield self if block_given?
        end

        def empty?
          @stack.empty?
        end

        def match(request_method, path, request_headers)
          return false if !@stack.key?(request_method)
          @stack[request_method].detect { |stub| stub.matches?(path, request_headers) }
        end

        def get(path, request_headers = {}, &block)
          (@stack[:get] ||= []) << new_stub(path, request_headers, block)
        end

        def new_stub(path, request_headers, block)
          status, response_headers, body = block.call
          Stub.new(path, request_headers, status, response_headers, body)
        end
      end

      class Stub < Struct.new(:path, :request_headers, :status, :response_headers, :body)
        def matches?(request_path, headers)
          return false if request_path != path
          return true  if request_headers.empty?
          request_headers.each do |key, value|
            return true if headers[key] == value
          end 
          false
        end
      end

      def initialize &block
        super nil
        yield stubs
      end

      def stubs
        @stubs ||= Stubs.new
      end

      def _get(uri, headers)
        raise ConnectionFailed, "no stubbed requests" if stubs.empty?
        if stub = @stubs.match(:get, uri.path, headers)
          response_class.new do |resp|
            resp.headers = stub.response_headers
            resp.process stub.body
          end
        else
          nil
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
faraday-0.0.2 lib/faraday/adapter/mock_request.rb
faraday-0.0.1 lib/faraday/adapter/mock_request.rb