# HTTP assertions

> Conditions an HTTP response must meet - status code, response time, body text, headers and JSON values - with every operator and its limits.

An HTTP check has up to 20 assertions in `kind.assertions`. A run passes only when every one holds. Without a `status` assertion, any 2xx or 3xx status passes; with one, only the status it allows.

```json
[
  { "target": "status", "op": "eq", "value": 200 },
  { "target": "response_time", "op": "lt", "ms": 800 },
  { "target": "body", "op": "contains", "value": "Welcome" },
  { "target": "header", "name": "content-type", "op": "contains", "value": "application/json" },
  { "target": "json", "path": "$.checks[0].status", "op": "equals", "value": "ok" }
]
```

## Targets

| `target`        | Fields                                  | Compares                                                                                        |
| --------------- | --------------------------------------- | ----------------------------------------------------------------------------------------------- |
| `status`        | `op` (number operator), `value` 100–599 | The status code of the final response                                                           |
| `response_time` | `op` (number operator), `ms`            | The total response time in milliseconds; `ms` must be shorter than the check's timeout          |
| `body`          | `op` (text operator), `value`           | The response body, as text, up to the first 1 MiB                                               |
| `header`        | `name`, `op` (text operator), `value`   | One response header. Names compare without regard to case; a missing header is the empty string |
| `json`          | `path`, `op` (JSON operator), `value`   | One value of a JSON body, selected by `path`                                                    |

## Operators

**Number operators** (`status`, `response_time`): `eq`, `ne`, `lt`, `le`, `gt`, `ge`.

**Text operators** (`body`, `header`):

| `op`                       | Holds when                                                                                            |
| -------------------------- | ----------------------------------------------------------------------------------------------------- |
| `contains`, `not_contains` | The text contains `value`, or does not                                                                |
| `equals`, `not_equals`     | The text is exactly `value`, or is not                                                                |
| `matches`, `not_matches`   | The regular expression `value` matches anywhere in the text, or does not. Use `^…$` for a whole match |

**JSON operators** (`json`):

| `op`                                                 | Holds when                                                                                                                                   |
| ---------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------- |
| `exists`, `not_exists`                               | The path selects a value, or selects nothing. `value` is not needed                                                                          |
| `equals`, `not_equals`                               | The selected value equals `value`. `value` is read as JSON, and as a string when it is not JSON; numbers compare by value (`1` equals `1.0`) |
| `lt`, `le`, `gt`, `ge`                               | Both are numbers and compare so. `value` must be a number                                                                                    |
| `contains`, `not_contains`, `matches`, `not_matches` | As the text operators, on a string, or on the JSON text of anything else                                                                     |

If the body is not JSON or the path selects nothing, a JSON assertion fails, except `not_exists`. The run then says `body is not JSON: …` or `no value at <path>`.

## JSON paths

`path` is a singular JSONPath query (RFC 9535) that selects at most one value:

- `$` is the whole body;
- `.name` or `['name']` is a member of an object;
- `[0]` is an element of an array, and `[-1]` counts from the end.

Wildcards (`*`), descendants (`..`), slices, unions and filters are not accepted (`check.invalid_json_path`). A path is at most 1024 bytes.

## Regular expressions

`matches` and `not_matches` take a regular expression in the syntax of the Rust `regex` crate. It runs in linear time, so look-around and backreferences are not available. An expression is at most 1024 bytes and at most 1 MiB once compiled; otherwise the check is refused with `check.invalid_regex`.

## Limits

|                                 | Limit                            |
| ------------------------------- | -------------------------------- |
| Assertions on one check         | 20                               |
| `value` of an assertion         | 1 KiB                            |
| Body read for `body` and `json` | 1 MiB                            |
| `response_time` threshold       | Shorter than the check's timeout |
