diff --git a/examples/jsonrpc/test/integ/jsonrpc.bats b/examples/jsonrpc/test/integ/jsonrpc.bats index a6bcb94..e9e346a 100644 --- a/examples/jsonrpc/test/integ/jsonrpc.bats +++ b/examples/jsonrpc/test/integ/jsonrpc.bats @@ -27,11 +27,11 @@ 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" + --raw \ + -d "$2" \ + -s -S \ + --insecure \ + "https://127.0.0.1:8888/v1" } query() { @@ -46,76 +46,76 @@ grepstr() { printrep() { declare query=$1 - declare result=$2 - printf "Sent:\n" - printf "%s\n" "$query" - printf "Received:\n" - printf "%s\n" "$result" + declare result=$2 + printf "Sent:\n" + printf "%s\n" "$query" + printf "Received:\n" + printf "%s\n" "$result" } -@test "requests with no protocol raise errors" { - query='{"method":"foo"}' - result=`query "$query"` - printrep "$query" "$result" - grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"' +@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) raise errors" { - query='{"jsonrpc":"1.0","method":"foo"}' - result=`query "$query"` - printrep "$query" "$result" - grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"' +@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) raise errors" { - query='{"jsonrpc":2.0,"method":"foo"}' - result=`query "$query"` - printrep "$query" "$result" - grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"' +@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"}' - result=`query "$query"` - printrep "$query" "$result" - grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"' + 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}' - result=`query "$query"` - printrep "$query" "$result" - grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"' + 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"}' - result=`query "$query"` - printrep "$query" "$result" - grepstr "$result" '"error"[ \t\n]*:[ \t\n]*{[ \t\n]*"code"' + 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"' + 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' + 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" { query='{"jsonrpc":"2.0","method":"echo","params":"foobar","id":"tau"}' - result=`query "$query"` - printrep "$query" "$result" - grepstr "$result" '"result"[ \t\n]*:[ \t\n]*"foobar"' - grepstr "$result" '"id"[ \t\n]*:[ \t\n]*"tau"' + result=`query "$query"` + printrep "$query" "$result" + grepstr "$result" '"result"[ \t\n]*:[ \t\n]*"foobar"' + 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]*"foobar"' - grepstr "$result" '"id"[ \t\n]*:[ \t\n]*6' + result=`query "$query"` + printrep "$query" "$result" + grepstr "$result" '"result"[ \t\n]*:[ \t\n]*"foobar"' + grepstr "$result" '"id"[ \t\n]*:[ \t\n]*6' } diff --git a/src/jsonrpc.c b/src/jsonrpc.c index 478d7e7..a617fba 100644 --- a/src/jsonrpc.c +++ b/src/jsonrpc.c @@ -236,9 +236,6 @@ write_id(yajl_gen gen, yajl_val id) { int status; - if (id == NULL) - return (yajl_gen_status_ok); - if (YAJL_GEN_KO(status = YAJL_GEN_CONST_STRING(gen, "id"))) return (status); @@ -248,7 +245,7 @@ write_id(yajl_gen gen, yajl_val id) if (YAJL_IS_NUMBER(id)) { if (YAJL_IS_INTEGER(id)) return yajl_gen_integer(gen, YAJL_GET_INTEGER(id)); - return (-2); + return yajl_gen_null(gen); } if (YAJL_IS_STRING(id)) { @@ -258,7 +255,7 @@ write_id(yajl_gen gen, yajl_val id) strlen(id_str)); } - return (-1); + return yajl_gen_null(gen); } static int @@ -393,10 +390,13 @@ int jsonrpc_error(struct jsonrpc_request *req, int code, const char *msg) { char *msg_fallback; - const unsigned char *body; - size_t body_len; + const unsigned char *body = NULL; + size_t body_len = 0; int status; + if (req->id == NULL) + goto succeeded; + if ((req->gen = yajl_gen_alloc(NULL)) == NULL) { kore_log(LOG_ERR, "jsonrpc_error: Failed to allocate yajl gen"); goto failed; @@ -427,8 +427,10 @@ jsonrpc_error(struct jsonrpc_request *req, int code, const char *msg) kore_log(LOG_ERR, "jsonrpc_error: Body length overflow"); goto failed; } +succeeded: http_response(req->http, 200, body, body_len); - yajl_gen_clear(req->gen); + if (req->gen != NULL) + yajl_gen_clear(req->gen); free_request(req); return (KORE_RESULT_OK); failed: @@ -441,8 +443,11 @@ int jsonrpc_result(struct jsonrpc_request *req, int (*write_result)(struct jsonrpc_request *, void *), void *ctx) { - const unsigned char *body; - size_t body_len; + const unsigned char *body = NULL; + size_t body_len = 0; + + if (req->id == NULL) + goto succeeded; if ((req->gen = yajl_gen_alloc(NULL)) == NULL) { kore_log(LOG_ERR, "jsonrpc_result: Failed to allocate yajl gen"); @@ -469,8 +474,10 @@ jsonrpc_result(struct jsonrpc_request *req, kore_log(LOG_ERR, "jsonrpc_result: Body length overflow"); goto failed; } +succeeded: http_response(req->http, 200, body, body_len); - yajl_gen_clear(req->gen); + if (req->gen != NULL) + yajl_gen_clear(req->gen); free_request(req); return (KORE_RESULT_OK); failed: