2017-01-05 19:29:11 +00:00
|
|
|
---
|
|
|
|
title: About the http Resource
|
2018-02-16 00:28:15 +00:00
|
|
|
platform: linux
|
2017-01-05 19:29:11 +00:00
|
|
|
---
|
|
|
|
|
|
|
|
# http
|
|
|
|
|
|
|
|
Use the `http` InSpec audit resource to test an http endpoint.
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
|
|
|
|
2017-01-05 19:29:11 +00:00
|
|
|
## Syntax
|
|
|
|
|
|
|
|
An `http` resource block declares the configuration settings to be tested:
|
|
|
|
|
2017-05-30 16:33:59 +00:00
|
|
|
describe http('url', auth: {user: 'user', pass: 'test'}, params: {params}, method: 'method', headers: {headers}, data: data, open_timeout: 60, read_timeout: 60, ssl_verify: true) do
|
2017-01-05 19:29:11 +00:00
|
|
|
its('status') { should eq number }
|
|
|
|
its('body') { should eq 'body' }
|
2017-01-26 13:18:49 +00:00
|
|
|
its('headers.name') { should eq 'header' }
|
2017-01-05 19:29:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
where
|
|
|
|
|
|
|
|
* `('url')` is the url to test
|
2017-05-30 16:33:59 +00:00
|
|
|
* `auth: { user: 'user', pass: 'test' }` may be specified for basic auth request
|
2017-01-05 19:29:11 +00:00
|
|
|
* `{params}` may be specified for http request parameters
|
|
|
|
* `'method'` may be specified for http request method (default to 'GET')
|
|
|
|
* `{headers}` may be specified for http request headers
|
2017-05-30 16:33:59 +00:00
|
|
|
* `data` may be specified for http request body
|
|
|
|
* `open_timeout` may be specified for a timeout for opening connections (default to 60)
|
|
|
|
* `read_timeout` may be specified for a timeout for reading connections (default to 60)
|
|
|
|
* `ssl_verify` may be specified to enable or disable verification of SSL certificates (default to `true`)
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
## Example
|
|
|
|
|
|
|
|
The following examples show how to use this InSpec audit resource. An `http` resource block declares the configuration settings to be tested:
|
|
|
|
|
|
|
|
### Simple http test
|
|
|
|
|
|
|
|
For example, a service is listening on default http port can be tested like this:
|
|
|
|
|
|
|
|
describe http('http://localhost') do
|
|
|
|
its('status') { should cmp 200 }
|
|
|
|
end
|
|
|
|
|
|
|
|
### Complex http test
|
|
|
|
|
|
|
|
describe http('http://localhost:8080/ping',
|
|
|
|
auth: {user: 'user', pass: 'test'},
|
|
|
|
params: {format: 'html'},
|
|
|
|
method: 'POST',
|
|
|
|
headers: {'Content-Type' => 'application/json'},
|
|
|
|
data: '{"data":{"a":"1","b":"five"}}') do
|
|
|
|
its('status') { should cmp 200 }
|
|
|
|
its('body') { should cmp 'pong' }
|
|
|
|
its('headers.Content-Type') { should cmp 'text/html' }
|
|
|
|
end
|
|
|
|
|
|
|
|
<br>
|
|
|
|
|
2018-02-16 02:34:11 +00:00
|
|
|
## Local vs. Remote
|
|
|
|
|
|
|
|
Beginning with InSpec 1.41, you can enable the ability to have the HTTP test execute on the remote target:
|
|
|
|
|
|
|
|
describe http('http://www.example.com', enable_remote_worker: true) do
|
|
|
|
its('body') { should cmp 'awesome' }
|
|
|
|
end
|
|
|
|
|
|
|
|
In InSpec 2.0, the HTTP test will automatically execute remotely whenever InSpec is testing a remote node.
|
|
|
|
|
2018-02-08 21:41:14 +00:00
|
|
|
<br>
|
2018-02-16 02:34:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
## Parameters
|
2018-02-08 21:41:14 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
* `url`, `auth`, `params`, `method`, `headers`, `data`, `open_timeout`, `read_timeout`, `ssl_verify`
|
2018-02-08 21:41:14 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
## Parameter Examples
|
2018-02-16 02:34:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
### url
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
`('url')` is the url to test.
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
describe http('http://localhost:8080/ping') do
|
|
|
|
...
|
|
|
|
end
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
### auth
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
`auth: { user: 'user', pass: 'test' }` may be specified for basic auth request.
|
|
|
|
|
|
|
|
describe http('http://localhost:8080/ping',
|
|
|
|
auth: {user: 'user', pass: 'test'}) do
|
|
|
|
...
|
2017-10-03 21:35:10 +00:00
|
|
|
end
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
### params
|
|
|
|
|
|
|
|
`{params}` may be specified for http request parameters.
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
describe http('http://localhost:8080/ping',
|
2018-02-23 21:49:22 +00:00
|
|
|
params: {format: 'html'}) do
|
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
### method
|
|
|
|
|
|
|
|
`'method'` may be specified for http request method (default to 'GET').
|
|
|
|
|
|
|
|
describe http('http://localhost:8080/ping',
|
|
|
|
method: 'POST') do
|
|
|
|
...
|
|
|
|
end
|
|
|
|
|
2018-05-16 12:53:37 +00:00
|
|
|
### headers
|
2018-02-23 21:49:22 +00:00
|
|
|
|
|
|
|
`{headers}` may be specified for http request headers.
|
|
|
|
|
|
|
|
describe http('http://localhost:8080/ping',
|
|
|
|
headers: {'Content-Type' => 'application/json'}) do
|
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
### data
|
|
|
|
|
|
|
|
`data` may be specified for http request body.
|
|
|
|
|
|
|
|
describe http('http://localhost:8080/ping',
|
2017-10-03 21:35:10 +00:00
|
|
|
data: '{"data":{"a":"1","b":"five"}}') do
|
2018-02-23 21:49:22 +00:00
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
### open_timeout
|
|
|
|
|
|
|
|
`open_timeout` may be specified for a timeout for opening connections (default to 60).
|
|
|
|
|
2018-05-16 12:53:37 +00:00
|
|
|
describe('http://localhost:8080/ping',
|
2018-02-23 21:49:22 +00:00
|
|
|
open_timeout: '90') do
|
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
### read_timeout
|
|
|
|
|
|
|
|
`read_timeout` may be specified for a timeout for reading connections (default to 60).
|
|
|
|
|
2018-05-16 12:53:37 +00:00
|
|
|
describe('http://localhost:8080/ping',
|
2018-02-23 21:49:22 +00:00
|
|
|
read_timeout: '90') do
|
|
|
|
...
|
2017-10-03 21:35:10 +00:00
|
|
|
end
|
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
### ssl_verify
|
|
|
|
|
|
|
|
`ssl_verify` may be specified to enable or disable verification of SSL certificates (default to `true`).
|
|
|
|
|
2018-05-16 12:53:37 +00:00
|
|
|
describe('http://localhost:8080/ping',
|
|
|
|
ssl_verify: true) do
|
2018-02-23 21:49:22 +00:00
|
|
|
...
|
|
|
|
end
|
|
|
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
## Properties
|
|
|
|
|
|
|
|
* `body`, `headers`, `http_method`, `status`,
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
<br>
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2018-02-23 21:49:22 +00:00
|
|
|
## Property Examples
|
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
### body
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2017-10-03 21:35:10 +00:00
|
|
|
The `body` matcher tests body content of http response:
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2018-06-06 18:10:48 +00:00
|
|
|
its('body') { should eq 'hello\n' }
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2017-01-26 13:18:49 +00:00
|
|
|
### headers
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2017-01-26 13:18:49 +00:00
|
|
|
The `headers` matcher returns an hash of all http headers:
|
2017-01-05 19:29:11 +00:00
|
|
|
|
2017-01-26 13:18:49 +00:00
|
|
|
its('headers') { should eq {} }
|
|
|
|
|
|
|
|
Individual headers can be tested via:
|
|
|
|
|
|
|
|
its('headers.Content-Type') { should cmp 'text/html' }
|
2017-01-05 19:29:11 +00:00
|
|
|
|
|
|
|
### status
|
|
|
|
|
|
|
|
The `status` matcher tests status of the http response:
|
|
|
|
|
|
|
|
its('status') { should eq 200 }
|
2018-02-23 21:49:22 +00:00
|
|
|
|
|
|
|
<br>
|
|
|
|
|
|
|
|
## Matchers
|
|
|
|
|
2018-05-16 12:53:37 +00:00
|
|
|
For a full list of available matchers, please visit our [matchers page](https://www.inspec.io/docs/reference/matchers/).
|