2017-01-05 19:29:11 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2017, Criteo
|
|
|
|
# author: Guilhem Lettron
|
|
|
|
# license: Apache v2
|
|
|
|
|
|
|
|
require 'http'
|
2017-01-26 13:18:49 +00:00
|
|
|
require 'hashie'
|
2017-01-05 19:29:11 +00:00
|
|
|
|
|
|
|
module Inspec::Resources
|
|
|
|
class Http < Inspec.resource(1)
|
|
|
|
name 'http'
|
|
|
|
desc 'Use the http InSpec audit resource to test http call.'
|
|
|
|
example "
|
|
|
|
describe http('http://localhost:8080/ping', auth: {user: 'user', pass: 'test'}, params: {format: 'html'}) do
|
|
|
|
its('status') { should cmp 200 }
|
|
|
|
its('body') { should cmp 'pong' }
|
2017-01-26 13:18:49 +00:00
|
|
|
its('headers.Content-Type') { should cmp 'text/html' }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe http('http://example.com/ping').headers do
|
|
|
|
its('Content-Length') { should cmp 258 }
|
|
|
|
its('Content-Type') { should cmp 'text/html; charset=UTF-8' }
|
2017-01-05 19:29:11 +00:00
|
|
|
end
|
|
|
|
"
|
|
|
|
|
|
|
|
# rubocop:disable ParameterLists
|
|
|
|
def initialize(url, method: 'GET', params: nil, auth: {}, headers: {}, data: nil)
|
|
|
|
@url = url
|
|
|
|
@method = method
|
|
|
|
@params = params
|
|
|
|
@auth = auth
|
|
|
|
@headers = headers
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
|
|
|
|
def status
|
|
|
|
response.status
|
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
response.to_s
|
|
|
|
end
|
|
|
|
|
2017-01-26 13:18:49 +00:00
|
|
|
def headers
|
|
|
|
Hashie::Mash.new(response.headers.to_h)
|
2017-01-05 19:29:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2017-01-26 13:18:49 +00:00
|
|
|
"http #{@method} on #{@url}"
|
2017-01-05 19:29:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def response
|
|
|
|
http = HTTP.headers(@headers)
|
|
|
|
http = http.basic_auth(@auth) unless @auth.empty?
|
|
|
|
@response ||= http.request(@method, @url, { body: @data, params: @params })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|