github.com/cloudwego/kitex@v0.9.0/pkg/generic/thrift/util_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 thrift
    18  
    19  import (
    20  	"context"
    21  	"encoding/json"
    22  	"testing"
    23  
    24  	"github.com/cloudwego/kitex/internal/test"
    25  	"github.com/cloudwego/kitex/pkg/generic/descriptor"
    26  )
    27  
    28  func TestSplitType(t *testing.T) {
    29  	pkg, name := splitType(".A")
    30  	test.Assert(t, pkg == "")
    31  	test.Assert(t, name == "A")
    32  
    33  	pkg, name = splitType("foo.bar.A")
    34  	test.Assert(t, pkg == "foo.bar")
    35  	test.Assert(t, name == "A")
    36  
    37  	pkg, name = splitType("A")
    38  	test.Assert(t, pkg == "")
    39  	test.Assert(t, name == "A")
    40  
    41  	pkg, name = splitType("")
    42  	test.Assert(t, pkg == "")
    43  	test.Assert(t, name == "")
    44  }
    45  
    46  func TestRequestMappingValue(t *testing.T) {
    47  	httpMapping := descriptor.NewAPIBody
    48  	val, err := requestMappingValue(context.Background(),
    49  		&descriptor.HTTPRequest{
    50  			Body:        map[string]interface{}{"num": ""},
    51  			ContentType: descriptor.MIMEApplicationJson,
    52  		},
    53  		&descriptor.FieldDescriptor{
    54  			Type:        &descriptor.TypeDescriptor{Type: descriptor.I64},
    55  			HTTPMapping: httpMapping("num"),
    56  		},
    57  	)
    58  	test.Assert(t, err == nil)
    59  	test.Assert(t, val == "")
    60  
    61  	val, err = requestMappingValue(context.Background(),
    62  		&descriptor.HTTPRequest{
    63  			Body:        map[string]interface{}{"num": "123"},
    64  			ContentType: descriptor.MIMEApplicationJson,
    65  		},
    66  		&descriptor.FieldDescriptor{
    67  			Type:        &descriptor.TypeDescriptor{Type: descriptor.I64},
    68  			HTTPMapping: httpMapping("num"),
    69  		},
    70  	)
    71  	test.Assert(t, err == nil)
    72  	test.Assert(t, val == int64(123))
    73  
    74  	val, err = requestMappingValue(context.Background(),
    75  		&descriptor.HTTPRequest{
    76  			Body:        map[string]interface{}{"num": json.Number("")},
    77  			ContentType: descriptor.MIMEApplicationJson,
    78  		},
    79  		&descriptor.FieldDescriptor{
    80  			Type:        &descriptor.TypeDescriptor{Type: descriptor.I64},
    81  			HTTPMapping: httpMapping("num"),
    82  		},
    83  	)
    84  	test.Assert(t, err == nil)
    85  	test.Assert(t, val == json.Number(""))
    86  
    87  	val, err = requestMappingValue(context.Background(),
    88  		&descriptor.HTTPRequest{
    89  			Body:        map[string]interface{}{"num": json.Number("123")},
    90  			ContentType: descriptor.MIMEApplicationJson,
    91  		},
    92  		&descriptor.FieldDescriptor{
    93  			Type:        &descriptor.TypeDescriptor{Type: descriptor.I64},
    94  			HTTPMapping: httpMapping("num"),
    95  		},
    96  	)
    97  	test.Assert(t, err == nil)
    98  	test.Assert(t, val == int64(123))
    99  }