Class: Dalziel::Matchers::ResponseMatcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_expression) ⇒ ResponseMatcher

Returns a new instance of ResponseMatcher.



178
179
180
181
# File 'lib/dalziel.rb', line 178

def initialize(json_expression)
  @json_expression = json_expression
  @status = 200
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



176
177
178
# File 'lib/dalziel.rb', line 176

def body
  @body
end

#json_expressionObject (readonly)

Returns the value of attribute json_expression.



176
177
178
# File 'lib/dalziel.rb', line 176

def json_expression
  @json_expression
end

#responseObject (readonly)

Returns the value of attribute response.



176
177
178
# File 'lib/dalziel.rb', line 176

def response
  @response
end

Instance Method Details

#does_not_match?(response) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/dalziel.rb', line 195

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

#failure_messageObject



199
200
201
202
203
204
205
206
207
# File 'lib/dalziel.rb', line 199

def failure_message
  if !@is_json
    "Content-Type is not JSON.\n\n%s\n\nContent-Type is %s" % [ show_response, @content_type.inspect ]
  elsif !@status_match
    "Unexpected response status.\n\n%s\n\nExpected response status to be %s, but was %s." % [ show_response, @status.inspect, response.status ]
  else
    "Response body did not match.\n\n%s\n\n%s" % [ show_response, payload_matcher.last_error ]
  end
end

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
191
192
193
# File 'lib/dalziel.rb', line 183

def matches?(response)
  @response = response
  @body = JSON.parse(response.body)
  @content_type = response.headers["Content-Type"]

  @is_json = (@content_type.to_s.split(";",2).first =~ /\bjson$/)
  @json_match = (payload_matcher =~ @body)
  @status_match = (@status == response.status)

  @status_match && @is_json && @json_match
end

#payload_matcherObject



209
210
211
# File 'lib/dalziel.rb', line 209

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

#show_responseObject



213
214
215
216
# File 'lib/dalziel.rb', line 213

def show_response
  headers = Dalziel.format_headers(response.headers)
  Dalziel.indent "HTTP/1.1 %s\n\n%s\n\n%s" % [ response.status, headers, JSON.pretty_generate(body) ]
end

#status(code) ⇒ Object



218
219
220
221
# File 'lib/dalziel.rb', line 218

def status(code)
  @status = code
  self
end