module Webpoke

Constants

VERSION

Public Class Methods

document(group) click to toggle source
# File lib/webpoke.rb, line 197
def Webpoke.document(group)
  
  groups = {}
  
  $tests.each do |test|
    return if group && !test.group == group
    
    if (!groups.has_key? test.group)
      groups[test.group] = $groups[test.group] || {tests:[], name: test.group}
    end
    
    groups[test.group][:tests] << test.describe
    
    test.on_success.each do |dp|
      dp.call()
    end
    
  end
  
  return JSON.pretty_generate groups.values
  
end
failed() click to toggle source
# File lib/webpoke.rb, line 245
def Webpoke.failed
  return $errors
end
formats() click to toggle source
# File lib/webpoke.rb, line 253
def Webpoke.formats
  return ['json', 'html', 'stdout']
end
results() click to toggle source
# File lib/webpoke.rb, line 221
def Webpoke.results()
  
  if $config.format == 'stdout'
    puts "\nTests: #{Webpoke.tested}, success: #{Webpoke.success}, errors: #{Webpoke.failed}";
  else
    puts JSON.pretty_generate({
      tests: Webpoke.tested,
      success: Webpoke.success,
      errors: Webpoke.failed
      })
  end
  
  if (Webpoke.failed > 0)
    exit 1;
  else
    exit 0;
  end
  
end
success() click to toggle source
# File lib/webpoke.rb, line 241
def Webpoke.success
  return $successes
end
tested() click to toggle source
# File lib/webpoke.rb, line 249
def Webpoke.tested
  return $tested
end

Public Instance Methods

args_for_test(test) click to toggle source
# File lib/webpoke.rb, line 66
def args_for_test(test)
    
  if (!$config)
    $config = Webpoke::Config.new
  end
          
  args = {
    headers: $config.headers.merge(test.headers),
  }
  
  if test.data
    if $config.parse[:input]
      args[:body] = $config.parse[:input].call(test.data)
    else
      args[:data] = test.data
    end
  end
  
  if test.body
    args[:body] = test.body
  end
  
  args[:query] = test.query if test.query
  
  args
end
config(&block) click to toggle source

Configures the poking @param &block [Block] the configuration options (see #Webpoke::Config)

# File lib/webpoke.rb, line 96
def config (&block)
  $config = Webpoke::Config.new(&block)
end
gauge_success(test) click to toggle source
# File lib/webpoke.rb, line 140
def gauge_success(test)
  begin
    success = run_test test
  rescue Webpoke::TestHTTPError => e
    log 'FAIL:'.red
    log e
    $errors += 1;
  rescue Webpoke::TestParseError => e
    log "Parsing failure: ".red
    log "\t#{e}"
    log "Data:"
    log "\t"+e.object
    $errors += 1;
  rescue Webpoke::TestSuccessError => e
    log "\nError while executing success for test".red
    log e
    log e.backtrace.join "\n"
  end
  
  if (success)
    $successes +=1
    log "OK!".green
    test.on_success.each do |dependent|
      dt = dependent.call()
      gauge_success dt
    end
  else
    $errors += 1
    log "FAIL!".red
  end
  return success
  
end
group(groupName, description: nil) click to toggle source
# File lib/webpoke.rb, line 58
def group (groupName, description: nil)
  $groups[groupName] = {
    name: groupName,
    description: description,
    tests: []
  }
end
log(message, newLine=true) click to toggle source
# File lib/webpoke.rb, line 190
def log (message, newLine=true)
  return true if $config.format != 'stdout'
  $stdout << message
  $stdout << "\n" if newLine
end
run(group=nil) click to toggle source
# File lib/webpoke.rb, line 175
def run (group=nil)
  
  $tests.each do |test|
    next if group && test.group != group && !test.run_always
    
    next if test.dependant?
    next if test.did_run?
    
    gauge_success(test)
    
  end
    
end
run_test(test) click to toggle source
# File lib/webpoke.rb, line 100
def run_test(test)
  
  $tested +=1
  fqu = if test.url.match(/^https?:\/\//i) then test.url; else $config.base+test.url; end
  args = args_for_test(test)
  
  log "#{$tested}: #{test.description}".bold
  log "#{test.method.upcase}: #{test.url}...", false
  $config.beforeSend(test.method, fqu, args) if $config.beforeSend
  
  begin
    r = HTTParty.send(test.method, fqu, args)
  rescue Interrupt
    puts Webpoke.results
    exit!
  rescue Exception => e
    raise Webpoke::TestHTTPError.new(e.message, e)
  end
  
  body = r.body
  begin
    if test.should_parse? && $config.parse[:output] && body.is_a?(String)
      body = $config.parse[:output].call(r.body)
    end
  rescue Exception => e
    raise Webpoke::TestParseError.new(e.message, body)
  end
  
  
  if test.passed?(r.code, body)
    true
  else
    if $config.on_failure
      log $config.on_failure.call(r.code, body)
    end
    false
  end
  
end
test(&block) click to toggle source

Adds a test to the queue @param block [Proc] the configuration for this test

# File lib/webpoke.rb, line 51
def test(&block)
  test = Test.new(&block)
  $tests << test
  return test
end