jsonrpc.bats (3214B)
1 #!/usr/bin/env bats
2
3 # Simple and non exhaustive test suite using bats:
4 # https://github.com/sstephenson/bats
5
6 PIDFILE=run/jsonrpc.pid
7 CONFFILE=conf/jsonrpc.conf
8
9 # Start and stop have to be tweaked before being used
10 stop_app() {
11 if [ -f "$PIDFILE" ]; then
12 kill -QUIT `cat "$PIDFILE"`
13 sleep 3
14 fi
15 if [ -f "$PIDFILE" ]; then
16 kill -KILL `cat "$PIDFILE"`
17 sleep 2
18 fi
19 }
20
21 start_app() {
22 stop_app
23 kore -nrc "$CONFFILE"
24 }
25
26 query_with_content_type() {
27 curl -q \
28 -H "Content-Type: $1" \
29 -X POST \
30 --raw \
31 -d "$2" \
32 -s -S \
33 --insecure \
34 "https://127.0.0.1:8888/v1"
35 }
36
37 query() {
38 query_with_content_type "application/json" "$1"
39 }
40
41 grepstr() {
42 declare result=$1
43 shift
44 printf "%s" "$result" | grep "$@" >/dev/null
45 }
46
47 printrep() {
48 declare query=$1
49 declare result=$2
50 printf "Sent:\n"
51 printf "%s\n" "$query"
52 printf "Received:\n"
53 printf "%s\n" "$result"
54 }
55
56 @test "requests with no protocol returns nothing" {
57 query='{"method":"foo","id":"foo"}'
58 result=`query "$query"`
59 printrep "$query" "$result"
60 [ "$result" = "" ]
61 }
62 @test "requests with invalid protocol (1) returns nothing" {
63 query='{"jsonrpc":"1.0","method":"foo","id":"foo"}'
64 result=`query "$query"`
65 printrep "$query" "$result"
66 [ "$result" = "" ]
67 }
68 @test "requests with invalid protocol (2) returns nothing" {
69 query='{"jsonrpc":2.0,"method":"foo","id":"foo"}'
70 result=`query "$query"`
71 printrep "$query" "$result"
72 [ "$result" = "" ]
73 }
74
75 @test "requests with no method raise errors" {
76 query='{"jsonrpc":"2.0","id":"foo"}'
77 result=`query "$query"`
78 printrep "$query" "$result"
79 grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
80 }
81 @test "requests with invalid method raise errors" {
82 query='{"jsonrpc":"2.0","method":1,"id":"foo"}'
83 result=`query "$query"`
84 printrep "$query" "$result"
85 grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
86 }
87 @test "requests with unknown method raise errors" {
88 query='{"jsonrpc":"2.0","method":"foobar","id":"foo"}'
89 result=`query "$query"`
90 printrep "$query" "$result"
91 grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
92 }
93
94 @test "error responses give back the string request id" {
95 query='{"jsonrpc":"2.0","id":"foo"}'
96 result=`query "$query"`
97 printrep "$query" "$result"
98 grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
99 grepstr "$result" '"id"[ \t\n]*:[ \t\n]*"foo"'
100 }
101 @test "error responses give back the integer request id" {
102 query='{"jsonrpc":"2.0","id":1}'
103 result=`query "$query"`
104 printrep "$query" "$result"
105 grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
106 grepstr "$result" '"id"[ \t\n]*:[ \t\n]*1'
107 }
108 @test "result responses give back the string request id" {
109 query='{"jsonrpc":"2.0","method":"echo","params":["foobar"],"id":"tau"}'
110 result=`query "$query"`
111 printrep "$query" "$result"
112 grepstr "$result" '"result"[ \t\n]*:[ \t\n]*[[ \t\n]*"foobar"[ \t\n]*]'
113 grepstr "$result" '"id"[ \t\n]*:[ \t\n]*"tau"'
114 }
115 @test "result responses give back the integer request id" {
116 query='{"jsonrpc":"2.0","method":"echo","params":["foobar"],"id":6}'
117 result=`query "$query"`
118 printrep "$query" "$result"
119 grepstr "$result" '"result"[ \t\n]*:[ \t\n]*[[ \t\n]*"foobar"[ \t\n]*]'
120 grepstr "$result" '"id"[ \t\n]*:[ \t\n]*6'
121 }