github.com/cloudwego/kitex@v0.9.0/pkg/generic/mapthrift_codec_test.go (about)

     1  /*
     2   * Copyright 2021 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  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/cloudwego/kitex/internal/mocks"
    25  	"github.com/cloudwego/kitex/internal/test"
    26  	"github.com/cloudwego/kitex/pkg/generic/descriptor"
    27  	"github.com/cloudwego/kitex/pkg/remote"
    28  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    29  	"github.com/cloudwego/kitex/transport"
    30  )
    31  
    32  func TestMapThriftCodec(t *testing.T) {
    33  	p, err := NewThriftFileProvider("./map_test/idl/mock.thrift")
    34  	test.Assert(t, err == nil)
    35  	mtc, err := newMapThriftCodec(p, thriftCodec)
    36  	test.Assert(t, err == nil)
    37  	defer mtc.Close()
    38  	test.Assert(t, mtc.Name() == "MapThrift")
    39  
    40  	method, err := mtc.getMethod(nil, "Test")
    41  	test.Assert(t, err == nil)
    42  	test.Assert(t, method.Name == "Test")
    43  
    44  	ctx := context.Background()
    45  	sendMsg := initMapSendMsg(transport.TTHeader)
    46  
    47  	// Marshal side
    48  	out := remote.NewWriterBuffer(256)
    49  	err = mtc.Marshal(ctx, sendMsg, out)
    50  	test.Assert(t, err == nil)
    51  
    52  	// UnMarshal side
    53  	recvMsg := initMapRecvMsg()
    54  	buf, err := out.Bytes()
    55  	test.Assert(t, err == nil)
    56  	recvMsg.SetPayloadLen(len(buf))
    57  	in := remote.NewReaderBuffer(buf)
    58  	err = mtc.Unmarshal(ctx, recvMsg, in)
    59  	test.Assert(t, err == nil)
    60  }
    61  
    62  func TestMapThriftCodecSelfRef(t *testing.T) {
    63  	p, err := NewThriftFileProvider("./map_test/idl/self_ref.thrift")
    64  	test.Assert(t, err == nil)
    65  	mtc, err := newMapThriftCodec(p, thriftCodec)
    66  	test.Assert(t, err == nil)
    67  	defer mtc.Close()
    68  	test.Assert(t, mtc.Name() == "MapThrift")
    69  
    70  	method, err := mtc.getMethod(nil, "Test")
    71  	test.Assert(t, err == nil)
    72  	test.Assert(t, method.Name == "Test")
    73  
    74  	ctx := context.Background()
    75  	sendMsg := initNilMapSendMsg(transport.TTHeader)
    76  
    77  	// Marshal side
    78  	out := remote.NewWriterBuffer(0)
    79  	err = mtc.Marshal(ctx, sendMsg, out)
    80  	test.Assert(t, err == nil)
    81  
    82  	// UnMarshal side
    83  	recvMsg := initMapRecvMsg()
    84  	buf, err := out.Bytes()
    85  	test.Assert(t, err == nil)
    86  	recvMsg.SetPayloadLen(len(buf))
    87  	in := remote.NewReaderBuffer(buf)
    88  	err = mtc.Unmarshal(ctx, recvMsg, in)
    89  	test.Assert(t, err == nil)
    90  	exp := map[string]interface{}{
    91  		"self":  map[string]interface{}{},
    92  		"extra": "",
    93  	}
    94  	act := recvMsg.Data().(*Args).Request
    95  	test.Assert(t, reflect.DeepEqual(exp, act))
    96  }
    97  
    98  func TestMapThriftCodecForJSON(t *testing.T) {
    99  	p, err := NewThriftFileProvider("./map_test/idl/mock.thrift")
   100  	test.Assert(t, err == nil)
   101  	mtc, err := newMapThriftCodecForJSON(p, thriftCodec)
   102  	test.Assert(t, err == nil)
   103  	defer mtc.Close()
   104  	test.Assert(t, mtc.Name() == "MapThrift")
   105  
   106  	method, err := mtc.getMethod(nil, "Test")
   107  	test.Assert(t, err == nil)
   108  	test.Assert(t, method.Name == "Test")
   109  
   110  	ctx := context.Background()
   111  	sendMsg := initMapSendMsg(transport.TTHeader)
   112  
   113  	// Marshal side
   114  	out := remote.NewWriterBuffer(256)
   115  	err = mtc.Marshal(ctx, sendMsg, out)
   116  	test.Assert(t, err == nil)
   117  
   118  	// UnMarshal side
   119  	recvMsg := initMapRecvMsg()
   120  	buf, err := out.Bytes()
   121  	test.Assert(t, err == nil)
   122  	recvMsg.SetPayloadLen(len(buf))
   123  	in := remote.NewReaderBuffer(buf)
   124  	err = mtc.Unmarshal(ctx, recvMsg, in)
   125  	test.Assert(t, err == nil)
   126  	args, ok := recvMsg.Data().(*Args)
   127  	test.Assert(t, ok)
   128  	fieldMap, ok := args.Request.(map[string]interface{})
   129  	test.Assert(t, ok)
   130  	_, ok = fieldMap["strMap"].(map[string]interface{})
   131  	test.Assert(t, ok)
   132  }
   133  
   134  func TestMapExceptionError(t *testing.T) {
   135  	p, err := NewThriftFileProvider("./map_test/idl/mock.thrift")
   136  	test.Assert(t, err == nil)
   137  	mtc, err := newMapThriftCodec(p, thriftCodec)
   138  	test.Assert(t, err == nil)
   139  
   140  	ctx := context.Background()
   141  	out := remote.NewWriterBuffer(256)
   142  	// empty method test
   143  	emptyMethodInk := rpcinfo.NewInvocation("", "")
   144  	emptyMethodRi := rpcinfo.NewRPCInfo(nil, nil, emptyMethodInk, nil, nil)
   145  	emptyMethodMsg := remote.NewMessage(nil, nil, emptyMethodRi, remote.Exception, remote.Client)
   146  	// Marshal side
   147  	err = mtc.Marshal(ctx, emptyMethodMsg, out)
   148  	test.Assert(t, err.Error() == "empty methodName in thrift Marshal")
   149  
   150  	// Exception MsgType test
   151  	exceptionMsgTypeInk := rpcinfo.NewInvocation("", "Test")
   152  	exceptionMsgTypeRi := rpcinfo.NewRPCInfo(nil, nil, exceptionMsgTypeInk, nil, nil)
   153  	exceptionMsgTypeMsg := remote.NewMessage(&remote.TransError{}, nil, exceptionMsgTypeRi, remote.Exception, remote.Client)
   154  	// Marshal side
   155  	err = mtc.Marshal(ctx, exceptionMsgTypeMsg, out)
   156  	test.Assert(t, err == nil)
   157  }
   158  
   159  func initMapSendMsg(tp transport.Protocol) remote.Message {
   160  	req := &Args{
   161  		Request: &descriptor.HTTPRequest{
   162  			Body: map[string]interface{}{
   163  				"strMap": map[string]interface{}{
   164  					"Test1": "Test2",
   165  				},
   166  			},
   167  		},
   168  		Method: "Test",
   169  	}
   170  	svcInfo := mocks.ServiceInfo()
   171  	ink := rpcinfo.NewInvocation("", "Test")
   172  	ri := rpcinfo.NewRPCInfo(nil, nil, ink, nil, rpcinfo.NewRPCStats())
   173  	msg := remote.NewMessage(req, svcInfo, ri, remote.Call, remote.Client)
   174  	msg.SetProtocolInfo(remote.NewProtocolInfo(tp, svcInfo.PayloadCodec))
   175  	return msg
   176  }
   177  
   178  func initNilMapSendMsg(tp transport.Protocol) remote.Message {
   179  	req := &Args{
   180  		Request: &descriptor.HTTPRequest{
   181  			Body: map[string]interface{}{
   182  				"self": nil,
   183  			},
   184  		},
   185  		Method: "Test",
   186  	}
   187  	svcInfo := mocks.ServiceInfo()
   188  	ink := rpcinfo.NewInvocation("", "Test")
   189  	ri := rpcinfo.NewRPCInfo(nil, nil, ink, nil, rpcinfo.NewRPCStats())
   190  	msg := remote.NewMessage(req, svcInfo, ri, remote.Call, remote.Client)
   191  	msg.SetProtocolInfo(remote.NewProtocolInfo(tp, svcInfo.PayloadCodec))
   192  	return msg
   193  }
   194  
   195  func initMapRecvMsg() remote.Message {
   196  	req := &Args{
   197  		Request: "Test",
   198  		Method:  "Test",
   199  	}
   200  	ink := rpcinfo.NewInvocation("", "Test")
   201  	ri := rpcinfo.NewRPCInfo(nil, nil, ink, nil, rpcinfo.NewRPCStats())
   202  	msg := remote.NewMessage(req, mocks.ServiceInfo(), ri, remote.Call, remote.Server)
   203  	return msg
   204  }