class Serverspec::Type::Http_Get
Perform an HTTP GET request against the serverspec target using {www.rubydoc.info/gems/faraday/ Faraday}.
Public Class Methods
Initialize a bunch of instance variables, then call {#getpage}
Calls {#getpage} within a {Timeout::timeout} block; If {#getpage} times out, set “timed_out_status“ to [True]
@param port [Int] the port to connect to HTTP over @param host_header [String] the value to set in the 'Host' HTTP request header @param path [String] the URI/path to request from the server @param timeout_sec [Int] how many seconds to allow request to run before timing out and setting @timed_out_status to True @param protocol [String] the protocol to connect to the server (e.g. 'http', 'https') @param bypass_ssl_verify [Boolean] if true, SSL verification will be bypassed (useful for self-signed certificates)
@example
describe http_get(80, 'myhostname', '/') do # tests here end
@api public @return [nil]
# File lib/serverspec_extended_types/http_get.rb, line 45 def initialize(port, host_header, path, timeout_sec=10, protocol='http', bypass_ssl_verify=false) @ip = ENV['TARGET_HOST'] @port = port @host = host_header @path = path @timed_out_status = false @content_str = nil @headers_hash = nil @response_code_int = nil @response_json = nil @protocol = protocol @bypass_ssl_verify = bypass_ssl_verify @redirects = false @redirect_path = nil begin Timeout::timeout(timeout_sec) do getpage end rescue Timeout::Error @timed_out_status = true end end
Public Instance Methods
Returns the body/content of the HTTP response
@example
describe http_get(80, 'myhostname', '/') do its(:body) { should match /<html>/ } end
@api public @return [String]
# File lib/serverspec_extended_types/http_get.rb, line 166 def body @content_str end
Returns the HTTP response headers as a hash
@example
describe http_get(80, 'myhostname', '/') do its(:headers) { should include('HeaderName' => /value regex/) } end
@api public @return [Hash]
# File lib/serverspec_extended_types/http_get.rb, line 123 def headers @headers_hash end
Returns the decoded JSON response, or an empty hash
@example
describe http_get(80, 'myhostname', '/') do its(:json) { should include('foo' => /value regex/) } end
@api public @return [Hash]
# File lib/serverspec_extended_types/http_get.rb, line 136 def json @response_json end
Whether or not it redirects to any other page
@example
describe http_get(80, 'myhostname', '/') do it { should be_redirected } end
@api public @return [Boolean]
# File lib/serverspec_extended_types/http_get.rb, line 192 def redirected? @redirects end
Whether or not it redirects to some other page
@example
describe http_get(80, 'myhostname', '/') do it { should be_redirected_to 'https://myhostname/' } end
@api public @return [Boolean]
# File lib/serverspec_extended_types/http_get.rb, line 179 def redirected_to? (redirect_path) @redirects and @redirect_path == redirect_path end
Returns the HTTP status code, or 0 if timed out
@example
describe http_get(80, 'myhostname', '/') do its(:status) { should eq 200 } end
@api public @return [Int]
# File lib/serverspec_extended_types/http_get.rb, line 149 def status if @timed_out_status 0 else @response_code_int end end
Whether or not the request timed out
@example
describe http_get(80, 'myhostname', '/') do it { should_not be_timed_out } end
@api public @return [Boolean]
# File lib/serverspec_extended_types/http_get.rb, line 110 def timed_out? @timed_out_status end
Private Instance Methods
Private method to actually get the page; must be called within a timeout block
Gets the page using {www.rubydoc.info/gems/faraday/ Faraday} and then sets instance variables for the various attribute readers.
@api private @return [nil]
# File lib/serverspec_extended_types/http_get.rb, line 74 def getpage ip = @ip port = @port protocol = @protocol options = [] options << { ssl: { verify: false } } if @bypass_ssl_verify conn = Faraday.new("#{protocol}://#{ip}:#{port}/", *options) version = ServerspecExtendedTypes::VERSION conn.headers[:user_agent] = "Serverspec::Type::Http_Get/#{version} (https://github.com/jantman/serverspec-extended-types)" conn.headers[:Host] = @host response = conn.get(@path) @response_code_int = response.status @content_str = response.body @headers_hash = Hash.new('') response.headers.each do |header, val| @headers_hash[header] = val end @redirects = @@redirect_codes.include? @response_code_int @redirect_path = @redirects ? @headers_hash['location'] : nil # try to JSON decode begin @response_json = JSON.parse(@content_str) rescue @response_json = {} end end