class Strelka::Testing::FinishWithMatcher
finish_with matcher
Attributes
The data structures expected to be part of the response's status_info.
The data structures expected to be part of the response's status_info.
The data structures expected to be part of the response's status_info.
Public Class Methods
Create a new matcher for the specified status
, expected_message
, and expected_headers
.
# File lib/strelka/testing.rb, line 39 def initialize( status, expected_message=nil, expected_headers={} ) # Allow headers, but no message if expected_message.is_a?( Hash ) expected_headers = expected_message expected_message = nil end @expected_status = status @expected_message = expected_message @expected_headers = expected_headers || {} @failure = nil end
Public Instance Methods
Also expect a header with the given name
and value
from the response.
# File lib/strelka/testing.rb, line 71 def and_header( name, value=nil ) if name.is_a?( Hash ) self.expected_headers.merge!( name ) else self.expected_headers[ name ] = value end return self end
Check the result from calling the proc to ensure it's a status info Hash, returning true if so, or setting the failure message and returning false if not.
# File lib/strelka/testing.rb, line 102 def check_finish( status_info ) return true if status_info && status_info.is_a?( Hash ) @failure = "an abnormal status" return false end
Check the result's headers against the expectation, returning true if all expected headers were present and set to expected values, or setting the failure message and returning false if not.
# File lib/strelka/testing.rb, line 140 def check_headers( status_info ) headers = self.expected_headers or return true return true if headers.empty? status_headers = Mongrel2::Table.new( status_info[:headers] ) headers.each do |name, value| self.log.debug "Testing for %p header: %p" % [ name, value ] unless status_value = status_headers[ name ] @failure = "a %s header" % [ name ] return false end if status_value.empty? @failure = "a %s header matching %p, but it was blank" % [ name, value ] return false end self.log.debug " got value: %p" % [ status_value ] if value.respond_to?( :match ) unless value.match( status_value ) @failure = "a %s header matching %p, but got %p" % [ name, value, status_value ] return false end else unless value == status_value @failure = "the %s header %p, but got %p" % [ name, value, status_value ] return false end end end return true end
Check the result's status message against the expectation, returning true if it was present and matched the expectation, or setting the failure message and returning false if not.
# File lib/strelka/testing.rb, line 122 def check_message( status_info ) msg = self.expected_message or return true if msg.respond_to?( :match ) return true if msg.match( status_info[:message] ) @failure = "a message matching %p, but got: %p" % [ msg, status_info[:message] ] return false else return true if msg == status_info[:message] @failure = "the message %p, but got: %p" % [ msg, status_info[:message] ] return false end end
Check the result's status code against the expectation, returning true if it was the same, or setting the failure message and returning false if not.
# File lib/strelka/testing.rb, line 111 def check_status_code( status_info ) return true if status_info[:status] == self.expected_status @failure = "a %d status, but got %d instead" % [ self.expected_status, status_info[:status] ] return false end
Return a message suitable for describing when the matcher fails when it should succeed.
# File lib/strelka/testing.rb, line 178 def failure_message return "expected response to finish_with %s" % [ @failure ] end
Return a message suitable for describing when the matcher succeeds when it should fail.
# File lib/strelka/testing.rb, line 184 def failure_message_when_negated return "expected response not to finish_with %s" % [ @failure ] end
RSpec matcher API – call the given_proc
and ensure that it behaves in the expected manner.
# File lib/strelka/testing.rb, line 83 def matches?( given_proc ) result = nil status_info = catch( :finish ) do given_proc.call nil end self.log.debug "Test proc called; status info is: %p" % [ status_info ] return self.check_finish( status_info ) && self.check_status_code( status_info ) && self.check_message( status_info ) && self.check_headers( status_info ) end
Matcher API – return true to enable the use of block expectations.
# File lib/strelka/testing.rb, line 65 def supports_block_expectations? return true end