Tests
To assert or validate the outcome of your input data, tests can be used. You can provide an array of tests to validate different input values. Each test can can contain one or many functions to validate the targetted value.
Usage
interfaces:
user/register:
output: http
method: POST
show_error_detail: true
actions:
- name: Input
input: a|body
assert:
error_message: "Password not strong enough"
tests:
- value: first
is_not_null: true
is_greater_than: 2
is_less_than: 20
regex: \S+
- value: last
is_not_null: true
is_greater_than: 3
is_less_than: 20
regex: \S+
- value: email
is_not_null: true
is_greater_than: 3
is_less_than: 100
regex: \S+
- value: pass
is_not_null: true
is_greater_than: 9
is_less_than: 30
- value: pass
regex: "[A-Z]"
error_message: "Password should contain one uppercase character"
- value: pass
regex: "[a-z]"
error_message: "Password should contain one lowercase character"
- value: pass
regex: "[0-9!@#]"
error_message: "Password should contain at least one number (0-9) or symbol (!,@,#)"
Tests should follow the above format. All tests should start off with the value
you want to test followed by the functions you will use to test the value
.
Value
The value field is to access the data from your defined action input.
Take for example the below JSON data is sent to an API interface.
{
"first": "tom",
"last": "bob"
}
To access the this input we would use a|body
Usage
actions:
- name: Input
input: a|body
assert:
error_message: "First name not long enough"
tests:
- value: first
You can access any values that are available in the body, including nested values using JavaScript like syntax. data[0].name.first
Name & Description
Optionally attach a name and/or description to each of your tests
Usage
name: assert_example
version: 0.0.1
description: Assert Example
metrics_enabled: true
interfaces:
comp:
output: http
route: details
method: POST
actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: firstname
name: First Name Check
description: checks if the first name is equal to tom
is_equal_to: tom
JQ
Name | Description | Examples |
---|---|---|
jq | used for parsing JSON data |
|
Usage
JQ Example
Consider the following payload
{
"firstname": "tom",
"isNull": null,
"num": 10,
"info": {
"thing1": "something",
"thing2": "something2"
}
"infoArr": [
{"name": "bob"}, {"name": "tomcat"}
]
}
name: jq_example
version: 0.0.1
description: JQ Example
metrics_enabled: true
interfaces:
JQ:
output: http
route: details
method: POST
actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: .info.thing1
is_equal_to: something
- value: .infoArr[0].name
is_equal_to: bob
Comparison Functions
Name | Description | Examples |
---|---|---|
is_equal_to | Checks if two values are equal |
|
is_not_equal_to | checks if two values are not equal |
|
is_less_than | checks if one value is less than another |
|
is_less_than_or_equal_to | checks if one value is less than or equal to another value |
|
is_greater_than | checks if one value is greater than another |
|
is_greater_than_or_equal_to | checks if one value is greater than or equal to another |
|
is_not_null | checks if one value is not null |
|
is_null | checks if one value is null |
|
is_not_empty | checks if one value is not empty |
|
is_uuid | checks if one value is a uuid |
|
is_valid_jwt | checks if one value is a valid jwt | |
contains | checks if one value contains another value | |
not_contains | checks if one value does not contain another value | |
starts_with | checks if one value starts with a value | |
ends_with | check of one value ends with a value | |
case_insensitive | expects a bool, that effects string operations: contains, not_contains, is_equal_to |
Examples
Comparison Examples
{
"firstname": "tom",
"isNull": null,
"num": 10,
"JWT": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJhaXJwaXBlIiwiZXhwIjoxNzEzMTAyODcwfQ.YnECqEgzBGnyHNyGaAHF4-OVsr7ROvZqsqQDkhzdlAM"
"uuid": "bb0f25dd-00e3-45fb-851f-b897c4aa5418"
}
name: comparison_example
version: 0.0.1
description: Comparison Example
metrics_enabled: true
interfaces:
comp:
output: http
route: details
method: POST
actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: firstname
is_equal_to: tom
is_not_equal_to: bob
is_not_null: true
is_not_empty: true
contains: to
not_contains: v
starts_with: t
ends_with: m
- value: num
is_greater_than: 5
is_greater_than_or_equal_to: 10
- value: num
is_less_than: 20
is_less_than_or_equal_to: 10
- value: uuid
is_uuid: true
- value: isNull
is_null: true
- value: JWT
is_valid_jwt: airpipe
This will result in the below output when the API is called.
{
"data": {
"CheckBody": {
"data": {
"JWT": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJhaXJwaXBlIiwiZXhwIjoxNzEzMTAyODcwfQ.YnECqEgzBGnyHNyGaAHF4-OVsr7ROvZqsqQDkhzdlAM",
"firstname": "tom",
"isNull": null,
"jwt_claims": {
"data": null,
"exp": 1713102870,
"iss": "airpipe"
},
"num": 10,
"uuid": "bb0f25dd-00e3-45fb-851f-b897c4aa5418"
},
"message": "success",
"time.ms": 0
}
},
"metrics": {
"comparision_example.config.ms": 0
}
}
In the event of failures.
{
"data": {
"CheckBody": {
"data": {
"JWT": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJpc3MiOiJhaXJwaXBlIiwiZXhwIjoxNzEzMTAyODcwfQ.YnECqEgzBGnyHNyGaAHF4-OVsr7ROvZqsqQDkhzdlAM",
"firstname": "bob",
"isNull": "s",
"num": 10,
"uuid": "bb0f-00e3-45fb-851f-b897c4aa5418"
},
"errors": [
"firstname::is_equal_to: expected tom, got bob",
"firstname::contains: expected target bob does not contain to",
"t::starts_with: expected target bob to end with t",
"m::ends_with: expected target bob to end with m",
"isNull::is_null: expected to be null",
"JWT::is_valid_jwt: ExpiredSignature",
"uuid::is_uuid: expected to be a uuid"
],
"time.ms": 0
}
},
"metrics": {
"comparison_example.config.ms": 0
}
}
Regex
Name | Description | Examples |
---|---|---|
regex | utilises Rust's regular expression crate |
|
Usage
Regex Example
Consider the following payload
{
"pass": "tom123!@#TOM",
}
name: regex_example
version: 0.0.1
description: Regex Example
metrics_enabled: true
interfaces:
REG:
output: http
route: details
method: POST
actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: pass
is_not_null: true
is_greater_than: 9
is_less_than: 30
- value: pass
regex: "[A-Z]"
error_message: "Password should contain one uppercase character"
- value: pass
regex: "[a-z]"
error_message: "Password should contain one lowercase character"
- value: pass
regex: "[0-9!@#]"
error_message: "Password should contain at least one number (0-9) or symbol (!,@,#)"
You should see a similar output if the regex does not match
{
"data": {
"CheckBody": {
"data": {
"pass": "testing"
},
"errors": [
"pass::regex: Password should contain one uppercase character",
"pass::is_greater_than: expected target testing not greater than 9",
"pass::regex: Password should contain at least one number (0-9) or symbol (!,@,#)"
],
"time.ms": 0
}
},
"metrics": {
"regex_example.config.ms": 0
}
}
Bcrypt Verify
Name | Description | Examples |
---|---|---|
bcrypt | verify bcrypt values | bcrypt_verify: <hash> |
Usage
Bcrypt Verify Example
Consider the following payload
{
"pass": "airpipe",
}
name: bcrypt_verifyexample
version: 0.0.1
description: Bcrypt Verify Example
metrics_enabled: true
interfaces:
BV:
output: http
route: details
method: POST
actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: pass
is_not_null: true
bcrypt_verify: $2y$10$ThgA0htLEcaJwOyLx33HZ.DdNolY6pjnXTtoG4fDVswXqepbSL9yu
If the verify is successful
{
"data": {
"CheckBody": {
"data": {
"pass": "airpipe"
},
"message": "success",
"time.ms": 44
}
},
"metrics": {
"regex_example.config.ms": 44
}
}
If the verify is unsuccessful
{
"data": {
"CheckBody": {
"data": {
"pass": "airpi"
},
"errors": ["pass::bcrypt_verify: false"],
"time.ms": 44
}
},
"metrics": {
"regex_example.config.ms": 44
}
}
Success & Error Message
Optionally define success and/or error messages for each of your tests.
name: assert_example
version: 0.0.1
description: Assert Example
metrics_enabled: true
interfaces:
comp:
output: http
route: details
method: POST
actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: firstname
success_message: success message
error_message: error message
is_equal_to: tom
HTTP Code On Error
Name | Description | Examples |
---|---|---|
http_code_on_error | define the http code to use when a test fails | 404 , 401 , 403 , 500 |
Usage
name: jq_example
version: 0.0.1
description: JQ Example
metrics_enabled: true
interfaces:
JQ:
output: http
route: details
method: POST
actions:
- name: CheckBody
input: a|body
assert:
tests:
- value: firstname
is_equal_to: tom
http_code_on_error: 401
Hide Errors
Name | Description | Examples |
---|---|---|
hide_errors | hide the errors from the response | hide_errors: true |