Class: Dalziel::Matchers::ResponseMatcher
- Inherits:
-
Object
- Object
- Dalziel::Matchers::ResponseMatcher
- Defined in:
- lib/dalziel.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#json_expression ⇒ Object
readonly
Returns the value of attribute json_expression.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #does_not_match?(response) ⇒ Boolean
- #failure_message ⇒ Object
-
#initialize(json_expression) ⇒ ResponseMatcher
constructor
A new instance of ResponseMatcher.
- #matches?(response) ⇒ Boolean
- #payload_matcher ⇒ Object
- #show_response ⇒ Object
- #status(code) ⇒ Object
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
176 177 178 |
# File 'lib/dalziel.rb', line 176 def body @body end |
#json_expression ⇒ Object (readonly)
Returns the value of attribute json_expression.
176 177 178 |
# File 'lib/dalziel.rb', line 176 def json_expression @json_expression end |
#response ⇒ Object (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
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_message ⇒ Object
199 200 201 202 203 204 205 206 207 |
# File 'lib/dalziel.rb', line 199 def 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
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_matcher ⇒ Object
209 210 211 |
# File 'lib/dalziel.rb', line 209 def payload_matcher @payload_matcher ||= JsonExpressions::Matcher.new(json_expression) end |
#show_response ⇒ Object
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 |