github.com/cloudwego/kitex@v0.9.0/pkg/generic/httppb_test/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 test
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"io/ioutil"
    23  	"net"
    24  	"net/http"
    25  	"os"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/jhump/protoreflect/dynamic"
    30  	"google.golang.org/protobuf/proto"
    31  
    32  	"github.com/cloudwego/kitex/client/callopt"
    33  	"github.com/cloudwego/kitex/client/genericclient"
    34  	"github.com/cloudwego/kitex/internal/test"
    35  	"github.com/cloudwego/kitex/pkg/generic"
    36  	"github.com/cloudwego/kitex/pkg/generic/descriptor"
    37  	"github.com/cloudwego/kitex/pkg/generic/httppb_test/idl"
    38  	"github.com/cloudwego/kitex/server"
    39  )
    40  
    41  func TestRun(t *testing.T) {
    42  	t.Run("testThriftNormalEcho", testThriftNormalEcho)
    43  }
    44  
    45  func initThriftClientByIDL(t *testing.T, addr, idl, pbIdl string) genericclient.Client {
    46  	p, err := generic.NewThriftFileProvider(idl)
    47  	test.Assert(t, err == nil)
    48  	pbf, err := os.Open(pbIdl)
    49  	test.Assert(t, err == nil)
    50  	pbContent, err := ioutil.ReadAll(pbf)
    51  	test.Assert(t, err == nil)
    52  	pbf.Close()
    53  	pbp, err := generic.NewPbContentProvider(pbIdl, map[string]string{pbIdl: string(pbContent)})
    54  	test.Assert(t, err == nil)
    55  	g, err := generic.HTTPPbThriftGeneric(p, pbp)
    56  	test.Assert(t, err == nil)
    57  	cli := newGenericClient("destServiceName", g, addr)
    58  	test.Assert(t, err == nil)
    59  	return cli
    60  }
    61  
    62  func initThriftServer(t *testing.T, address string, handler generic.Service) server.Server {
    63  	addr, _ := net.ResolveTCPAddr("tcp", address)
    64  	p, err := generic.NewThriftFileProvider("./idl/echo.thrift")
    65  	test.Assert(t, err == nil)
    66  	g, err := generic.MapThriftGeneric(p)
    67  	test.Assert(t, err == nil)
    68  	svr := newGenericServer(g, addr, handler)
    69  	test.Assert(t, err == nil)
    70  	test.WaitServerStart(addr.String())
    71  	return svr
    72  }
    73  
    74  func testThriftNormalEcho(t *testing.T) {
    75  	addr := test.GetLocalAddress()
    76  	svr := initThriftServer(t, addr, new(GenericServiceEchoImpl))
    77  	cli := initThriftClientByIDL(t, addr, "./idl/echo.thrift", "./idl/echo.proto")
    78  	url := "http://example.com/Echo"
    79  
    80  	// []byte value for field
    81  	body := &idl.Message{
    82  		Tiny:  3,
    83  		Large: 1 << 40,
    84  		Tenum: idl.TestEnum_First,
    85  		Str:   "test string",
    86  		Elems: map[string]*idl.Elem{
    87  			"key1": {Ok: true},
    88  			"key2": {Ok: false},
    89  		},
    90  		Els: []*idl.Elem{{Ok: true}},
    91  	}
    92  	data, err := proto.Marshal(body)
    93  	if err != nil {
    94  		panic(err)
    95  	}
    96  	req, err := http.NewRequest(http.MethodGet, url, bytes.NewBuffer(data))
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  	customReq, err := generic.FromHTTPPbRequest(req)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	resp, err := cli.GenericCall(context.Background(), "", customReq, callopt.WithRPCTimeout(100*time.Second))
   105  	test.Assert(t, err == nil, err)
   106  	gr, ok := resp.(*generic.HTTPResponse)
   107  	test.Assert(t, ok)
   108  	test.Assert(t, gr.ContentType == descriptor.MIMEApplicationProtobuf)
   109  	pbresp := gr.GeneralBody.(*dynamic.Message)
   110  
   111  	res := &idl.Message{}
   112  	err = pbresp.ConvertTo(res)
   113  	test.Assert(t, err == nil)
   114  
   115  	test.Assert(t, body.Tiny == res.Tiny)
   116  	test.Assert(t, body.Large == res.Large)
   117  	test.Assert(t, body.Tenum == res.Tenum)
   118  	test.Assert(t, body.Str == res.Str)
   119  	test.Assert(t, body.Elems["key1"].Ok == res.Elems["key1"].Ok)
   120  	test.Assert(t, body.Elems["key2"].Ok == res.Elems["key2"].Ok)
   121  	test.Assert(t, body.Els[0].Ok == res.Els[0].Ok)
   122  
   123  	svr.Stop()
   124  }