Fix http with connection error (#2770)

* fix: http resource handle connection failed (ex. port is not open)
* add test case

Signed-off-by: Wing924 <weihe924stephen@gmail.com>
This commit is contained in:
Wei He 2018-03-08 13:04:26 +09:00 committed by Jared Quick
parent 18675ae326
commit a3898db2fe
2 changed files with 27 additions and 2 deletions

View file

@ -176,9 +176,10 @@ module Inspec::Resources
def run_curl
return if @ran_curl
response = inspec.command(curl_command).stdout
cmd_result = inspec.command(curl_command)
response = cmd_result.stdout
@ran_curl = true
return if response.nil?
return if response.nil? || cmd_result.exit_status != 0
# strip any carriage returns to normalize output
response.delete!("\r")

View file

@ -160,6 +160,30 @@ describe 'Inspec::Resources::Http' do
_(worker.response_headers['Access-Control-Max-Age']).must_equal '86400'
end
end
describe 'run_curl request' do
it 'returns nil when nil is returned' do
Inspec::Resources::Cmd.any_instance
.stubs(:stdout)
.returns(nil)
_(worker.send(:run_curl)).must_be_nil
end
it 'returns nil when failure is returned' do
Inspec::Resources::Cmd.any_instance
.stubs(:exit_status)
.returns(1)
_(worker.send(:run_curl)).must_be_nil
end
it 'returns html when html is returned' do
Inspec::Resources::Cmd.any_instance
.stubs(:stdout)
.returns("HTTP/1.1 200 OK\nDate: Tue, 03 Oct 2017 20:30:08 GMT\nExpires: -1\nCache-Control: private")
assert = ["Date: Tue, 03 Oct 2017 20:30:08 GMT", "Expires: -1", "Cache-Control: private"]
_(worker.send(:run_curl)).must_equal assert
end
end
end
describe 'Inspec::Resource::Http::Headers' do