github.com/cloudwego/kitex@v0.9.0/pkg/generic/jsonpb_codec_test.go (about) 1 /* 2 * Copyright 2023 CloudWeGo Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package generic 18 19 import ( 20 "context" 21 "testing" 22 23 "github.com/cloudwego/dynamicgo/conv" 24 dproto "github.com/cloudwego/dynamicgo/proto" 25 26 "github.com/cloudwego/kitex/internal/mocks" 27 "github.com/cloudwego/kitex/internal/test" 28 "github.com/cloudwego/kitex/pkg/remote" 29 "github.com/cloudwego/kitex/pkg/rpcinfo" 30 "github.com/cloudwego/kitex/transport" 31 ) 32 33 var echoIDLPath = "./jsonpb_test/idl/echo.proto" 34 35 func TestRun(t *testing.T) { 36 t.Run("TestJsonPbCodec", TestJsonPbCodec) 37 } 38 39 func TestJsonPbCodec(t *testing.T) { 40 gOpts := &Options{dynamicgoConvOpts: conv.Options{}} 41 opts := dproto.Options{} 42 p, err := NewPbFileProviderWithDynamicGo(echoIDLPath, context.Background(), opts) 43 test.Assert(t, err == nil) 44 45 jpc, err := newJsonPbCodec(p, pbCodec, gOpts) 46 test.Assert(t, err == nil) 47 48 defer jpc.Close() 49 50 test.Assert(t, jpc.Name() == "JSONPb") 51 method, err := jpc.getMethod(nil, "Echo") 52 test.Assert(t, err == nil) 53 test.Assert(t, method.Name == "Echo") 54 55 ctx := context.Background() 56 sendMsg := initJsonPbSendMsg(transport.TTHeaderFramed) 57 58 // Marshal side 59 out := remote.NewWriterBuffer(256) 60 err = jpc.Marshal(ctx, sendMsg, out) 61 test.Assert(t, err == nil) 62 63 // UnMarshal side 64 recvMsg := initJsonPbRecvMsg() 65 buf, err := out.Bytes() 66 test.Assert(t, err == nil) 67 recvMsg.SetPayloadLen(len(buf)) 68 in := remote.NewReaderBuffer(buf) 69 err = jpc.Unmarshal(ctx, recvMsg, in) 70 test.Assert(t, err == nil) 71 args, ok := recvMsg.Data().(*Args) 72 test.Assert(t, ok) 73 test.Assert(t, args.Request == `{"message":"hello world!"}`) 74 } 75 76 func initJsonPbSendMsg(tp transport.Protocol) remote.Message { 77 req := &Args{ 78 Request: `{"message":"hello world!"}`, 79 Method: "Echo", 80 } 81 svcInfo := mocks.ServiceInfo() 82 ink := rpcinfo.NewInvocation("", "Echo") 83 ri := rpcinfo.NewRPCInfo(nil, nil, ink, nil, rpcinfo.NewRPCStats()) 84 msg := remote.NewMessage(req, svcInfo, ri, remote.Call, remote.Client) 85 msg.SetProtocolInfo(remote.NewProtocolInfo(tp, svcInfo.PayloadCodec)) 86 return msg 87 } 88 89 func initJsonPbRecvMsg() remote.Message { 90 resp := &Args{} 91 ink := rpcinfo.NewInvocation("", "Echo") 92 ri := rpcinfo.NewRPCInfo(nil, nil, ink, nil, rpcinfo.NewRPCStats()) 93 msg := remote.NewMessage(resp, mocks.ServiceInfo(), ri, remote.Call, remote.Server) 94 return msg 95 }