refactor tests

This commit is contained in:
Anton Kaliaev 2017-03-10 12:52:40 +04:00
parent d66ebbd904
commit 0874c72819
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
1 changed files with 29 additions and 58 deletions

View File

@ -3,6 +3,7 @@ package rpc
import ( import (
"bytes" "bytes"
crand "crypto/rand" crand "crypto/rand"
"fmt"
"math/rand" "math/rand"
"net/http" "net/http"
"os/exec" "os/exec"
@ -100,31 +101,25 @@ func init() {
} }
func testURI(t *testing.T, cl *client.URIClient) { func status(cl client.HTTPClient, val string) (string, error) {
val := "acbd"
params := map[string]interface{}{ params := map[string]interface{}{
"arg": val, "arg": val,
} }
var result Result var result Result
_, err := cl.Call("status", params, &result) if _, err := cl.Call("status", params, &result); err != nil {
require.Nil(t, err) return "", err
got := result.(*ResultStatus).Value
assert.Equal(t, got, val)
}
func testJSONRPC(t *testing.T, cl *client.JSONRPCClient) {
val := "acbd"
params := map[string]interface{}{
"arg": val,
} }
var result Result return result.(*ResultStatus).Value, nil
_, err := cl.Call("status", params, &result) }
func testWithHTTPClient(t *testing.T, cl client.HTTPClient) {
val := "acbd"
got, err := status(cl, val)
require.Nil(t, err) require.Nil(t, err)
got := result.(*ResultStatus).Value
assert.Equal(t, got, val) assert.Equal(t, got, val)
} }
func testWS(t *testing.T, cl *client.WSClient) { func testWithWSClient(t *testing.T, cl *client.WSClient) {
val := "acbd" val := "acbd"
params := map[string]interface{}{ params := map[string]interface{}{
"arg": val, "arg": val,
@ -151,51 +146,32 @@ func testWS(t *testing.T, cl *client.WSClient) {
//------------- //-------------
func TestURI_TCP(t *testing.T) { func TestServersAndClientsBasic(t *testing.T) {
cl := client.NewURIClient(tcpAddr) serverAddrs := [...]string{tcpAddr, unixAddr}
testURI(t, cl) for _, addr := range serverAddrs {
} cl1 := client.NewURIClient(addr)
fmt.Printf("=== testing server on %s using %v client", addr, cl1)
testWithHTTPClient(t, cl1)
func TestURI_UNIX(t *testing.T) { cl2 := client.NewJSONRPCClient(tcpAddr)
cl := client.NewURIClient(unixAddr) fmt.Printf("=== testing server on %s using %v client", addr, cl2)
testURI(t, cl) testWithHTTPClient(t, cl2)
}
func TestJSONRPC_TCP(t *testing.T) { cl3 := client.NewWSClient(tcpAddr, websocketEndpoint)
cl := client.NewJSONRPCClient(tcpAddr) _, err := cl3.Start()
testJSONRPC(t, cl)
}
func TestJSONRPC_UNIX(t *testing.T) {
cl := client.NewJSONRPCClient(unixAddr)
testJSONRPC(t, cl)
}
func TestWS_TCP(t *testing.T) {
cl := client.NewWSClient(tcpAddr, websocketEndpoint)
_, err := cl.Start()
require.Nil(t, err) require.Nil(t, err)
testWS(t, cl) fmt.Printf("=== testing server on %s using %v client", addr, cl3)
} testWithWSClient(t, cl3)
cl3.Stop()
func TestWS_UNIX(t *testing.T) { }
cl := client.NewWSClient(unixAddr, websocketEndpoint)
_, err := cl.Start()
require.Nil(t, err)
testWS(t, cl)
} }
func TestHexStringArg(t *testing.T) { func TestHexStringArg(t *testing.T) {
cl := client.NewURIClient(tcpAddr) cl := client.NewURIClient(tcpAddr)
// should NOT be handled as hex // should NOT be handled as hex
val := "0xabc" val := "0xabc"
params := map[string]interface{}{ got, err := status(cl, val)
"arg": val,
}
var result Result
_, err := cl.Call("status", params, &result)
require.Nil(t, err) require.Nil(t, err)
got := result.(*ResultStatus).Value
assert.Equal(t, got, val) assert.Equal(t, got, val)
} }
@ -203,13 +179,8 @@ func TestQuotedStringArg(t *testing.T) {
cl := client.NewURIClient(tcpAddr) cl := client.NewURIClient(tcpAddr)
// should NOT be unquoted // should NOT be unquoted
val := "\"abc\"" val := "\"abc\""
params := map[string]interface{}{ got, err := status(cl, val)
"arg": val,
}
var result Result
_, err := cl.Call("status", params, &result)
require.Nil(t, err) require.Nil(t, err)
got := result.(*ResultStatus).Value
assert.Equal(t, got, val) assert.Equal(t, got, val)
} }