Add the new api-test.sh script, along with a javascript websocket

tester. Now you can try out API calls on the fly, which are sent
via REST and WebSockets, to verify you get expected results back
on both.
This commit is contained in:
Kris Moore
2016-01-06 17:07:30 -05:00
parent 2bfdc0928b
commit 0694be78bf
3 changed files with 132 additions and 26 deletions

74
tests/api-test.sh Executable file
View File

@@ -0,0 +1,74 @@
#!/usr/local/bin/bash
# (Yes, yes, this is a bash script)
# Both resty/jsawk use bash'isms
# Set variable to call jsawk utility
JSAWK="./utils/jsawk -j js24"
which npm >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
echo "Please install npm first"
exit 1
fi
if [ ! -d "${HOME}/.npm/ws" ] ; then
npm install ws
if [ $? -ne 0 ] ; then
echo "Failed installing ws node module"
exit 1
fi
fi
echo "Enter your username:"
echo -e ">\c"
read fuser
echo ""
echo "Enter your password:"
echo -e ">\c"
read -s fpass
echo ""
echo "Enter the namespace:"
echo -e "(sysadm)>\c"
read namesp
if [ -z "$namesp" ] ; then
namesp="sysadm"
fi
echo ""
echo "Enter the class name:"
echo -e "(lifepreserver)>\c"
read name
if [ -z "$name" ] ; then
name="lifepreserver"
fi
echo ""
echo "Enter the payload json:"
echo -e "{ \"action\":\"listcron\" }>\c"
read payload
if [ -z "$payload" ] ; then
payload="{ \"action\":\"listcron\" }"
fi
echo ""
# Source our resty functions
. ./utils/resty -W "http://127.0.0.1:12151" -H "Accept: application/json" -H "Content-Type: application/json" -u ${fuser}:${fpass}
# Check the reply of this REST query
echo ""
echo "REST Response:"
echo "-------------------------------"
PUT /${namesp}/${name} "${payload}" -v 2>/tmp/.rstErr
if [ $? -ne 0 ] ; then
echo "Failed.. Error output:"
cat /tmp/.rstErr
fi
# Now check the response via WebSockets
echo ""
echo "WebSocket Response:"
echo "-------------------------------"
echo "{ \"namespace\":\"${namesp}\", \"name\":\"${name}\", \"id\":\"fooid\", \"args\":${payload} }" | node sendwebsocket.js "$fuser" "$fpass"

View File

@@ -1,26 +0,0 @@
#!/usr/local/bin/bash
# (Yes, yes, this is a bash script)
# Both resty/jsawk use bash'isms
# Set variable to call jsawk utility
JSAWK="./utils/jsawk -j js24"
echo "Enter your username:"
echo -e ">\c"
read fuser
echo ""
echo "Enter your password:"
echo -e ">\c"
read -s fpass
echo ""
# Source our resty functions
. ./utils/resty -W "http://127.0.0.1:12151" -H "Accept: application/json" -H "Content-Type: application/json" -u ${fuser}:${fpass}
# Check the reply of this REST query
PUT /sysadm/lifepreserver '{ "action":"listcron" }' -v 2>/tmp/.rstErr
if [ $? -ne 0 ] ; then
echo "Failed.. Error output:"
cat /tmp/.rstErr
fi

58
tests/sendwebsocket.js Normal file
View File

@@ -0,0 +1,58 @@
var WebSocket = require('ws');
var wsserver = "ws://127.0.0.1:12150";
var wstoken = "foome";
var stdargs = "";
var ignorefirst = "true";
function connectWebSocket()
{
websocket = new WebSocket(wsserver);
websocket.onopen = function(evt) { onOpen(evt) };
websocket.onclose = function(evt) { onClose(evt) };
websocket.onmessage = function(evt) { onMessage(evt) };
websocket.onerror = function(evt) { onError(evt) };
}
function onOpen(evt)
{
var authjson = '{ "namespace":"rpc", "name":"auth", "id":"authrequest", "args": { "username":"' + process.argv[2] +'", "password":"' + process.argv[3] + '" } }';
nstatus = "auth";
wstatus = "idle";
doSend(authjson);
doSend(stdargs);
websocket.close();
}
function onClose(evt)
{
}
function onMessage(evt)
{
var jsonobj = JSON.parse(evt.data);
if ( ignorefirst == "true" ) {
ignorefirst = "false";
} else {
console.log(JSON.stringify(jsonobj, null, 2));
}
}
function doSend(message)
{
//console.log('Sent: ' + message);
websocket.send(message);
}
function readStdIn(msg)
{
stdargs = msg;
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(message) {
readStdIn(message);
});
connectWebSocket();