dubbo.apache.org/dubbo-go/v3@v3.1.1/protocol/jsonrpc/json_test.go (about) 1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package jsonrpc 19 20 import ( 21 "encoding/json" 22 "testing" 23 ) 24 25 import ( 26 "github.com/stretchr/testify/assert" 27 ) 28 29 type TestData struct { 30 Test string 31 } 32 33 func TestJsonClientCodecWrite(t *testing.T) { 34 cd := &CodecData{ 35 ID: 1, 36 Method: "GetUser", 37 Args: []interface{}{"args", 2}, 38 } 39 codec := newJsonClientCodec() 40 data, err := codec.Write(cd) 41 assert.NoError(t, err) 42 assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"method\":\"GetUser\",\"params\":[\"args\",2],\"id\":1}\n", string(data)) 43 44 cd.Args = 1 45 _, err = codec.Write(cd) 46 assert.EqualError(t, err, "unsupported param type: int") 47 } 48 49 func TestJsonClientCodecRead(t *testing.T) { 50 codec := newJsonClientCodec() 51 codec.pending[1] = "GetUser" 52 rsp := &TestData{} 53 err := codec.Read([]byte("{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"}}\n"), rsp) 54 assert.NoError(t, err) 55 assert.Equal(t, "test", rsp.Test) 56 57 // error 58 codec.pending[1] = "GetUser" 59 err = codec.Read([]byte("{\"jsonrpc\":\"2.0\",\"id\":1,\"error\":{\"code\":-32000,\"message\":\"error\"}}\n"), rsp) 60 assert.EqualError(t, err, "{\"code\":-32000,\"message\":\"error\"}") 61 } 62 63 func TestServerCodecWrite(t *testing.T) { 64 codec := newServerCodec() 65 a := json.RawMessage([]byte("1")) 66 codec.req = serverRequest{Version: "1.0", Method: "GetUser", ID: &a} 67 data, err := codec.Write("error", &TestData{Test: "test"}) 68 assert.NoError(t, err) 69 assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"},\"error\":{\"code\":-32000,\"message\":\"error\"}}\n", string(data)) 70 71 data, err = codec.Write("{\"code\":-32000,\"message\":\"error\"}", &TestData{Test: "test"}) 72 assert.NoError(t, err) 73 assert.Equal(t, "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":{\"Test\":\"test\"},\"error\":{\"code\":-32000,\"message\":\"error\"}}\n", string(data)) 74 } 75 76 func TestServerCodecRead(t *testing.T) { 77 codec := newServerCodec() 78 header := map[string]string{} 79 err := codec.ReadHeader(header, []byte("{\"jsonrpc\":\"2.0\",\"method\":\"GetUser\",\"params\":[\"args\",2],\"id\":1}\n")) 80 assert.EqualError(t, err, "{\"code\":-32601,\"message\":\"Method not found\"}") 81 82 header["HttpMethod"] = "POST" 83 err = codec.ReadHeader(header, []byte("{\"jsonrpc\":\"2.0\",\"method\":\"GetUser\",\"params\":[\"args\",2],\"id\":1}\n")) 84 assert.NoError(t, err) 85 assert.Equal(t, "1", string([]byte(*codec.req.ID))) 86 assert.Equal(t, "GetUser", codec.req.Method) 87 assert.Equal(t, "2.0", codec.req.Version) 88 assert.Equal(t, "[\"args\",2]", string([]byte(*codec.req.Params))) 89 90 req := []interface{}{} 91 err = codec.ReadBody(&req) 92 assert.NoError(t, err) 93 assert.Equal(t, "args", req[0]) 94 assert.Equal(t, float64(2), req[1]) 95 }