Sha256: 1776bb46065260f883d3ee20d264b4ccc96ab434576cbfc54ae38adf92535f97

Contents?: true

Size: 1.8 KB

Versions: 8

Compression:

Stored size: 1.8 KB

Contents

class Checker

  def initialize(testcase, response, excludes=nil)
    @testcase = testcase
    @response = response
    @excludes = excludes
  end

  # executes the checking routine and returns a result object
  # to be overwritten in child classes
  def check
    result = Result.new(@testcase, @response)
  end

private

  # recursively parses the tree and returns a set of relative pathes
  # that can be used to match the both trees leafs
  def matcher_pathes_from(node, pathes = nil)
    pathes ||= []
    if not node.children.blank?
      node.children.each do |sub_node|
        matcher_pathes_from(sub_node, pathes)
      end
    else
      pathes << relative_path(node.parent.path)
    end
    pathes
  end

  # returns relative path for matching the target tree of the response body
  # explicit array adressing is replaced by *
  def relative_path(path)
    path.gsub(/\/([^\/]+)\[\d+\]\//i,"/*/")
  end

  # returns true if given attributes is an excluded item that does not have to be evaluated in this environment
  def excluded?(item)
    @excludes.include?(item)
  end

  # returns true if given string seems to be a regular expression
  def is_regex?(string)
    string.to_s.match(/^\/.+\/$/)
  end

  # returns true if the given regular expression matches the given value
  def regex_matches?(regex, value)
    regex = Regexp.compile( regex.gsub(/^\//, '').gsub(/\/$/,'') )
    !!value.to_s.match(regex)
  end

  # returns true if the given string exactly matches the given value
  def string_matches?(string, value)
    string.to_s == value.to_s
  end

  # parses output into JSON object
  def valid_json?(response_body)
    # responses may be nil, return true then
    return true if response_body.blank?
    # returns true if given response is valid json, else false
    JSON.parse(response_body.to_s) rescue false
  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
apirunner-0.3.3 lib/checker.rb
apirunner-0.3.2 lib/checker.rb
apirunner-0.3.1 lib/checker.rb
apirunner-0.3.0 lib/checker.rb
apirunner-0.2.8 lib/checker.rb
apirunner-0.2.7 lib/checker.rb
apirunner-0.2.6 lib/checker.rb
apirunner-0.2.5 lib/checker.rb