class Dalziel::Matchers::ResponseMatcher
Attributes
body[R]
json_expression[R]
response[R]
Public Class Methods
new(json_expression)
click to toggle source
# File lib/dalziel.rb, line 178 def initialize(json_expression) @json_expression = json_expression @status = 200 end
Public Instance Methods
does_not_match?(response)
click to toggle source
# File lib/dalziel.rb, line 195 def does_not_match?(response) fail "Inverted matching is not implemented with this matcher" end
failure_message()
click to toggle source
# 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)
click to toggle source
# 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_matcher()
click to toggle source
# File lib/dalziel.rb, line 209 def payload_matcher @payload_matcher ||= JsonExpressions::Matcher.new(json_expression) end
show_response()
click to toggle source
# 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)
click to toggle source
# File lib/dalziel.rb, line 218 def status(code) @status = code self end