Sha256: 34cd7322d574026dfc6cf0dc8bf7faa0ac5b7232e84e69d181d48ea1d0ee235c

Contents?: true

Size: 1.51 KB

Versions: 2

Compression:

Stored size: 1.51 KB

Contents

module WebMock

  class StubRegistry
    include Singleton

    attr_accessor :request_stubs
    attr_accessor :global_stub

    def initialize
      reset!
    end

    def reset!
      self.request_stubs = global_stub ? [global_stub] : []
    end

    def global_stub_block=(block)
      self.global_stub = ::WebMock::RequestStub.new(:any, /.*/)

      # This hash contains the responses returned by the block,
      # keyed by the exact request (using the object_id).
      # That way, there's no race condition in case #to_return
      # doesn't run immediately after stub.with.
      responses = {}

      self.global_stub.with { |request|
        responses[request.object_id] = block.call(request)
      }.to_return(lambda { |request| responses.delete(request.object_id) })

      register_request_stub(self.global_stub)
    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

    private

    def request_stub_for(request_signature)
      request_stubs.detect { |registered_request_stub|
        registered_request_stub.request_pattern.matches?(request_signature)
      }
    end

    def evaluate_response_for_request(response, request_signature)
      response.evaluate(request_signature)
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webmock-1.7.10 lib/webmock/stub_registry.rb
webmock-1.7.8 lib/webmock/stub_registry.rb