From 9490880f99de4f29609757805e84400189dcf837 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 23 Nov 2012 21:49:24 +0000 Subject: Added simple implementation - no test cases yet, no real testing carried out. --- lib/custodian/protocoltest/http.rb | 156 ++++++++++++++++++++++++++++++++++++- 1 file changed, 152 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/custodian/protocoltest/http.rb b/lib/custodian/protocoltest/http.rb index 133fdaf..44f76a6 100644 --- a/lib/custodian/protocoltest/http.rb +++ b/lib/custodian/protocoltest/http.rb @@ -1,3 +1,9 @@ +require 'openssl' +require 'socket' +require 'timeout' +require 'uri' + + # # The HTTP-protocol test. @@ -21,12 +27,24 @@ module Custodian # attr_reader :line - # # The URL to poll # attr_reader :url + # + # The expected status + content + # + attr_reader :expected_status, :expected_content + + + # + # The actual status & content + # + attr_reader :status, :content + + + # # Constructor # @@ -42,7 +60,6 @@ module Custodian # @url = line.split( /\s+/)[0] - if ( @url !~ /^https?:/ ) raise ArgumentError, "The target wasn't an URL" end @@ -56,6 +73,27 @@ module Custodian @inverted = false end + # + # Expected status + # + if ( line =~ /with status ([0-9]+)/ ) + @expected_status = $1.dup + else + @expected_status = "200" + end + + # + # The content we expect to find + # + if ( line =~ /with content '([^']+)'/ ) + @expected_content = $1.dup + else + @expected_content = nil + end + + @status = nil + @content = nil + end @@ -74,12 +112,122 @@ module Custodian # Run the test. # def run_test - @error = "Not implemented" - false + + # Reset state, in case we've previously run. + @error = nil + + # Parse the URL + uri = URI.parse(@url) + + # + # Ensure we have a path to request - to cover people who write: + # + # http://example.com must run http .. + # + if ( uri.path.empty? ) + uri.path = "/" + end + + # + # Connect a socket to the host. + # + socket = connect( uri.host, uri.port, uri.scheme == "https" ) + + path = uri.path + if ( uri.query ) + path = "#{url.path}?#{url.query}" + end + + req =< err + @error = err + ensure + socket.close if socket.is_a?(Socket) and not socket.closed? + end + + end + + # # If the test fails then report the error. -- cgit v1.2.1