Assertions (spec: nap-assert)
The [assert] section in .nap files provides declarative assertions on HTTP responses. No scripting needed for common checks.
Syntax
Each assertion is a single line in the form:
target operator value
Status code (spec: assert-status)
[assert]
status = 200
JSON body paths (spec: assert-equals, assert-exists, assert-gt)
Assert on values in the JSON response body using dot notation:
[assert]
body.id = 1
body.name = "Ada Lovelace"
body.email exists
body.users.length > 0
Headers (spec: assert-contains, assert-exists)
Check response headers:
[assert]
headers.Content-Type contains "application/json"
headers.X-Request-Id exists
Response time (spec: assert-lt)
Assert that the response completes within a time limit:
[assert]
duration < 500ms
duration < 2s
Operators
| Operator | Description | Example | Spec |
|---|---|---|---|
= |
Equals | status = 200 |
(spec: assert-equals) |
> |
Greater than | body.count > 0 |
(spec: assert-gt) |
< |
Less than | duration < 500ms |
(spec: assert-lt) |
exists |
Field is present | body.id exists |
(spec: assert-exists) |
contains |
String contains | headers.Content-Type contains "json" |
(spec: assert-contains) |
matches |
Regex match | body.email matches "^.+@.+$" |
(spec: assert-matches) |
Multiple assertions
Add as many assertions as you need:
[assert]
status = 200
body.id exists
body.name = "Ada Lovelace"
body.email contains "@"
headers.Content-Type contains "application/json"
duration < 1000ms
All assertions are evaluated. Napper reports each one as passed or failed.
Complex assertions with scripts
For assertions that go beyond the declarative syntax, use F# or C# post-request scripts:
[script]
post = ./scripts/validate-schema.fsx
// validate-schema.fsx
let users = ctx.Response.Json.EnumerateArray() |> Seq.toList
for user in users do
if not (user.TryGetProperty("email") |> fst) then
ctx.Fail $"User {user.GetProperty("id")} missing email"