2017-03-07 18:34:54 +04:00
#!/usr/bin/env bash
set -e
2017-01-12 21:59:02 -05:00
2017-03-07 18:34:54 +04:00
# Get the directory of where this script is.
SOURCE = " ${ BASH_SOURCE [0] } "
while [ -h " $SOURCE " ] ; do SOURCE = " $( readlink " $SOURCE " ) " ; done
DIR = " $( cd -P " $( dirname " $SOURCE " ) " && pwd ) "
2017-01-12 21:59:02 -05:00
2017-03-07 18:34:54 +04:00
# Change into that dir because we expect that.
pushd " $DIR "
2016-06-23 20:36:59 -04:00
2017-03-08 16:24:04 +04:00
echo "==> Building the server"
go build -o rpcserver main.go
echo "==> (Re)starting the server"
PID = $( pgrep rpcserver || echo "" )
if [ [ $PID != "" ] ] ; then
kill -9 " $PID "
fi
./rpcserver &
2016-06-23 20:36:59 -04:00
PID = $!
sleep 2
2017-03-08 16:24:04 +04:00
echo "==> simple request"
2017-03-07 18:34:54 +04:00
R1 = $( curl -s 'http://localhost:8008/hello_world?name="my_world"&num=5' )
R2 = $( curl -s --data @data.json http://localhost:8008)
2017-01-02 09:50:20 -08:00
if [ [ " $R1 " != " $R2 " ] ] ; then
echo "responses are not identical:"
echo " R1: $R1 "
echo " R2: $R2 "
2017-03-08 17:16:01 +04:00
echo "FAIL"
2017-01-07 14:00:27 -08:00
exit 1
2017-01-02 09:50:20 -08:00
else
2017-03-08 16:24:04 +04:00
echo "OK"
2017-01-02 09:50:20 -08:00
fi
2016-06-23 20:36:59 -04:00
2017-03-08 16:24:04 +04:00
echo "==> request with 0x-prefixed hex string arg"
2017-03-07 18:34:54 +04:00
R1 = $( curl -s 'http://localhost:8008/hello_world?name=0x41424344&num=123' )
2017-01-02 09:50:20 -08:00
R2 = '{"jsonrpc":"2.0","id":"","result":{"Result":"hi ABCD 123"},"error":""}'
if [ [ " $R1 " != " $R2 " ] ] ; then
echo "responses are not identical:"
echo " R1: $R1 "
echo " R2: $R2 "
2017-03-08 17:16:01 +04:00
echo "FAIL"
2017-01-07 14:00:27 -08:00
exit 1
2017-01-02 09:50:20 -08:00
else
2017-03-08 16:24:04 +04:00
echo "OK"
2017-01-02 09:50:20 -08:00
fi
2016-06-23 20:36:59 -04:00
2017-03-08 17:16:01 +04:00
echo "==> request with missing params"
R1 = $( curl -s 'http://localhost:8008/hello_world' )
R2 = '{"jsonrpc":"2.0","id":"","result":{"Result":"hi 0"},"error":""}'
if [ [ " $R1 " != " $R2 " ] ] ; then
echo "responses are not identical:"
echo " R1: $R1 "
echo " R2: $R2 "
echo "FAIL"
exit 1
else
echo "OK"
fi
echo "==> request with unquoted string arg"
2017-03-07 18:34:54 +04:00
R1 = $( curl -s 'http://localhost:8008/hello_world?name=abcd&num=123' )
2017-01-02 09:50:20 -08:00
R2 = "{\"jsonrpc\":\"2.0\",\"id\":\"\",\"result\":null,\"error\":\"Error converting http params to args: invalid character 'a' looking for beginning of value\"}"
2016-06-23 20:36:59 -04:00
if [ [ " $R1 " != " $R2 " ] ] ; then
echo "responses are not identical:"
echo " R1: $R1 "
echo " R2: $R2 "
2017-03-08 17:16:01 +04:00
echo "FAIL"
2017-01-07 14:00:27 -08:00
exit 1
2017-01-02 09:50:20 -08:00
else
2017-03-08 16:24:04 +04:00
echo "OK"
2016-06-23 20:36:59 -04:00
fi
2017-01-02 09:50:20 -08:00
2017-03-08 16:24:04 +04:00
echo "==> request with string type when expecting number arg"
2017-03-07 18:34:54 +04:00
R1 = $( curl -s 'http://localhost:8008/hello_world?name="abcd"&num=0xabcd' )
2017-01-12 10:22:23 -05:00
R2 = "{\"jsonrpc\":\"2.0\",\"id\":\"\",\"result\":null,\"error\":\"Error converting http params to args: Got a hex string arg, but expected 'int'\"}"
2017-01-07 14:21:49 -08:00
if [ [ " $R1 " != " $R2 " ] ] ; then
echo "responses are not identical:"
echo " R1: $R1 "
echo " R2: $R2 "
2017-03-08 17:16:01 +04:00
echo "FAIL"
2017-01-07 14:21:49 -08:00
exit 1
else
2017-03-08 16:24:04 +04:00
echo "OK"
2017-01-07 14:21:49 -08:00
fi
2017-03-08 16:24:04 +04:00
echo "==> Stopping the server"
kill -9 $PID
rm -f rpcserver
2017-03-07 18:34:54 +04:00
popd
2017-03-08 16:24:04 +04:00
exit 0