fettle_checks v0.2.2 Fettle.HttpChecker View Source
A Fettle.Checker
that is healthy depending on the result of an HTTP request.
Implements the Fettle.Checker
behaviour.
Configure in Fettle health-check config as e.g.:
{
%{
name: "my-service-check",
panic_guide_url: "...",
...
checker: Fettle.HttpChecker,
args: [url: "http://my-service.com/endpoint", method: "POST", req_body: body(), status_code: 200..202, resp_body: ~r/.*xy??y.*/]
},
}
Options
Key | Type | Description | Required/default |
---|---|---|---|
url | String.t | URL to call | required |
headers | {String.t, String.t} | Headers to pass to request | [] |
method | String.t | HTTP method | "GET" |
req_body | String.t | Body to send with POST, PUT | "" |
httpoison | Keyword.t | Additional options for HTTPoison.request/5 | [] |
status_code | non_neg_integer | Range.t | [non_neg_integer | Range.t] | Status code to match | 200 |
resp_body | any | Expected response body | false (don’t care) |
Specifying the expected response body
The supported values for resp_body
are:
String
- exact value of body (comparison viaString.equivalent?/2
).Regex
- a regex to use to match the body.function/3
- called passing content-type header, body and options map; returningFettle.Checker.Result
.{module, function}
- called passing content-type header, body and options map; returningFettle.Checker.Result
.
Simple customization can be performed by using the function or module resp_body
options.
Customizing via Fettle.HttpCheckerBase
Note that the check/1
, compare_response/2
and compare_resp_body/4
functions are all overridable
so that this module can be used as a base for more custom implementations; indeed Fettle.HttpChecker
itself
is just a default implementation of Fettle.HttpCheckerBase
.
e.g.
defmodule MyResponseChecker do
use Fettle.HttpCheckerBase
def compare_response(resp = %HTTPoison.Response{}, config) do
# your implementation
end
end
compare_response/2
is called from check/1
, and checks the status code matches, before calling compare_resp_body/4
if
the resp_body
option is given, so you can override at either the request or body level.
Note that if you are overriding only compare_resp_body/4
, you must provide a value for the resp_body
option,
else it will be skipped by the default implementation of compare_response/2
. You can do this robustly by also
overriding init/1
to pass a truthy value for the resp_body
opt, and calling super
:
defmodule JsonBodyChecker do
use Fettle.HttpCheckerBase
def init(opts), do: super([{:resp_body, true} | opts])
def compare_resp_body("application/json", body, true, config) do
# your implementation
end
end
The result of the init/1
function is a map with all keys from the opts
argument; this is passed through to the lower-level functions
as config
, so you can add your own parameters for these functions.
Link to this section Summary
Functions
Call an HTTP(S) end-point and assert a response code/response body and return a Fettle.Checker.Response
Compare the response body with to an expected content-type, body or pattern and compute a Fettle.Checker.Response
compare the HTTP response and compute a Fettle.Checker.Result
merges default HTTPoison
hackney options with httpoison[:hackney]
options specified in config
adds a user-agent
header using fettle.system_code
config, if not already present
merges default HTTPoison
options with httpoison
options specified in config
check options and transform into a map, applying defaults as necessary
choses the method used to compare the response body and executes it
Link to this section Functions
Call an HTTP(S) end-point and assert a response code/response body and return a Fettle.Checker.Response
compare_resp_body( content_type :: String.t(), body :: String.t(), expected_body :: any(), config :: map() ) :: Fettle.Checker.Result.t()
Compare the response body with to an expected content-type, body or pattern and compute a Fettle.Checker.Response
.
Supported types for expected_body
are: String
or Regex
, extensions of this module may implement others.
compare_response(resp :: HTTPoison.Response.t(), config :: map()) :: Fettle.Checker.Result.t()
compare the HTTP response and compute a Fettle.Checker.Result
merges default HTTPoison
hackney options with httpoison[:hackney]
options specified in config.
adds a user-agent
header using fettle.system_code
config, if not already present
merges default HTTPoison
options with httpoison
options specified in config.
check options and transform into a map, applying defaults as necessary
run_compare_response_body( resp_body_opt :: any(), resp :: HTTPoison.Response.t(), config :: map() ) :: Fettle.Checker.Result.t()
choses the method used to compare the response body and executes it