Class: Serverspec::Type::Http_Get

Inherits:
Base
  • Object
show all
Defined in:
lib/serverspec_extended_types/http_get.rb

Overview

Perform an HTTP GET request against the serverspec target using / Faraday.

Constant Summary collapse

@@redirect_codes =

Http status codes that are tested against for redirect test

[301, 302, 307, 308]

Instance Method Summary collapse

Constructor Details

#initialize(port, host_header, path, timeout_sec = 10, protocol = 'http', bypass_ssl_verify = false) ⇒ nil

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]

Examples:

describe http_get(80, 'myhostname', '/') do
  # tests here
end

Parameters:

  • port (Int)

    the port to connect to HTTP over

  • host_header (String)

    the value to set in the ‘Host’ HTTP request header

  • path (String)

    the URI/path to request from the server

  • timeout_sec (Int) (defaults to: 10)

    how many seconds to allow request to run before timing out and setting @timed_out_status to True

  • protocol (String) (defaults to: 'http')

    the protocol to connect to the server (e.g. ‘http’, ‘https’)

  • bypass_ssl_verify (Boolean) (defaults to: false)

    if true, SSL verification will be bypassed (useful for self-signed certificates)



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

Instance Method Details

#bodyString

Returns the body/content of the HTTP response

Examples:

describe http_get(80, 'myhostname', '/') do
  its(:body) { should match /<html>/ }
end

Returns:

  • (String)


166
167
168
# File 'lib/serverspec_extended_types/http_get.rb', line 166

def body
  @content_str
end

#getpagenil (private)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Private method to actually get the page; must be called within a timeout block

Gets the page using / Faraday and then sets instance variables for the various attribute readers.

Returns:

  • (nil)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# 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

#headersHash

Returns the HTTP response headers as a hash

Examples:

describe http_get(80, 'myhostname', '/') do
  its(:headers) { should include('HeaderName' => /value regex/) }
end

Returns:

  • (Hash)


123
124
125
# File 'lib/serverspec_extended_types/http_get.rb', line 123

def headers
  @headers_hash
end

#jsonHash

Returns the decoded JSON response, or an empty hash

Examples:

describe http_get(80, 'myhostname', '/') do
  its(:json) { should include('foo' => /value regex/) }
end

Returns:

  • (Hash)


136
137
138
# File 'lib/serverspec_extended_types/http_get.rb', line 136

def json
  @response_json
end

#redirected?Boolean

Whether or not it redirects to any other page

Examples:

describe http_get(80, 'myhostname', '/') do
  it { should be_redirected }
end

Returns:

  • (Boolean)


192
193
194
# File 'lib/serverspec_extended_types/http_get.rb', line 192

def redirected?
  @redirects
end

#redirected_to?(redirect_path) ⇒ Boolean

Whether or not it redirects to some other page

Examples:

describe http_get(80, 'myhostname', '/') do
  it { should be_redirected_to 'https://myhostname/' }
end

Returns:

  • (Boolean)


179
180
181
# File 'lib/serverspec_extended_types/http_get.rb', line 179

def redirected_to? (redirect_path)
  @redirects and @redirect_path == redirect_path
end

#statusInt

Returns the HTTP status code, or 0 if timed out

Examples:

describe http_get(80, 'myhostname', '/') do
  its(:status) { should eq 200 }
end

Returns:

  • (Int)


149
150
151
152
153
154
155
# File 'lib/serverspec_extended_types/http_get.rb', line 149

def status
  if @timed_out_status
    0
  else
    @response_code_int
  end
end

#timed_out?Boolean

Whether or not the request timed out

Examples:

describe http_get(80, 'myhostname', '/') do
  it { should_not be_timed_out }
end

Returns:

  • (Boolean)


110
111
112
# File 'lib/serverspec_extended_types/http_get.rb', line 110

def timed_out?
  @timed_out_status
end