github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zhttp/json_rpc_test.go (about) 1 package zhttp 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/sohaha/zlsgo" 8 "github.com/sohaha/zlsgo/zlog" 9 ) 10 11 func TestNewJSONRPC(t *testing.T) { 12 tt := zlsgo.NewTest(t) 13 14 client, err := NewJSONRPC("18181", "/__rpc", func(o *JSONRPCOptions) { 15 o.Timeout = time.Second * 10 16 o.Retry = true 17 o.RetryDelay = time.Second * 6 18 // o.TlsConfig = &tls.Config{} 19 }) 20 tt.NoError(err) 21 22 res, err := Get("http://127.0.0.1:18181/__rpc") 23 tt.NoError(err) 24 tt.Equal([]interface{}{"int", "*zhttp.Result"}, res.JSON("Cal\\.Square").Value()) 25 zlog.Debug(res) 26 27 var result Result 28 err = client.Call("Cal.Square", 12, &result) 29 tt.NoError(err) 30 tt.Equal(144, result.Ans) 31 tt.Equal(12, result.Num) 32 t.Log(result, err) 33 34 res, err = Post("http://127.0.0.1:18181/__rpc", BodyJSON(map[string]interface{}{ 35 "method": "Cal.Square", 36 "params": []int{12}, 37 "id": 1, 38 })) 39 tt.NoError(err) 40 tt.Equal(144, res.JSON("result.Ans").Int()) 41 zlog.Debug(res, err) 42 43 res, err = Put("http://127.0.0.1:18181/__rpc", BodyJSON(map[string]interface{}{ 44 "method": "Cal.Square", 45 "params": []int{12}, 46 "id": 1, 47 })) 48 t.Log(err) 49 t.Log(res) 50 51 _ = client.Close() 52 }