You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.3 KiB

8 years ago
  1. #!/usr/bin/env bash
  2. set -e
  3. # Get the directory of where this script is.
  4. SOURCE="${BASH_SOURCE[0]}"
  5. while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
  6. DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
  7. # Change into that dir because we expect that.
  8. pushd "$DIR"
  9. echo "==> Building the server"
  10. go build -o rpcserver main.go
  11. echo "==> (Re)starting the server"
  12. PID=$(pgrep rpcserver || echo "")
  13. if [[ $PID != "" ]]; then
  14. kill -9 "$PID"
  15. fi
  16. ./rpcserver &
  17. PID=$!
  18. sleep 2
  19. echo "==> simple request"
  20. R1=$(curl -s 'http://localhost:8008/hello_world?name="my_world"&num=5')
  21. R2=$(curl -s --data @data.json http://localhost:8008)
  22. if [[ "$R1" != "$R2" ]]; then
  23. echo "responses are not identical:"
  24. echo "R1: $R1"
  25. echo "R2: $R2"
  26. echo "FAIL"
  27. exit 1
  28. else
  29. echo "OK"
  30. fi
  31. echo "==> request with 0x-prefixed hex string arg"
  32. R1=$(curl -s 'http://localhost:8008/hello_world?name=0x41424344&num=123')
  33. R2='{"jsonrpc":"2.0","id":"","result":{"Result":"hi ABCD 123"},"error":""}'
  34. if [[ "$R1" != "$R2" ]]; then
  35. echo "responses are not identical:"
  36. echo "R1: $R1"
  37. echo "R2: $R2"
  38. echo "FAIL"
  39. exit 1
  40. else
  41. echo "OK"
  42. fi
  43. echo "==> request with missing params"
  44. R1=$(curl -s 'http://localhost:8008/hello_world')
  45. R2='{"jsonrpc":"2.0","id":"","result":{"Result":"hi 0"},"error":""}'
  46. if [[ "$R1" != "$R2" ]]; then
  47. echo "responses are not identical:"
  48. echo "R1: $R1"
  49. echo "R2: $R2"
  50. echo "FAIL"
  51. exit 1
  52. else
  53. echo "OK"
  54. fi
  55. echo "==> request with unquoted string arg"
  56. R1=$(curl -s 'http://localhost:8008/hello_world?name=abcd&num=123')
  57. R2="{\"jsonrpc\":\"2.0\",\"id\":\"\",\"result\":null,\"error\":\"Error converting http params to args: invalid character 'a' looking for beginning of value\"}"
  58. if [[ "$R1" != "$R2" ]]; then
  59. echo "responses are not identical:"
  60. echo "R1: $R1"
  61. echo "R2: $R2"
  62. echo "FAIL"
  63. exit 1
  64. else
  65. echo "OK"
  66. fi
  67. echo "==> request with string type when expecting number arg"
  68. R1=$(curl -s 'http://localhost:8008/hello_world?name="abcd"&num=0xabcd')
  69. R2="{\"jsonrpc\":\"2.0\",\"id\":\"\",\"result\":null,\"error\":\"Error converting http params to args: Got a hex string arg, but expected 'int'\"}"
  70. if [[ "$R1" != "$R2" ]]; then
  71. echo "responses are not identical:"
  72. echo "R1: $R1"
  73. echo "R2: $R2"
  74. echo "FAIL"
  75. exit 1
  76. else
  77. echo "OK"
  78. fi
  79. echo "==> Stopping the server"
  80. kill -9 $PID
  81. rm -f rpcserver
  82. popd
  83. exit 0