Mercurial > hg > piper-cpp
changeset 63:72c43bc69616
Validate JSON requests and responses
author | Chris Cannam <c.cannam@qmul.ac.uk> |
---|---|
date | Fri, 23 Sep 2016 14:20:06 +0100 |
parents | 18951a1f1001 |
children | 85ec33975434 |
files | test/test-vampipe-server.sh |
diffstat | 1 files changed, 52 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/test/test-vampipe-server.sh Wed Sep 21 12:59:35 2016 +0100 +++ b/test/test-vampipe-server.sh Fri Sep 23 14:20:06 2016 +0100 @@ -1,11 +1,58 @@ -#!/bin/bash +#!/bin/bash -( bin/vampipe-convert request -i json -o capnp | - VAMP_PATH=./vamp-plugin-sdk/examples bin/vampipe-server | +set -eu -# capnp decode capnproto/vamp.capnp VampResponse +reqfile="/tmp/$$.req.json" +respfile="/tmp/$$.resp.json" +trap "rm -f $reqfile $respfile" 0 - bin/vampipe-convert response -i capnp -o json +schema=vamp-json-schema/schema + +validate() { + local file="$1" + local schemaname="$2" + jsonschema -i "$file" "$schema/$schemaname.json" 1>&2 && \ + echo "validated $schemaname" 1>&2 || \ + echo "failed to validate $schemaname" 1>&2 +} + +validate_request() { + local json="$1" + echo "$json" > "$reqfile" + validate "$reqfile" "request" + type=$(grep '"type":' "$reqfile" | sed 's/^.*"type": *"\([^"]*\)".*$/\1/') + if [ "$type" == "configure" ]; then type=configuration; fi + if [ "$type" != "list" ]; then + echo "$json" | + sed 's/^.*"content"://' | + sed 's/}}$/}/' > "$reqfile" + validate "$reqfile" "${type}request" + fi +} + +validate_response() { + local json="$1" + echo "$json" > "$respfile" + validate "$respfile" "response" + type=$(grep '"type":' "$respfile" | sed 's/^.*"type": "\([^"]*\)".*$/\1/') + if [ "$type" == "configure" ]; then type=configuration; fi + echo "$json" | + sed 's/^.*"content"://' | + sed 's/, "error.*$//' | + sed 's/, "success.*$//' > "$respfile" + validate "$respfile" "${type}response" +} + +( while read request ; do + validate_request "$request" + echo "$request" + done | + bin/vampipe-convert request -i json -o capnp | + VAMP_PATH=./vamp-plugin-sdk/examples bin/vampipe-server | + bin/vampipe-convert response -i capnp -o json | + while read response ; do + validate_response "$response" + done ) <<EOF {"type":"list"} {"type":"load","content": {"pluginKey":"vamp-example-plugins:percussiononsets","inputSampleRate":44100,"adapterFlags":["AdaptInputDomain","AdaptBufferSize"]}} @@ -13,4 +60,3 @@ {"type":"process","content": {"pluginHandle": 1, "processInput": { "timestamp": {"s": 0, "n": 0}, "inputBuffers": [{"values": [1,2,3,4,5,6,7,8]}]}}} {"type":"finish","content": {"pluginHandle": 1}} EOF -