Class: Testotron::Tests::HTTP

Inherits:
Testotron::Test show all
Defined in:
lib/tests/http.rb

Constant Summary collapse

KEY =
"http"

Instance Method Summary collapse

Constructor Details

#initialize(host, options = {}) ⇒ HTTP

Returns a new instance of HTTP.



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

def initialize(host, options = {})
	@host = host
	@port = options[:port] || 80
	@requests = [*(options[:requests] || "http://#{host}")]
	@grep = options[:grep]
end

Instance Method Details

#human_nameObject



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

def human_name
	"HTTP test of #{@host}, port #{@port}, requests #{@requests.join ','}"
end

#run(runner) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tests/http.rb', line 19

def run(runner)
	runner.report self, "Testing HTTP server on #{@host} port #{@port}..."
	http = Net::HTTP.new(@host, @port)
	http.read_timeout = 2
	http.open_timeout = 2
	@requests.each do |page|
		runner.report self, "Trying #{page}..."
		request = Net::HTTP::Get.new URI.parse(page).request_uri

		begin
			response = http.request(request)
		rescue Timeout::Error
			raise TestFailed, "HTTP connection timed out (Timeout::Error)"
		rescue Errno::ETIMEDOUT
			raise TestFailed, "HTTP connection timed out (ETIMEDOUT)"
		rescue Errno::ECONNREFUSED
			raise TestFailed, "HTTP connection refused (ECONNREFUSED)"
		rescue SocketError
			raise TestFailed, "HTTP connection failed (SocketError)"
		end

		good_codes = 100...400
		code = response.code.to_i
		unless good_codes.include? code
			raise TestFailed, "Response code #{code} on #{@post}:#{@port} GET #{page}"
		end

		if @grep
			unless response.body.include? @grep
				raise TestFailed, "Response on GET #{page} doesn't match '#@grep'"
			end
		end
	end
end