Class: ResponseBodyChecker
- Defined in:
- lib/plugins/plug04_response_body_checker.rb
Instance Method Summary collapse
Methods inherited from Checker
available_plugins, #initialize
Constructor Details
This class inherits a constructor from Checker
Instance Method Details
#check ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/plugins/plug04_response_body_checker.rb', line 4 def check result = Result.new(@testcase, @response) begin # special case: the whole body has to be matched via a regular expression if is_regex?(@testcase.response_expectation['body']) if not regex_matches?(@testcase.response_expectation['body'], @response.body) result.succeeded = false result. = " expected the whole body to match regex --#{@testcase.response_expectation['body']}--\n got --#{@response.body}--" end return result end expected_body_hash = @testcase.response_expectation['body'] # in case we have no body expectation we simply return success return result if expected_body_hash.nil? # in case the response body is nil or damaged we return an error begin responded_body_hash = JSON.parse(@response.body) rescue result = Result.new(@testcase, @response) result.succeeded = false result. = " expected response to have a body\n got raw body --#{@response.body}-- which is nil or an unparseable hash" return result end # else we build trees from both body structures... expectation_tree = Nokogiri::XML(expected_body_hash.to_xml({ :indent => 0 })) response_tree = Nokogiri::XML(responded_body_hash.to_xml({ :indent => 0 })) # retrieve all the leafs pathes and match the leafs values using xpath matcher_pathes_from(expectation_tree).each do |path| expectation_node = expectation_tree.xpath(path).first response_node = response_tree.xpath(path).first # in some (not awesome) cases the root node occures as leaf, so we have to skip him here next if expectation_node.name == "hash" # return error if response body does not have the expected entry if response_node.nil? result.succeeded = false result. = " expected body to have identifier --#{expectation_node.name}--\n got nil" return result end # last but not least try the regex or direct match and return errors in case of any if is_regex?(expectation_node.text) if not (excluded?(expectation_node.name) or regex_matches?(expectation_node.text, response_node.text)) result.succeeded = false result. = " expected body identifier --#{expectation_node.name}-- to match regex --#{expectation_node.text}--\n got --#{response_node.text}--" end else if not (excluded?(expectation_node.name) or string_matches?(expectation_node.text, response_node.text)) result.succeeded = false result. = " expected body identifier --#{expectation_node.name}-- to match --#{expectation_node.text}--\n got --#{response_node.text}--" end end end rescue result.succeeded = false result. = " unexpected error while parsing testcase/response. Check your testcase format!" end result end |