class RequestInterceptor::Matchers::InterceptedRequest

Attributes

body[R]
method[R]
path[R]
query[R]
transactions[R]

Public Class Methods

new(method, path) click to toggle source
# File lib/request_interceptor/matchers.rb, line 19
def initialize(method, path)
  @method = method
  @path = path
  @count = (1..Float::INFINITY)
  @transactions = []
end

Public Instance Methods

count(count = nil) click to toggle source

Chains

# File lib/request_interceptor/matchers.rb, line 30
def count(count = nil)
  return @count if count.nil?

  @count =
    case count
    when Integer
      (count .. count)
    when Range
      count
    else
      raise ArgumentError
    end

  self
end
description() click to toggle source
# File lib/request_interceptor/matchers.rb, line 115
def description
  "should intercept a #{format_method(method)} request to #{path}"
end
failure_message() click to toggle source
# File lib/request_interceptor/matchers.rb, line 81
def failure_message
  expected_request = "#{format_method(method)} #{path}"
  expected_request += " with query #{format_object(query)}" if query
  expected_request += " and" if query && body
  expected_request += " with body #{format_object(body)}" if body

  similar_intercepted_requests = similar_transactions.map.with_index do |transaction, index|
    method = format_method(transaction.request.method)
    path = transaction.request.path
    query = transaction.request.query
    body = transaction.request.body
    indentation_required = index != 0

    message = "#{method} #{path}"
    message += (query.nil? || query.empty?) ? " with no query" : " with query #{format_object(query)}"
    message += (body.nil? || body.empty?) ? " and with no body" : " and with body #{format_object(body)}"
    message = " " * 10 + message if indentation_required
    message
  end

  similar_intercepted_requests = ["none"] if similar_intercepted_requests.none?

  "\nexpected: #{expected_request}" \
    "\n     got: #{similar_intercepted_requests.join("\n")}"
end
failure_message_when_negated() click to toggle source
# File lib/request_interceptor/matchers.rb, line 107
def failure_message_when_negated
  message = "intercepted a #{format_method(method)} request to #{path}"
  message += " with query #{format_object(query)}" if query
  message += " and" if query && body
  message += " with body #{format_object(body)}" if body
  message
end
matches?(transactions) click to toggle source

Rspec Matcher Protocol

# File lib/request_interceptor/matchers.rb, line 76
def matches?(transactions)
  @transactions = transactions
  count.cover?(matching_transactions.count)
end
with_body(body) click to toggle source
# File lib/request_interceptor/matchers.rb, line 59
def with_body(body)
  body_matcher =
    if body.respond_to?(:matches?) && body.respond_to?(:failure_message)
      body
    else
      RSpec::Matchers::BuiltIn::Eq.new(body)
    end

  @body = MatcherWrapper.new(body_matcher)

  self
end
with_query(query) click to toggle source
# File lib/request_interceptor/matchers.rb, line 46
def with_query(query)
  query_matcher =
    if query.respond_to?(:matches?) && query.respond_to?(:failure_message)
      query
    else
      RSpec::Matchers::BuiltIn::Eq.new(query)
    end

  @query = MatcherWrapper.new(query_matcher)

  self
end

Private Instance Methods

format_method(method) click to toggle source

Helper methods

# File lib/request_interceptor/matchers.rb, line 125
def format_method(method)
  method.to_s.upcase
end
format_object(object) click to toggle source
# File lib/request_interceptor/matchers.rb, line 129
def format_object(object)
  RSpec::Support::ObjectFormatter.format(object)
end
matching_transactions() click to toggle source
# File lib/request_interceptor/matchers.rb, line 133
def matching_transactions
  transactions.select do |transaction|
    request = transaction.request
    request.method?(method) &&
      request.path?(path) &&
      request.query?(query) &&
      request.body?(body)
  end
end
similar_transactions() click to toggle source
# File lib/request_interceptor/matchers.rb, line 143
def similar_transactions
  transactions.select do |transaction|
    request = transaction.request
    request.method?(method) && request.path?(path)
  end
end