Sha256: 0dcf812a6b54dfb63a87953e66a47b7b1df628f538eb605fa3506f4af77ce194

Contents?: true

Size: 1.48 KB

Versions: 21

Compression:

Stored size: 1.48 KB

Contents

module WebMock

  class StubRegistry
    include Singleton

    attr_accessor :request_stubs

    def initialize
      reset!
    end

    def global_stubs
      @global_stubs ||= []
    end

    def reset!
      self.request_stubs = []
    end

    def register_global_stub(&block)
      # 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 = {}

      stub = ::WebMock::RequestStub.new(:any, /.*/).with { |request|
        responses[request.object_id] = block.call(request)
      }.to_return(lambda { |request| responses.delete(request.object_id) })

      global_stubs.push 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)
      (global_stubs + 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

21 entries across 21 versions & 3 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/webmock-1.8.11/lib/webmock/stub_registry.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/webmock-1.8.11/lib/webmock/stub_registry.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/webmock-1.8.11/lib/webmock/stub_registry.rb
webmock-1.9.3 lib/webmock/stub_registry.rb
webmock-1.9.2 lib/webmock/stub_registry.rb
webmock-1.9.1 lib/webmock/stub_registry.rb
simple-client-0.0.3 vendor/bundle/ruby/1.9.1/gems/webmock-1.8.8/lib/webmock/stub_registry.rb
webmock-1.9.0 lib/webmock/stub_registry.rb
webmock-1.8.11 lib/webmock/stub_registry.rb
webmock-1.8.10 lib/webmock/stub_registry.rb
webmock-1.8.9 lib/webmock/stub_registry.rb
simple-client-0.0.2 vendor/bundle/ruby/1.8/gems/webmock-1.8.8/lib/webmock/stub_registry.rb
webmock-1.8.8 lib/webmock/stub_registry.rb
webmock-1.8.7 lib/webmock/stub_registry.rb
webmock-1.8.6 lib/webmock/stub_registry.rb
webmock-1.8.5 lib/webmock/stub_registry.rb
webmock-1.8.4 lib/webmock/stub_registry.rb
webmock-1.8.3 lib/webmock/stub_registry.rb
webmock-1.8.2 lib/webmock/stub_registry.rb
webmock-1.8.1 lib/webmock/stub_registry.rb