Sha256: ee7c7c27d52f817bcfd7f6bebe570c57c94d85b05c545e56d5f23a8a1fb05e05

Contents?: true

Size: 1.45 KB

Versions: 5

Compression:

Stored size: 1.45 KB

Contents

module WebMock

  class RequestRegistry
    include Singleton

    attr_accessor :request_stubs, :requested_signatures

    def initialize
      reset_webmock
    end

    def reset_webmock
      self.request_stubs = []
      self.requested_signatures = Util::HashCounter.new
    end

    def register_request_stub(stub)
      request_stubs.insert(0, stub)
      stub
    end

    def registered_request?(request_signature)
      request_stub_for(request_signature)
    end

    def response_for_request(request_signature)
      stub = request_stub_for(request_signature)
      stub ? evaluate_response_for_request(stub.response, request_signature) : nil
    end

    def times_executed(request_profile)
      self.requested_signatures.hash.select { |request_signature, times_executed|
        request_signature.match(request_profile)
      }.inject(0) {|sum, (_, times_executed)| sum + times_executed }
    end

    private

    def request_stub_for(request_signature)
      request_stubs.detect { |registered_request_stub|
        request_signature.match(registered_request_stub.request_profile)
      }
    end

    def evaluate_response_for_request(response, request_signature)
      evaluated_response = response.dup
      [:body, :headers].each do |attribute|
        if response.options[attribute].is_a?(Proc)
          evaluated_response.options[attribute] = response.options[attribute].call(request_signature)
        end
      end
      evaluated_response
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
webmock-0.9.1 lib/webmock/request_registry.rb
webmock-0.9.0 lib/webmock/request_registry.rb
webmock-0.8.2 lib/webmock/request_registry.rb
webmock-0.8.1 lib/webmock/request_registry.rb
webmock-0.8.0 lib/webmock/request_registry.rb