Add basic param handling to remote HTTP worker (#2286)

http resource: Add basic param handling to remote HTTP worker
This commit is contained in:
Seth Chisamore 2017-11-16 12:16:23 -05:00 committed by Adam Leff
parent 6ccc8aad26
commit 396752ba26
4 changed files with 32 additions and 3 deletions

View file

@ -165,7 +165,7 @@ module Inspec::Resources
def body
run_curl
@body
@body&.strip
end
def response_headers
@ -202,7 +202,7 @@ module Inspec::Resources
end
end
def curl_command
def curl_command # rubocop:disable Metrics/AbcSize
cmd = ["curl -i -X #{http_method}"]
cmd << "--connect-timeout #{open_timeout}"
cmd << "--max-time #{open_timeout+read_timeout}"
@ -214,7 +214,11 @@ module Inspec::Resources
cmd << "-H '#{k}: #{v}'"
end
cmd << "'#{url}'"
if params.nil?
cmd << "'#{url}'"
else
cmd << "'#{url}?#{params.map { |e| e.join('=') }.join('&')}'"
end
cmd.join(' ')
end

View file

@ -458,6 +458,7 @@ class MockLoader
"curl -i -X GET --connect-timeout 60 --max-time 120 --user 'user:pass' 'http://www.example.com'" => cmd.call('http-remote-basic-auth'),
'f77ebcedaf6fbe8f02d2f9d4735a90c12311d2ca4b43ece9efa2f2e396491747' => cmd.call('http-remote-post'),
"curl -i -X GET --connect-timeout 60 --max-time 120 -H 'accept: application/json' -H 'foo: bar' 'http://www.example.com'" => cmd.call('http-remote-headers'),
"curl -i -X GET --connect-timeout 60 --max-time 120 'http://www.example.com?a=b&c=d'" => cmd.call('http-remote-params'),
# elasticsearch resource
"curl -H 'Content-Type: application/json' http://localhost:9200/_nodes" => cmd.call('elasticsearch-cluster-nodes-default'),

View file

@ -0,0 +1,15 @@
HTTP/1.1 200 OK
Date: Tue, 03 Oct 2017 20:30:08 GMT
Expires: -1
Cache-Control: private, max-age=0
Content-Type: text/html; charset=ISO-8859-1
P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
Server: gws
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Set-Cookie: NID=113=kNR6MIUK3vNjVH3KvQqIjfnDLLPHJ96wmC_z643weEFQ6Cfq0B2iUYqxzQk5pKVoAkbL8ZxKFvvM9v55qiNFXH_O655WuuUxPshmlSIM5xpCSH0xy09SnIQJVi0l7eKY; expires=Wed, 04-Apr-2018 20:30:08 GMT; path=/; domain=.google.com; HttpOnly
Accept-Ranges: none
Vary: Accept-Encoding
Transfer-Encoding: chunked
params ok

View file

@ -109,5 +109,14 @@ describe 'Inspec::Resources::Http' do
_(worker.response_headers['mock']).must_equal 'ok'
end
end
describe 'with params' do
let(:opts) { { params: { a: 'b', c: 'd' } } }
it 'returns correct data' do
_(worker.status).must_equal 200
_(worker.body).must_equal 'params ok'
end
end
end
end