add test example

This commit is contained in:
Ethan Buchman
2016-06-23 20:37:35 -04:00
parent 0c70a4636a
commit a44e0e0f4b
5 changed files with 98 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
{
"jsonrpc":"2.0",
"id":"",
"method":"hello_world",
"params":["my_world", 5]
}
+35
View File
@@ -0,0 +1,35 @@
package main
import (
"fmt"
"net/http"
. "github.com/tendermint/go-common"
rpcserver "github.com/tendermint/go-rpc/server"
)
var routes = map[string]*rpcserver.RPCFunc{
"hello_world": rpcserver.NewRPCFunc(HelloWorld, "name,num"),
}
func HelloWorld(name string, num int) (Result, error) {
return Result{fmt.Sprintf("hi %s %d", name, num)}, nil
}
type Result struct {
Result string
}
func main() {
mux := http.NewServeMux()
rpcserver.RegisterRPCFuncs(mux, routes)
_, err := rpcserver.StartHTTPServer("0.0.0.0:8008", mux)
if err != nil {
Exit(err.Error())
}
// Wait forever
TrapSignal(func() {
})
}
+23
View File
@@ -0,0 +1,23 @@
#! /bin/bash
set -e
go build -o server main.go
./server > /dev/null &
PID=$!
sleep 2
R1=`curl -s 'http://localhost:8008/hello_world?name="my_world"&num=5'`
R2=`curl -s --data @data.json http://localhost:8008`
kill -9 $PID
if [[ "$R1" != "$R2" ]]; then
echo "responses are not identical:"
echo "R1: $R1"
echo "R2: $R2"
exit 1
fi
echo "Success"