Class: Strelka::Testing::FinishWithMatcher

Inherits:
Object
  • Object
show all
Extended by:
Loggability
Defined in:
lib/strelka/testing.rb

Overview

finish_with matcher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, expected_message = nil, expected_headers = {}) ⇒ FinishWithMatcher

Create a new matcher for the specified status, expected_message, and expected_headers.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/strelka/testing.rb', line 40

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

Instance Attribute Details

#expected_headersObject (readonly)

The data structures expected to be part of the response's status_info.



62
63
64
# File 'lib/strelka/testing.rb', line 62

def expected_headers
  @expected_headers
end

#expected_messageObject (readonly)

The data structures expected to be part of the response's status_info.



62
63
64
# File 'lib/strelka/testing.rb', line 62

def expected_message
  @expected_message
end

#expected_statusObject (readonly)

The data structures expected to be part of the response's status_info.



62
63
64
# File 'lib/strelka/testing.rb', line 62

def expected_status
  @expected_status
end

Instance Method Details

#and_header(name, value = nil) ⇒ Object

Also expect a header with the given name and value from the response.



72
73
74
75
76
77
78
79
# File 'lib/strelka/testing.rb', line 72

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_finish(status_info) ⇒ Object

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.



103
104
105
106
107
# File 'lib/strelka/testing.rb', line 103

def check_finish( status_info )
	return true if status_info && status_info.is_a?( Hash )
	@failure = "an abnormal status"
	return false
end

#check_headers(status_info) ⇒ Object

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.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/strelka/testing.rb', line 141

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_message(status_info) ⇒ Object

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.



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/strelka/testing.rb', line 123

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_status_code(status_info) ⇒ Object

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.



112
113
114
115
116
117
# File 'lib/strelka/testing.rb', line 112

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

#failure_messageObject

Return a message suitable for describing when the matcher fails when it should succeed.



179
180
181
# File 'lib/strelka/testing.rb', line 179

def failure_message
	return "expected response to finish_with %s" % [ @failure ]
end

#failure_message_when_negatedObject

Return a message suitable for describing when the matcher succeeds when it should fail.



185
186
187
# File 'lib/strelka/testing.rb', line 185

def failure_message_when_negated
	return "expected response not to finish_with %s" % [ @failure ]
end

#matches?(given_proc) ⇒ Boolean

RSpec matcher API -- call the given_proc and ensure that it behaves in the expected manner.

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/strelka/testing.rb', line 84

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

#supports_block_expectations?Boolean

Matcher API -- return true to enable the use of block expectations.

Returns:

  • (Boolean)


66
67
68
# File 'lib/strelka/testing.rb', line 66

def supports_block_expectations?
	return true
end