diff --git a/tests/api-test.sh b/tests/api-test.sh new file mode 100755 index 0000000..7fd5b50 --- /dev/null +++ b/tests/api-test.sh @@ -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" diff --git a/tests/rest-api-test.sh b/tests/rest-api-test.sh deleted file mode 100755 index 038ed21..0000000 --- a/tests/rest-api-test.sh +++ /dev/null @@ -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 diff --git a/tests/sendwebsocket.js b/tests/sendwebsocket.js new file mode 100644 index 0000000..993ed31 --- /dev/null +++ b/tests/sendwebsocket.js @@ -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();