github.com/cloudwego/kitex@v0.9.0/pkg/generic/httppbthrift_codec_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 generic
    18  
    19  import (
    20  	"bytes"
    21  	"io/ioutil"
    22  	"net/http"
    23  	"reflect"
    24  	"testing"
    25  
    26  	"github.com/bytedance/mockey"
    27  
    28  	"github.com/cloudwego/kitex/internal/test"
    29  )
    30  
    31  // TestFromHTTPPbRequest this test is to make sure mockey can work.
    32  // If it fails, please run this test with `-gcflags="all=-N -l"`
    33  func TestFromHTTPPbRequest(t *testing.T) {
    34  	mockey.PatchConvey("TestFromHTTPPbRequest", t, func() {
    35  		req, err := http.NewRequest("POST", "/far/boo", bytes.NewBuffer([]byte("321")))
    36  		test.Assert(t, err == nil)
    37  		mockey.Mock(ioutil.ReadAll).Return([]byte("123"), nil).Build()
    38  		hreq, err := FromHTTPPbRequest(req)
    39  		test.Assert(t, err == nil)
    40  		test.Assert(t, reflect.DeepEqual(hreq.RawBody, []byte("123")), string(hreq.RawBody))
    41  		test.Assert(t, hreq.GetMethod() == "POST")
    42  		test.Assert(t, hreq.GetPath() == "/far/boo")
    43  	})
    44  }