Class: Dalziel::Matchers::RequestMatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_expression) ⇒ RequestMatcher

Returns a new instance of RequestMatcher.



122
123
124
# File 'lib/dalziel.rb', line 122

def initialize(json_expression)
  @json_expression = json_expression
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



120
121
122
# File 'lib/dalziel.rb', line 120

def body
  @body
end

#json_expressionObject (readonly)

Returns the value of attribute json_expression.



120
121
122
# File 'lib/dalziel.rb', line 120

def json_expression
  @json_expression
end

#requestObject (readonly)

Returns the value of attribute request.



120
121
122
# File 'lib/dalziel.rb', line 120

def request
  @request
end

Instance Method Details

#all_stubbed_requestsObject



168
169
170
# File 'lib/dalziel.rb', line 168

def all_stubbed_requests
  WebMock::RequestRegistry.instance.requested_signatures
end

#does_not_match?(response) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/dalziel.rb', line 145

def does_not_match?(response)
  fail "Inverted matching is not implemented with this matcher"
end

#failure_messageObject



149
150
151
152
153
154
155
156
157
# File 'lib/dalziel.rb', line 149

def failure_message
  if @request == []
    "No request matched"
  elsif !@is_json
    "Accept header is not JSON.\n\n%s\n\nAccept is %s" % [ show_request, @accept.inspect ]
  else
    "Request body did not match.\n\n%s\n\n%s" % [ show_request, payload_matcher.last_error ]
  end
end

#matches?(request_pattern) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/dalziel.rb', line 126

def matches?(request_pattern)
  @request = nil
  all_stubbed_requests.each { |request_signature, _count|
    if request_pattern.matches?(request_signature)
      @request = request_signature
      break
    end
  }
  return false if @request.nil?

  @body = JSON.parse(@request.body)
  @accept = @request.headers["Accept"]

  @is_json = @accept =~ /\bjson$/
  @json_match = payload_matcher =~ @body

  @is_json && @json_match
end

#payload_matcherObject



164
165
166
# File 'lib/dalziel.rb', line 164

def payload_matcher
  @payload_matcher ||= JsonExpressions::Matcher.new(json_expression)
end

#show_requestObject



159
160
161
162
# File 'lib/dalziel.rb', line 159

def show_request
  headers = Dalziel.format_headers(request.headers)
  Dalziel.indent "%s %s\n\n%s\n\n%s" % [ request.method.to_s.upcase, request.uri.to_s, headers, JSON.pretty_generate(body) ]
end