Class: AntiSmoker::HttpTest
- Inherits:
-
AbstractSmokeTest
- Object
- AbstractSmokeTest
- AntiSmoker::HttpTest
- Defined in:
- lib/antismoker/tests/http.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Attributes inherited from AbstractSmokeTest
#count, #delay, #host, #logger, #options, #period, #port, #timeout
Instance Method Summary collapse
- #fetch(uri, options = {}) ⇒ Object
-
#initialize(host, port, options = {}) ⇒ HttpTest
constructor
A new instance of HttpTest.
- #response_ok(response) ⇒ Object
- #run_once(options = {}) ⇒ Object
- #to_s ⇒ Object
- #uri ⇒ Object
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, ={}) super @data = .fetch(:data, {}) @method = .fetch(:method, "GET") @path = .fetch(:path, "/") @ok = .fetch(:ok, 200) end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
16 17 18 |
# File 'lib/antismoker/tests/http.rb', line 16 def data @data end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
17 18 19 |
# File 'lib/antismoker/tests/http.rb', line 17 def method @method end |
#path ⇒ Object (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
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, ={}) limit = .fetch(:limit, 10) method = .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(={}) response = fetch(uri, :method => method) logger.debug("HTTP response: #{self} => #{response.code} (#{response.body.length} bytes)") response_ok(response) end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/antismoker/tests/http.rb', line 54 def to_s "#{method} #{uri}" end |
#uri ⇒ Object
50 51 52 |
# File 'lib/antismoker/tests/http.rb', line 50 def uri URI::HTTP.build(:host => host, :port => port, :path => path) end |