Primitive testing of a REST API in bash
I have been developing the Windmill REST API using JAX-RS and wanted a very basic, lightweight way to ensure the API hasn’t regressed.
I have been using curl to quickly check the API anyway so I thought I’d take it a step further.
#!/bin/bash
set -e
function assertTrue()
{
if [ ${1} -eq ${2} ]; then
echo *SUCCESS*
else
echo "*FAILURE* expected: ${1} actual: ${2}"
exit 1
fi
}
echo "Given valid subscription access; Assert GET exports 200 OK"
HTTP_CODE=$(set -x;curl -s -o /dev/null -w "%{http_code}" https://api.windmill.io/account/qnoid/exports -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpc1ZhbGlkIn0.rqED0v7meSp72MBaeQtyXce3S_dJoh_eo6iIV2Rpt3s")
set +x;assertTrue 200 "${HTTP_CODE}"
echo "Given invalid subscription access; Assert GET exports 401 Unauthorized"
HTTP_CODE=$(set -x;curl -s -o /dev/null -w "%{http_code}" https://api.windmill.io/account/qnoid/exports -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpc1ZhbGlkIn0.3zjr4euU206esZpd0w6Au4o5qYk4ydiuAKXE6SClUaw")
set +x;assertTrue 401 "${HTTP_CODE}"
Which gives the following output
Given valid subscription access; Assert GET exports 200 OK
++ curl -s -o /dev/null -w "%{http_code}" https://api.windmill.io/account/qnoid/exports -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpc1ZhbGlkIn0.rqED0v7meSp72MBaeQtyXce3S_dJoh_eo6iIV2Rpt3s'
*SUCCESS*
Given invalid subscription access; Assert GET exports 401 Unauthorized
++ curl -s -o /dev/null -w "%{http_code}" https://api.windmill.io/account/qnoid/exports -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpc1ZhbGlkIn0.3zjr4euU206esZpd0w6Au4o5qYk4ydiuAKXE6SClUaw'
*SUCCESS*
In case of a failure, execution stops right away with the following output
Given valid subscription access; Assert GET exports 200 OK
++ curl -s -o /dev/null -w "%{http_code}" https://api.windmill.io/account/qnoid/exports -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJpc1ZhbGlkIn0.rqED0v7meSp72MBaeQtyXce3S_dJoh_eo6iIV2Rpt3s'
*FAILURE* expected: 200 actual: 401
It’s very primitive and not hardened but it should work in the meantime.-