github.com/cloudwego/kitex@v0.9.0/pkg/generic/generic_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  	"bytes"
    21  	"context"
    22  	"net/http"
    23  	"testing"
    24  
    25  	dproto "github.com/cloudwego/dynamicgo/proto"
    26  
    27  	"github.com/cloudwego/kitex/internal/test"
    28  	"github.com/cloudwego/kitex/pkg/serviceinfo"
    29  )
    30  
    31  func TestBinaryThriftGeneric(t *testing.T) {
    32  	g := BinaryThriftGeneric()
    33  	defer g.Close()
    34  
    35  	test.Assert(t, g.Framed() == true)
    36  	test.Assert(t, g.PayloadCodec().Name() == "RawThriftBinary")
    37  	test.Assert(t, g.PayloadCodecType() == serviceinfo.Thrift)
    38  
    39  	method, err := g.GetMethod(nil, "Test")
    40  	test.Assert(t, err == nil)
    41  	test.Assert(t, method.Name == "Test")
    42  }
    43  
    44  func TestMapThriftGeneric(t *testing.T) {
    45  	p, err := NewThriftFileProvider("./map_test/idl/mock.thrift")
    46  	test.Assert(t, err == nil)
    47  
    48  	// new
    49  	g, err := MapThriftGeneric(p)
    50  	test.Assert(t, err == nil)
    51  	defer g.Close()
    52  
    53  	mg, ok := g.(*mapThriftGeneric)
    54  	test.Assert(t, ok)
    55  
    56  	test.Assert(t, g.PayloadCodec().Name() == "MapThrift")
    57  
    58  	err = SetBinaryWithBase64(g, true)
    59  	test.Assert(t, err == nil)
    60  	test.Assert(t, mg.codec.binaryWithBase64)
    61  	test.Assert(t, g.Framed() == false)
    62  
    63  	err = SetBinaryWithByteSlice(g, true)
    64  	test.Assert(t, err == nil)
    65  	test.Assert(t, mg.codec.binaryWithByteSlice)
    66  
    67  	test.Assert(t, g.PayloadCodecType() == serviceinfo.Thrift)
    68  
    69  	method, err := g.GetMethod(nil, "Test")
    70  	test.Assert(t, err == nil)
    71  	test.Assert(t, method.Name == "Test")
    72  }
    73  
    74  func TestMapThriftGenericForJSON(t *testing.T) {
    75  	p, err := NewThriftFileProvider("./map_test/idl/mock.thrift")
    76  	test.Assert(t, err == nil)
    77  
    78  	// new
    79  	g, err := MapThriftGenericForJSON(p)
    80  	test.Assert(t, err == nil)
    81  	defer g.Close()
    82  
    83  	mg, ok := g.(*mapThriftGeneric)
    84  	test.Assert(t, ok)
    85  
    86  	test.Assert(t, g.PayloadCodec().Name() == "MapThrift")
    87  
    88  	err = SetBinaryWithBase64(g, true)
    89  	test.Assert(t, err == nil)
    90  	test.Assert(t, mg.codec.binaryWithBase64)
    91  	test.Assert(t, g.Framed() == false)
    92  
    93  	test.Assert(t, g.PayloadCodecType() == serviceinfo.Thrift)
    94  
    95  	method, err := g.GetMethod(nil, "Test")
    96  	test.Assert(t, err == nil)
    97  	test.Assert(t, method.Name == "Test")
    98  }
    99  
   100  func TestHTTPThriftGeneric(t *testing.T) {
   101  	p, err := NewThriftFileProvider("./http_test/idl/binary_echo.thrift")
   102  	test.Assert(t, err == nil)
   103  
   104  	var opts []Option
   105  	opts = append(opts, UseRawBodyForHTTPResp(true))
   106  	g, err := HTTPThriftGeneric(p, opts...)
   107  	test.Assert(t, err == nil)
   108  	defer g.Close()
   109  
   110  	hg, ok := g.(*httpThriftGeneric)
   111  	test.Assert(t, ok)
   112  
   113  	test.Assert(t, g.PayloadCodec().Name() == "HttpThrift")
   114  
   115  	test.Assert(t, !hg.codec.dynamicgoEnabled)
   116  	test.Assert(t, hg.codec.useRawBodyForHTTPResp)
   117  	test.Assert(t, !hg.codec.binaryWithBase64)
   118  	test.Assert(t, !hg.codec.convOpts.NoBase64Binary)
   119  	test.Assert(t, !hg.codec.convOptsWithThriftBase.NoBase64Binary)
   120  
   121  	err = SetBinaryWithBase64(g, true)
   122  	test.Assert(t, err == nil)
   123  	test.Assert(t, hg.codec.binaryWithBase64)
   124  	test.Assert(t, !hg.codec.convOpts.NoBase64Binary)
   125  	test.Assert(t, !hg.codec.convOptsWithThriftBase.NoBase64Binary)
   126  
   127  	test.Assert(t, g.Framed() == false)
   128  
   129  	test.Assert(t, g.PayloadCodecType() == serviceinfo.Thrift)
   130  
   131  	url := "https://example.com/BinaryEcho"
   132  	body := map[string]interface{}{
   133  		"msg": "Test",
   134  	}
   135  	data, err := customJson.Marshal(body)
   136  	test.Assert(t, err == nil)
   137  	// NewRequest
   138  	req, err := http.NewRequest(http.MethodGet, url, bytes.NewBuffer(data))
   139  	test.Assert(t, err == nil)
   140  	customReq, err := FromHTTPRequest(req)
   141  	test.Assert(t, err == nil)
   142  
   143  	method, err := g.GetMethod(customReq, "Test")
   144  	test.Assert(t, err == nil)
   145  	test.Assert(t, method.Name == "BinaryEcho")
   146  }
   147  
   148  func TestHTTPThriftGenericWithDynamicGo(t *testing.T) {
   149  	p, err := NewThriftFileProviderWithDynamicGo("./http_test/idl/binary_echo.thrift")
   150  	test.Assert(t, err == nil)
   151  
   152  	g, err := HTTPThriftGeneric(p)
   153  	test.Assert(t, err == nil)
   154  	defer g.Close()
   155  
   156  	hg, ok := g.(*httpThriftGeneric)
   157  	test.Assert(t, ok)
   158  
   159  	test.Assert(t, g.PayloadCodec().Name() == "HttpThrift")
   160  
   161  	test.Assert(t, hg.codec.dynamicgoEnabled)
   162  	test.Assert(t, !hg.codec.useRawBodyForHTTPResp)
   163  	test.Assert(t, !hg.codec.binaryWithBase64)
   164  	test.Assert(t, hg.codec.convOpts.NoBase64Binary)
   165  	test.Assert(t, hg.codec.convOptsWithThriftBase.NoBase64Binary)
   166  
   167  	err = SetBinaryWithBase64(g, true)
   168  	test.Assert(t, err == nil)
   169  	test.Assert(t, hg.codec.binaryWithBase64)
   170  	test.Assert(t, !hg.codec.convOpts.NoBase64Binary)
   171  	test.Assert(t, !hg.codec.convOptsWithThriftBase.NoBase64Binary)
   172  
   173  	test.Assert(t, g.Framed() == true)
   174  
   175  	test.Assert(t, g.PayloadCodecType() == serviceinfo.Thrift)
   176  
   177  	url := "https://example.com/BinaryEcho"
   178  	body := map[string]interface{}{
   179  		"msg": "Test",
   180  	}
   181  	data, err := customJson.Marshal(body)
   182  	test.Assert(t, err == nil)
   183  	// NewRequest
   184  	req, err := http.NewRequest(http.MethodGet, url, bytes.NewBuffer(data))
   185  	test.Assert(t, err == nil)
   186  	customReq, err := FromHTTPRequest(req)
   187  	test.Assert(t, err == nil)
   188  
   189  	method, err := g.GetMethod(customReq, "Test")
   190  	test.Assert(t, err == nil)
   191  	test.Assert(t, method.Name == "BinaryEcho")
   192  }
   193  
   194  func TestJSONThriftGeneric(t *testing.T) {
   195  	p, err := NewThriftFileProvider("./json_test/idl/mock.thrift")
   196  	test.Assert(t, err == nil)
   197  
   198  	g, err := JSONThriftGeneric(p)
   199  	test.Assert(t, err == nil)
   200  	defer g.Close()
   201  
   202  	jg, ok := g.(*jsonThriftGeneric)
   203  	test.Assert(t, ok)
   204  
   205  	test.Assert(t, g.PayloadCodec().Name() == "JSONThrift")
   206  
   207  	test.Assert(t, !jg.codec.dynamicgoEnabled)
   208  	test.Assert(t, jg.codec.binaryWithBase64)
   209  	test.Assert(t, !jg.codec.convOpts.NoBase64Binary)
   210  	test.Assert(t, !jg.codec.convOptsWithThriftBase.NoBase64Binary)
   211  
   212  	err = SetBinaryWithBase64(g, false)
   213  	test.Assert(t, err == nil)
   214  	test.Assert(t, !jg.codec.binaryWithBase64)
   215  	test.Assert(t, !jg.codec.convOpts.NoBase64Binary)
   216  	test.Assert(t, !jg.codec.convOptsWithThriftBase.NoBase64Binary)
   217  
   218  	test.Assert(t, g.Framed() == false)
   219  
   220  	test.Assert(t, g.PayloadCodecType() == serviceinfo.Thrift)
   221  
   222  	method, err := g.GetMethod(nil, "Test")
   223  	test.Assert(t, err == nil)
   224  	test.Assert(t, method.Name == "Test")
   225  }
   226  
   227  func TestJSONThriftGenericWithDynamicGo(t *testing.T) {
   228  	p, err := NewThriftFileProviderWithDynamicGo("./json_test/idl/mock.thrift")
   229  	test.Assert(t, err == nil)
   230  
   231  	g, err := JSONThriftGeneric(p)
   232  	test.Assert(t, err == nil)
   233  	defer g.Close()
   234  
   235  	jg, ok := g.(*jsonThriftGeneric)
   236  	test.Assert(t, ok)
   237  
   238  	test.Assert(t, g.PayloadCodec().Name() == "JSONThrift")
   239  
   240  	test.Assert(t, jg.codec.dynamicgoEnabled)
   241  	test.Assert(t, jg.codec.binaryWithBase64)
   242  	test.Assert(t, !jg.codec.convOpts.NoBase64Binary)
   243  	test.Assert(t, !jg.codec.convOptsWithThriftBase.NoBase64Binary)
   244  
   245  	err = SetBinaryWithBase64(g, false)
   246  	test.Assert(t, err == nil)
   247  	test.Assert(t, !jg.codec.binaryWithBase64)
   248  	test.Assert(t, jg.codec.convOpts.NoBase64Binary)
   249  	test.Assert(t, jg.codec.convOptsWithThriftBase.NoBase64Binary)
   250  
   251  	test.Assert(t, g.Framed() == true)
   252  
   253  	test.Assert(t, g.PayloadCodecType() == serviceinfo.Thrift)
   254  
   255  	method, err := g.GetMethod(nil, "Test")
   256  	test.Assert(t, err == nil)
   257  	test.Assert(t, method.Name == "Test")
   258  }
   259  
   260  func TestJSONPbGeneric(t *testing.T) {
   261  	path := "./jsonpb_test/idl/echo.proto"
   262  
   263  	// initialise dynamicgo proto.ServiceDescriptor
   264  	opts := dproto.Options{}
   265  	p, err := NewPbFileProviderWithDynamicGo(path, context.Background(), opts)
   266  	if err != nil {
   267  		panic(err)
   268  	}
   269  
   270  	g, err := JSONPbGeneric(p)
   271  	test.Assert(t, err == nil)
   272  	defer g.Close()
   273  
   274  	test.Assert(t, g.PayloadCodec().Name() == "JSONPb")
   275  
   276  	test.Assert(t, g.PayloadCodecType() == serviceinfo.Protobuf)
   277  
   278  	jg, ok := g.(*jsonPbGeneric)
   279  	test.Assert(t, ok)
   280  
   281  	test.Assert(t, jg.codec.dynamicgoEnabled == true)
   282  	test.Assert(t, !jg.codec.convOpts.NoBase64Binary)
   283  
   284  	test.Assert(t, err == nil)
   285  
   286  	method, err := g.GetMethod(nil, "Echo")
   287  	test.Assert(t, err == nil)
   288  	test.Assert(t, method.Name == "Echo")
   289  }