laconicd-deprecated/rpc/rpc_test.go

95 lines
1.8 KiB
Go
Raw Normal View History

2018-08-09 10:36:47 +00:00
package rpc
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"testing"
2018-09-28 21:40:58 +00:00
"time"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
2018-08-09 10:36:47 +00:00
)
type TestService struct{}
func (s *TestService) Foo(arg string) string {
return arg
}
func TestStartHTTPEndpointStartStop(t *testing.T) {
config := &Config{
RPCAddr: "127.0.0.1",
RPCPort: randomPort(),
}
2018-09-28 21:40:58 +00:00
2018-08-09 10:36:47 +00:00
ctx, cancel := context.WithCancel(context.Background())
2018-09-28 21:40:58 +00:00
_, err := StartHTTPEndpoint(
ctx, config, []rpc.API{
{
Namespace: "test",
Version: "1.0",
Service: &TestService{},
Public: true,
},
},
rpc.HTTPTimeouts{
ReadTimeout: 5 * time.Second,
WriteTimeout: 5 * time.Second,
IdleTimeout: 5 * time.Second,
2018-08-09 10:36:47 +00:00
},
2018-09-28 21:40:58 +00:00
)
2018-08-09 10:36:47 +00:00
require.Nil(t, err, "unexpected error")
2018-09-28 21:40:58 +00:00
2018-08-09 10:36:47 +00:00
res, err := rpcCall(config.RPCPort, "test_foo", []string{"baz"})
require.Nil(t, err, "unexpected error")
2018-09-28 21:40:58 +00:00
2018-08-09 10:36:47 +00:00
resStr := res.(string)
require.Equal(t, "baz", resStr)
2018-09-28 21:40:58 +00:00
2018-08-09 10:36:47 +00:00
cancel()
2018-09-28 21:40:58 +00:00
2018-08-09 10:36:47 +00:00
_, err = rpcCall(config.RPCPort, "test_foo", []string{"baz"})
require.NotNil(t, err)
}
func rpcCall(port int, method string, params []string) (interface{}, error) {
parsedParams, err := json.Marshal(params)
if err != nil {
return nil, err
}
2018-09-28 21:40:58 +00:00
fullBody := fmt.Sprintf(
`{ "id": 1, "jsonrpc": "2.0", "method": "%s", "params": %s }`,
method, string(parsedParams),
)
2018-08-09 10:36:47 +00:00
res, err := http.Post(fmt.Sprintf("http://127.0.0.1:%d", port), "application/json", strings.NewReader(fullBody))
if err != nil {
return nil, err
}
2018-09-28 21:40:58 +00:00
2018-08-09 10:36:47 +00:00
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var out map[string]interface{}
err = json.Unmarshal(data, &out)
if err != nil {
return nil, err
}
result := out["result"].(interface{})
return result, nil
}
func randomPort() int {
return rand.Intn(65535-1025) + 1025
}