github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/testdata/h2t_conv_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 testdata
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/cloudwego/dynamicgo/conv"
    24  	"github.com/cloudwego/dynamicgo/conv/j2t"
    25  	"github.com/cloudwego/dynamicgo/http"
    26  	"github.com/cloudwego/dynamicgo/internal/util_test"
    27  	"github.com/cloudwego/dynamicgo/meta"
    28  	"github.com/cloudwego/dynamicgo/thrift"
    29  	"github.com/davecgh/go-spew/spew"
    30  	"github.com/stretchr/testify/require"
    31  	thrift_iter "github.com/thrift-iterator/go"
    32  	"github.com/thrift-iterator/go/general"
    33  	"github.com/thrift-iterator/go/protocol"
    34  )
    35  
    36  func getFnDescByPathName(t *testing.T, filePath, fnName string) *thrift.FunctionDescriptor {
    37  	svc, err := thrift.NewDescritorFromPath(context.Background(), util_test.MustGitPath(filePath))
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	fn, err := svc.LookupFunctionByMethod(fnName)
    42  	if err != nil {
    43  		t.Fatal(err)
    44  	}
    45  	return fn
    46  }
    47  
    48  func TestHTTP2ThriftConv_Do(t *testing.T) {
    49  	t.Parallel()
    50  	getFavoriteFn := getFnDescByPathName(t, "testdata/idl/example4.thrift", "GetFavoriteMethod")
    51  
    52  	tests := []struct {
    53  		name    string
    54  		proto   meta.Encoding
    55  		fn      *thrift.FunctionDescriptor
    56  		req     http.RequestGetter
    57  		want    general.Message
    58  		wantErr bool
    59  	}{
    60  		{
    61  			name:  "exmple4",
    62  			proto: meta.EncodingThriftBinary,
    63  			fn:    getFavoriteFn,
    64  			req: util_test.Req{
    65  				BodyArr:  []byte(`{"Id":1234,"Base":{"Uid":123,"Stuffs":[]}}`),
    66  				QueryMap: map[string]string{"id": "7749"},
    67  			},
    68  			want: general.Message{
    69  				MessageHeader: protocol.MessageHeader{
    70  					MessageName: "GetFavoriteMethod",
    71  					MessageType: protocol.MessageTypeCall,
    72  				},
    73  				Arguments: general.Struct{
    74  					1: general.Struct{
    75  						1: int32(7749),
    76  						255: general.Struct{
    77  							1: int32(123),
    78  							2: general.List(nil),
    79  						},
    80  					},
    81  				},
    82  			},
    83  		},
    84  		{
    85  			name:  "exmple4empty",
    86  			proto: meta.EncodingThriftBinary,
    87  			fn:    getFavoriteFn,
    88  			req: util_test.Req{
    89  				BodyArr:  []byte(``),
    90  				QueryMap: map[string]string{"id": "7749"},
    91  			},
    92  			want: general.Message{
    93  				MessageHeader: protocol.MessageHeader{
    94  					MessageName: "GetFavoriteMethod",
    95  					MessageType: protocol.MessageTypeCall,
    96  				},
    97  				Arguments: general.Struct{
    98  					1: general.Struct{
    99  						1: int32(7749),
   100  						255: general.Struct{},
   101  					},
   102  				},
   103  			},
   104  		},
   105  	}
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			convIns := j2t.NewHTTPConv(tt.proto, tt.fn)
   109  			gotTbytes := make([]byte, 0, 1)
   110  			err := convIns.DoInto(context.Background(), tt.req, &gotTbytes, conv.Options{
   111  				WriteRequireField:            true,
   112  				ReadHttpValueFallback:        true,
   113  				EnableHttpMapping:            true,
   114  			})
   115  			spew.Dump(gotTbytes)
   116  			if (err != nil) != tt.wantErr {
   117  				t.Errorf("HTTPConv.Do() error = %v, wantErr %v", err, tt.wantErr)
   118  				return
   119  			}
   120  			got := general.Message{}
   121  			err = thrift_iter.Unmarshal(gotTbytes, &got)
   122  			require.NoError(t, err)
   123  			require.Equal(t, tt.want, got)
   124  		})
   125  	}
   126  }