Class: AntiSmoker::HttpTest

Inherits:
AbstractSmokeTest show all
Defined in:
lib/antismoker/tests/http.rb

Instance Attribute Summary collapse

Attributes inherited from AbstractSmokeTest

#count, #delay, #host, #logger, #options, #period, #port, #timeout

Instance Method Summary collapse

Methods inherited from AbstractSmokeTest

#run, #run_once_with_timeout, #sleep_with_progress

Constructor Details

#initialize(host, port, options = {}) ⇒ HttpTest

Returns a new instance of HttpTest.



9
10
11
12
13
14
15
# File 'lib/antismoker/tests/http.rb', line 9

def initialize(host, port, options={})
  super
  @data = options.fetch(:data, {})
  @method = options.fetch(:method, "GET")
  @path = options.fetch(:path, "/")
  @ok = options.fetch(:ok, 200)
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/antismoker/tests/http.rb', line 16

def data
  @data
end

#methodObject (readonly)

Returns the value of attribute method.



17
18
19
# File 'lib/antismoker/tests/http.rb', line 17

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



18
19
20
# File 'lib/antismoker/tests/http.rb', line 18

def path
  @path
end

Instance Method Details

#fetch(uri, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/antismoker/tests/http.rb', line 26

def fetch(uri, options={})
  limit = options.fetch(:limit, 10)
  method = options.fetch(:method, "GET")
  raise(ArgumentError.new("HTTP redirect too deep")) if limit <= 0
  case method
  when /^post$/i
    response = Net::HTTP.post_form(uri, data)
  else
    response = Net::HTTP.get_response(uri)
  end
  if Net::HTTPRedirection === response
    location = URI.parse(response["location"])
    new_uri = (location.absolute?) ? location : uri.merge(location)
    fetch(new_uri, :limit => limit-1)
  else
    response
  end
end

#response_ok(response) ⇒ Object



45
46
47
48
# File 'lib/antismoker/tests/http.rb', line 45

def response_ok(response)
  code = response.code.to_i
  [ @ok ].flatten.compact.map { |x| x === code }.any?
end

#run_once(options = {}) ⇒ Object



20
21
22
23
24
# File 'lib/antismoker/tests/http.rb', line 20

def run_once(options={})
  response = fetch(uri, :method => method)
  logger.debug("HTTP response: #{self} => #{response.code} (#{response.body.length} bytes)")
  response_ok(response)
end

#to_sObject



54
55
56
# File 'lib/antismoker/tests/http.rb', line 54

def to_s
  "#{method} #{uri}"
end

#uriObject



50
51
52
# File 'lib/antismoker/tests/http.rb', line 50

def uri
  URI::HTTP.build(:host => host, :port => port, :path => path)
end