github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/testdata/t2h_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/t2j"
    25  	"github.com/cloudwego/dynamicgo/internal/util_test"
    26  	"github.com/cloudwego/dynamicgo/meta"
    27  	"github.com/cloudwego/dynamicgo/thrift"
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  func TestThrift2HTTPConv_Do(t *testing.T) {
    32  	svc, err := thrift.NewDescritorFromPath(context.Background(), util_test.MustGitPath("testdata/idl/example4.thrift"))
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	fn, err := svc.LookupFunctionByMethod("GetFavoriteMethod")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	tests := []struct {
    42  		name    string
    43  		conv    *t2j.HTTPConv
    44  		tbytes  []byte
    45  		wantErr bool
    46  		want    util_test.Resp
    47  	}{
    48  		{
    49  			name: "example4",
    50  			conv: t2j.NewHTTPConv(meta.EncodingThriftBinary, fn),
    51  			tbytes: util_test.MustDecodeHex(
    52  				t,
    53  				"80010002000000114765744661766f726974654d6574686f64000000130c00000c000108000100001e450f00020b00000002"+
    54  					"0000000568656c6c6f00000005776f726c64000c00ff080002000000000b000100000000000000",
    55  			),
    56  			want: util_test.Resp{
    57  				Body: []byte(`{"favorite":{"Uid":7749,"Stuffs":["hello","world"]},` +
    58  					`"BaseResp":{"StatusCode":0,"StatusMessage":""}}`),
    59  				StatusCode: 0,
    60  			},
    61  		},
    62  	}
    63  	for _, tt := range tests {
    64  		t.Run(tt.name, func(t *testing.T) {
    65  			resp := &util_test.Resp{}
    66  			if err := tt.conv.Do(context.Background(), resp, tt.tbytes, conv.Options{}); (err != nil) != tt.wantErr {
    67  				t.Errorf("HTTPConv.Do() error = %v, wantErr %v", err, tt.wantErr)
    68  			}
    69  			util_test.JsonEqual(t, tt.want.Body, resp.Body)
    70  			assert.Equal(t, tt.want.StatusCode, resp.StatusCode)
    71  			assert.Equal(t, tt.want.Header, resp.Header)
    72  			assert.Equal(t, tt.want.Cookie, resp.Cookie)
    73  
    74  			resp = &util_test.Resp{}
    75  			buf := make([]byte, 0, 1)
    76  			if err := tt.conv.DoInto(context.Background(), resp, tt.tbytes, &buf, conv.Options{}); (err != nil) != tt.wantErr {
    77  				t.Errorf("HTTPConv.Do() error = %v, wantErr %v", err, tt.wantErr)
    78  			}
    79  			util_test.JsonEqual(t, tt.want.Body, resp.Body)
    80  			assert.Equal(t, tt.want.StatusCode, resp.StatusCode)
    81  			assert.Equal(t, tt.want.Header, resp.Header)
    82  			assert.Equal(t, tt.want.Cookie, resp.Cookie)
    83  		})
    84  	}
    85  }