aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/v2/json_test.go
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-12-15 00:38:10 +0800
committerFelix Lange <fjl@twurst.com>2015-12-15 00:38:10 +0800
commitfa187a366dda1894179635eeec2a929bfacc4ad3 (patch)
tree4494d4dcded47dd49f2fe7374e85fefa9249246e /rpc/v2/json_test.go
parent787d71d6595df98586c625e82f4decb034215203 (diff)
parenteae81465c1c815c317cd30e4de6bdf4d59df2340 (diff)
downloadgo-tangerine-fa187a366dda1894179635eeec2a929bfacc4ad3.tar.gz
go-tangerine-fa187a366dda1894179635eeec2a929bfacc4ad3.tar.zst
go-tangerine-fa187a366dda1894179635eeec2a929bfacc4ad3.zip
Merge pull request #2035 from bas-vk/rcp-v2-rebase
rpc: new RPC implementation with pub/sub support
Diffstat (limited to 'rpc/v2/json_test.go')
-rw-r--r--rpc/v2/json_test.go73
1 files changed, 73 insertions, 0 deletions
diff --git a/rpc/v2/json_test.go b/rpc/v2/json_test.go
new file mode 100644
index 000000000..dc8a345d7
--- /dev/null
+++ b/rpc/v2/json_test.go
@@ -0,0 +1,73 @@
+package v2
+
+import (
+ "bufio"
+ "bytes"
+ "reflect"
+ "testing"
+)
+
+type RWC struct {
+ *bufio.ReadWriter
+}
+
+func (rwc *RWC) Close() error {
+ return nil
+}
+
+func TestJSONRequestParsing(t *testing.T) {
+ server := NewServer()
+ service := new(Service)
+
+ if err := server.RegisterName("calc", service); err != nil {
+ t.Fatalf("%v", err)
+ }
+
+ req := bytes.NewBufferString(`{"id": 1234, "jsonrpc": "2.0", "method": "calc_add", "params": [11, 22]}`)
+ var str string
+ reply := bytes.NewBufferString(str)
+ rw := &RWC{bufio.NewReadWriter(bufio.NewReader(req), bufio.NewWriter(reply))}
+
+ codec := NewJSONCodec(rw)
+
+ requests, batch, err := codec.ReadRequestHeaders()
+ if err != nil {
+ t.Fatalf("%v", err)
+ }
+
+ if batch {
+ t.Fatalf("Request isn't a batch")
+ }
+
+ if len(requests) != 1 {
+ t.Fatalf("Expected 1 request but got %d requests - %v", len(requests), requests)
+ }
+
+ if requests[0].service != "calc" {
+ t.Fatalf("Expected service 'calc' but got '%s'", requests[0].service)
+ }
+
+ if requests[0].method != "add" {
+ t.Fatalf("Expected method 'Add' but got '%s'", requests[0].method)
+ }
+
+ if requests[0].id != 1234 {
+ t.Fatalf("Expected id 1234 but got %d", requests[0].id)
+ }
+
+ var arg int
+ args := []reflect.Type{reflect.TypeOf(arg), reflect.TypeOf(arg)}
+
+ v, err := codec.ParseRequestArguments(args, requests[0].params)
+ if err != nil {
+ t.Fatalf("%v", err)
+ }
+
+ if len(v) != 2 {
+ t.Fatalf("Expected 2 argument values, got %d", len(v))
+ }
+
+ if v[0].Int() != 11 || v[1].Int() != 22 {
+ t.Fatalf("expected %d == 11 && %d == 22", v[0].Int(), v[1].Int())
+ }
+}