github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/thrift/annotation/http_mapping_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 annotation
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"github.com/cloudwego/dynamicgo/thrift"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func GetDescFromContent(content string, method string) (*thrift.FunctionDescriptor, error) {
    28  	path := "a/b/main.thrift"
    29  	includes := map[string]string{
    30  		path: content,
    31  	}
    32  	p, err := thrift.NewDescritorFromContent(context.Background(), path, content, includes, true)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	return p.Functions()[method], nil
    37  }
    38  
    39  func TestAPIQuery(t *testing.T) {
    40  	fn, err := GetDescFromContent(`
    41  	namespace go kitex.test.server
    42  	struct Base {
    43  		1: required string required_field
    44  		999: required string test1 (api.query="test2")
    45  		3: string pass 
    46  	}
    47  
    48  	service InboxService {
    49  		string ExampleMethod(1: Base req)
    50  	}
    51  	`, "ExampleMethod")
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  	req := fn.Request().Struct().FieldByKey("req").Type()
    56  	field := req.Struct().FieldByKey("test1")
    57  	require.Equal(t, field, req.Struct().HttpMappingFields()[0])
    58  	hm := field.HTTPMappings()
    59  	require.Equal(t, []thrift.HttpMapping{apiQuery{"test2"}}, hm)
    60  }
    61  
    62  func TestAPIRawUri(t *testing.T) {
    63  	fn, err := GetDescFromContent(`
    64  	namespace go kitex.test.server
    65  	struct Base {
    66  		999: required string test1 (api.raw_uri="")
    67  	}
    68  
    69  	service InboxService {
    70  		string ExampleMethod(1: Base req)
    71  	}
    72  	`, "ExampleMethod")
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	req := fn.Request().Struct().Fields()[0].Type()
    77  	field := req.Struct().FieldByKey("test1")
    78  	require.Equal(t, field, req.Struct().HttpMappingFields()[0])
    79  	hm := field.HTTPMappings()
    80  	require.Equal(t, []thrift.HttpMapping{apiRawUri{}}, hm)
    81  }
    82  
    83  func TestAPIBody(t *testing.T) {
    84  	fn, err := GetDescFromContent(`
    85  	namespace go kitex.test.server
    86  	struct Base {
    87  		1: required Base2 f1
    88  		2: required string f2 (api.body="ff")
    89  	}
    90  
    91  	struct Base2 {
    92  		2: required string f (api.body="f")
    93  	}
    94  
    95  	service InboxService {
    96  		string ExampleMethod(1: Base req)
    97  	}
    98  	`, "ExampleMethod")
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	req := fn.Request().Struct().Fields()[0].Type()
   103  	require.Equal(t, 0, len(req.Struct().HttpMappingFields()))
   104  	f2 := req.Struct().FieldById(2)
   105  	require.Equal(t, "ff", f2.Alias())
   106  	f1 := req.Struct().FieldById(1).Type()
   107  	require.Equal(t, 1, len(f1.Struct().HttpMappingFields()))
   108  	require.Equal(t, "f", f1.Struct().FieldById(2).Alias())
   109  }