Class: RequestInterceptor::Matchers::InterceptedRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/request_interceptor/matchers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path) ⇒ InterceptedRequest

Returns a new instance of InterceptedRequest.



19
20
21
22
23
24
# File 'lib/request_interceptor/matchers.rb', line 19

def initialize(method, path)
  @method = method
  @path = path
  @count = (1..Float::INFINITY)
  @transactions = []
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



16
17
18
# File 'lib/request_interceptor/matchers.rb', line 16

def body
  @body
end

#methodObject (readonly)

Returns the value of attribute method.



13
14
15
# File 'lib/request_interceptor/matchers.rb', line 13

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/request_interceptor/matchers.rb', line 14

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



15
16
17
# File 'lib/request_interceptor/matchers.rb', line 15

def query
  @query
end

#transactionsObject (readonly)

Returns the value of attribute transactions.



17
18
19
# File 'lib/request_interceptor/matchers.rb', line 17

def transactions
  @transactions
end

Instance Method Details

#count(count = nil) ⇒ Object

Chains



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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

#descriptionObject



115
116
117
# File 'lib/request_interceptor/matchers.rb', line 115

def description
  "should intercept a #{format_method(method)} request to #{path}"
end

#failure_messageObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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_negatedObject



107
108
109
110
111
112
113
# 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) ⇒ Boolean

Rspec Matcher Protocol

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/request_interceptor/matchers.rb', line 76

def matches?(transactions)
  @transactions = transactions
  count.cover?(matching_transactions.count)
end

#with_body(body) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# 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