Module: Webpoke

Defined in:
lib/webpoke.rb,
lib/Webpoke/version.rb

Defined Under Namespace

Classes: Config, ConfigError, Test, TestError, TestHTTPError, TestParseError, TestSuccessError

Constant Summary collapse

VERSION =
"0.5.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.document(group) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# 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

.failedObject



245
246
247
# File 'lib/webpoke.rb', line 245

def Webpoke.failed
  return $errors
end

.formatsObject



253
254
255
# File 'lib/webpoke.rb', line 253

def Webpoke.formats
  return ['json', 'html', 'stdout']
end

.resultsObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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

.successObject



241
242
243
# File 'lib/webpoke.rb', line 241

def Webpoke.success
  return $successes
end

.testedObject



249
250
251
# File 'lib/webpoke.rb', line 249

def Webpoke.tested
  return $tested
end

Instance Method Details

#args_for_test(test) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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) ⇒ Object

Configures the poking (see #Webpoke::Config)

Parameters:

  • &block (Block)

    the configuration options



96
97
98
# File 'lib/webpoke.rb', line 96

def config (&block)
  $config = Webpoke::Config.new(&block)
end

#gauge_success(test) ⇒ Object



140
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
# 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) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/webpoke.rb', line 58

def group (groupName, description: nil)
  $groups[groupName] = {
    name: groupName,
    description: description,
    tests: []
  }
end

#log(message, newLine = true) ⇒ Object



190
191
192
193
194
# 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) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# 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) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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) ⇒ Object

Adds a test to the queue

Parameters:

  • block (Proc)

    the configuration for this test



51
52
53
54
55
# File 'lib/webpoke.rb', line 51

def test(&block)
  test = Test.new(&block)
  $tests << test
  return test
end