File: jsonrpc.bats

package info (click to toggle)
kore 4.2.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,912 kB
  • sloc: ansic: 29,227; makefile: 344; sh: 278; python: 149; cpp: 34
file content (121 lines) | stat: -rw-r--r-- 3,214 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bats

# Simple and non exhaustive test suite using bats:
# https://github.com/sstephenson/bats

PIDFILE=run/jsonrpc.pid
CONFFILE=conf/jsonrpc.conf

# Start and stop have to be tweaked before being used
stop_app() {
	if [ -f "$PIDFILE" ]; then
		kill -QUIT `cat "$PIDFILE"`
		sleep 3
	fi
	if [ -f "$PIDFILE" ]; then
		kill -KILL `cat "$PIDFILE"`
		sleep 2
	fi
}

start_app() {
	stop_app
	kore -nrc "$CONFFILE"
}

query_with_content_type() {
	curl -q \
	    -H "Content-Type: $1" \
	    -X POST \
	    --raw \
	    -d "$2" \
	    -s -S \
	    --insecure \
	    "https://127.0.0.1:8888/v1"
}

query() {
	query_with_content_type "application/json" "$1"
}

grepstr() {
	declare result=$1
	shift
	printf "%s" "$result" | grep "$@" >/dev/null
}

printrep() {
	declare query=$1
	declare result=$2
	printf "Sent:\n"
	printf "%s\n" "$query"
	printf "Received:\n"
	printf "%s\n" "$result"
}

@test "requests with no protocol returns nothing" {
	query='{"method":"foo","id":"foo"}'
	result=`query "$query"`
	printrep "$query" "$result"
	[ "$result" = "" ]
}
@test "requests with invalid protocol (1) returns nothing" {
	query='{"jsonrpc":"1.0","method":"foo","id":"foo"}'
	result=`query "$query"`
	printrep "$query" "$result"
	[ "$result" = "" ]
}
@test "requests with invalid protocol (2) returns nothing" {
	query='{"jsonrpc":2.0,"method":"foo","id":"foo"}'
	result=`query "$query"`
	printrep "$query" "$result"
	[ "$result" = "" ]
}

@test "requests with no method raise errors" {
	query='{"jsonrpc":"2.0","id":"foo"}'
	result=`query "$query"`
	printrep "$query" "$result"
	grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
}
@test "requests with invalid method raise errors" {
	query='{"jsonrpc":"2.0","method":1,"id":"foo"}'
	result=`query "$query"`
	printrep "$query" "$result"
	grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
}
@test "requests with unknown method raise errors" {
	query='{"jsonrpc":"2.0","method":"foobar","id":"foo"}'
	result=`query "$query"`
	printrep "$query" "$result"
	grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
}

@test "error responses give back the string request id" {
	query='{"jsonrpc":"2.0","id":"foo"}'
	result=`query "$query"`
	printrep "$query" "$result"
	grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
	grepstr "$result" '"id"[ \t\n]*:[ \t\n]*"foo"'
}
@test "error responses give back the integer request id" {
	query='{"jsonrpc":"2.0","id":1}'
	result=`query "$query"`
	printrep "$query" "$result"
	grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"'
	grepstr "$result" '"id"[ \t\n]*:[ \t\n]*1'
}
@test "result responses give back the string request id" {
	query='{"jsonrpc":"2.0","method":"echo","params":["foobar"],"id":"tau"}'
	result=`query "$query"`
	printrep "$query" "$result"
	grepstr "$result" '"result"[ \t\n]*:[ \t\n]*[[ \t\n]*"foobar"[ \t\n]*]'
	grepstr "$result" '"id"[ \t\n]*:[ \t\n]*"tau"'
}
@test "result responses give back the integer request id" {
	query='{"jsonrpc":"2.0","method":"echo","params":["foobar"],"id":6}'
	result=`query "$query"`
	printrep "$query" "$result"
	grepstr "$result" '"result"[ \t\n]*:[ \t\n]*[[ \t\n]*"foobar"[ \t\n]*]'
	grepstr "$result" '"id"[ \t\n]*:[ \t\n]*6'
}