class WebMock::StubRegistry

Attributes

request_stubs[RW]

Public Class Methods

new() click to toggle source
# File lib/webmock/stub_registry.rb, line 8
def initialize
  reset!
end

Public Instance Methods

global_stubs() click to toggle source
# File lib/webmock/stub_registry.rb, line 12
def global_stubs
  @global_stubs ||= Hash.new { |h, k| h[k] = [] }
end
register_global_stub(order = :before_local_stubs) { |request| ... } click to toggle source
# File lib/webmock/stub_registry.rb, line 20
def register_global_stub(order = :before_local_stubs, &block)
  unless %i[before_local_stubs after_local_stubs].include?(order)
    raise ArgumentError.new("Wrong order. Use :before_local_stubs or :after_local_stubs")
  end

  # 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 = {}
  response_lock = Mutex.new

  stub = ::WebMock::RequestStub.new(:any, ->(uri) { true }).with { |request|
    update_response = -> { responses[request.object_id] = yield(request) }

    # The block can recurse, so only lock if we don't already own it
    if response_lock.owned?
      update_response.call
    else
      response_lock.synchronize(&update_response)
    end
  }.to_return(lambda { |request|
    response_lock.synchronize { responses.delete(request.object_id) }
  })

  global_stubs[order].push stub
end
register_request_stub(stub) click to toggle source
# File lib/webmock/stub_registry.rb, line 48
def register_request_stub(stub)
  request_stubs.insert(0, stub)
  stub
end
registered_request?(request_signature) click to toggle source
# File lib/webmock/stub_registry.rb, line 59
def registered_request?(request_signature)
  request_stub_for(request_signature)
end
remove_request_stub(stub) click to toggle source
# File lib/webmock/stub_registry.rb, line 53
def remove_request_stub(stub)
  if not request_stubs.delete(stub)
    raise "Request stub \n\n #{stub.to_s} \n\n is not registered."
  end
end
reset!() click to toggle source
# File lib/webmock/stub_registry.rb, line 16
def reset!
  self.request_stubs = []
end
response_for_request(request_signature) click to toggle source
# File lib/webmock/stub_registry.rb, line 63
def response_for_request(request_signature)
  stub = request_stub_for(request_signature)
  stub ? evaluate_response_for_request(stub.response, request_signature) : nil
end

Private Instance Methods

evaluate_response_for_request(response, request_signature) click to toggle source
# File lib/webmock/stub_registry.rb, line 77
def evaluate_response_for_request(response, request_signature)
  response.dup.evaluate(request_signature)
end
request_stub_for(request_signature) click to toggle source
# File lib/webmock/stub_registry.rb, line 70
def request_stub_for(request_signature)
  (global_stubs[:before_local_stubs] + request_stubs + global_stubs[:after_local_stubs])
    .detect { |registered_request_stub|
      registered_request_stub.request_pattern.matches?(request_signature)
    }
end