Class: Serverspec::Type::Http_Get
- Inherits:
-
Base
- Object
- Base
- Serverspec::Type::Http_Get
- 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
-
#body ⇒ String
Returns the body/content of the HTTP response.
-
#getpage ⇒ nil
private
private
Private method to actually get the page; must be called within a timeout block.
-
#headers ⇒ Hash
Returns the HTTP response headers as a hash.
-
#initialize(port, host_header, path, timeout_sec = 10, protocol = 'http', bypass_ssl_verify = false) ⇒ nil
constructor
Initialize a bunch of instance variables, then call #getpage.
-
#json ⇒ Hash
Returns the decoded JSON response, or an empty hash.
-
#redirected? ⇒ Boolean
Whether or not it redirects to any other page.
-
#redirected_to?(redirect_path) ⇒ Boolean
Whether or not it redirects to some other page.
-
#status ⇒ Int
Returns the HTTP status code, or 0 if timed out.
-
#timed_out? ⇒ Boolean
Whether or not the request timed out.
Constructor Details
#initialize(port, host_header, path, timeout_sec = 10, protocol = 'http', bypass_ssl_verify = false) ⇒ nil
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
#body ⇒ String
Returns the body/content of the HTTP response
166 167 168 |
# File 'lib/serverspec_extended_types/http_get.rb', line 166 def body @content_str end |
#getpage ⇒ nil (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.
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 = [] << { ssl: { verify: false } } if @bypass_ssl_verify conn = Faraday.new("#{protocol}://#{ip}:#{port}/", *) 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 |
#headers ⇒ Hash
Returns the HTTP response headers as a hash
123 124 125 |
# File 'lib/serverspec_extended_types/http_get.rb', line 123 def headers @headers_hash end |
#json ⇒ Hash
Returns the decoded JSON response, or an empty 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
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
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 |
#status ⇒ Int
Returns the HTTP status code, or 0 if timed out
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
110 111 112 |
# File 'lib/serverspec_extended_types/http_get.rb', line 110 def timed_out? @timed_out_status end |