You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
5.5 KiB
207 lines
5.5 KiB
4 years ago
|
package testproto
|
||
|
|
||
|
import (
|
||
|
context "context"
|
||
4 years ago
|
"errors"
|
||
4 years ago
|
"fmt"
|
||
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/encoding"
|
||
4 years ago
|
"github.com/go-kratos/kratos/v2/metadata"
|
||
|
"github.com/go-kratos/kratos/v2/transport"
|
||
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
||
|
"github.com/go-kratos/kratos/v2/transport/http"
|
||
|
|
||
4 years ago
|
_struct "github.com/golang/protobuf/ptypes/struct"
|
||
4 years ago
|
grpcmd "google.golang.org/grpc/metadata"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
var md = metadata.Metadata{"test_key": "test_value"}
|
||
|
|
||
4 years ago
|
type echoService struct {
|
||
4 years ago
|
UnimplementedEchoServiceServer
|
||
4 years ago
|
}
|
||
|
|
||
|
func (s *echoService) Echo(ctx context.Context, m *SimpleMessage) (*SimpleMessage, error) {
|
||
4 years ago
|
md := transport.Metadata(ctx)
|
||
|
if v := md.Get("test_key"); v != "test_value" {
|
||
|
return nil, errors.New("md not match" + v)
|
||
|
}
|
||
4 years ago
|
return m, nil
|
||
|
}
|
||
|
|
||
|
func (s *echoService) EchoBody(ctx context.Context, m *SimpleMessage) (*SimpleMessage, error) {
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
|
func (s *echoService) EchoDelete(ctx context.Context, m *SimpleMessage) (*SimpleMessage, error) {
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
|
func (s *echoService) EchoPatch(ctx context.Context, m *DynamicMessageUpdate) (*DynamicMessageUpdate, error) {
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
|
func (s *echoService) EchoResponseBody(ctx context.Context, m *DynamicMessageUpdate) (*DynamicMessageUpdate, error) {
|
||
|
return m, nil
|
||
|
}
|
||
|
|
||
|
type echoClient struct {
|
||
4 years ago
|
client EchoServiceHTTPClient
|
||
4 years ago
|
}
|
||
|
|
||
|
// post: /v1/example/echo/{id}
|
||
|
func (c *echoClient) Echo(ctx context.Context, in *SimpleMessage) (out *SimpleMessage, err error) {
|
||
4 years ago
|
return c.client.Echo(ctx, in, http.Metadata(md))
|
||
4 years ago
|
}
|
||
|
|
||
|
// post: /v1/example/echo_body
|
||
|
func (c *echoClient) EchoBody(ctx context.Context, in *SimpleMessage) (out *SimpleMessage, err error) {
|
||
4 years ago
|
return c.client.EchoBody(ctx, in)
|
||
4 years ago
|
}
|
||
|
|
||
|
// delete: /v1/example/echo_delete/{id}/{num}
|
||
|
func (c *echoClient) EchoDelete(ctx context.Context, in *SimpleMessage) (out *SimpleMessage, err error) {
|
||
4 years ago
|
return c.client.EchoDelete(ctx, in)
|
||
4 years ago
|
}
|
||
|
|
||
|
// patch: /v1/example/echo_patch
|
||
|
func (c *echoClient) EchoPatch(ctx context.Context, in *DynamicMessageUpdate) (out *DynamicMessageUpdate, err error) {
|
||
4 years ago
|
return c.client.EchoPatch(ctx, in)
|
||
4 years ago
|
}
|
||
|
|
||
|
// post: /v1/example/echo_response_body
|
||
|
func (c *echoClient) EchoResponseBody(ctx context.Context, in *DynamicMessageUpdate) (out *DynamicMessageUpdate, err error) {
|
||
4 years ago
|
return c.client.EchoResponseBody(ctx, in)
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func TestJSON(t *testing.T) {
|
||
|
in := &SimpleMessage{Id: "test_id", Num: 100}
|
||
|
out := &SimpleMessage{}
|
||
|
codec := encoding.GetCodec("json")
|
||
|
data, err := codec.Marshal(in)
|
||
4 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
if err := codec.Unmarshal(data, out); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
// body
|
||
|
in2 := &DynamicMessageUpdate{Body: &DynamicMessage{
|
||
|
ValueField: &_struct.Value{Kind: &_struct.Value_StringValue{StringValue: "test"}},
|
||
|
}}
|
||
|
out2 := &DynamicMessageUpdate{}
|
||
|
data, err = codec.Marshal(&in2.Body)
|
||
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
|
if err := codec.Unmarshal(data, &out2.Body); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
func TestEchoHTTPServer(t *testing.T) {
|
||
|
echo := &echoService{}
|
||
|
ctx := context.Background()
|
||
|
srv := http.NewServer(http.Address(":2333"))
|
||
|
RegisterEchoServiceHTTPServer(srv, echo)
|
||
|
go func() {
|
||
|
if err := srv.Start(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}()
|
||
|
time.Sleep(time.Second)
|
||
|
testEchoHTTPClient(t, fmt.Sprintf("127.0.0.1:2333"))
|
||
|
srv.Stop(ctx)
|
||
|
}
|
||
|
|
||
|
func testEchoHTTPClient(t *testing.T, addr string) {
|
||
4 years ago
|
var (
|
||
|
err error
|
||
|
in = &SimpleMessage{Id: "test_id", Num: 100}
|
||
|
out = &SimpleMessage{}
|
||
|
)
|
||
4 years ago
|
check := func(name string, in, out *SimpleMessage) {
|
||
4 years ago
|
if in.Id != out.Id || in.Num != out.Num {
|
||
4 years ago
|
t.Errorf("[%s] expected %v got %v", name, in, out)
|
||
4 years ago
|
}
|
||
|
}
|
||
4 years ago
|
cc, _ := http.NewClient(context.Background(), http.WithEndpoint(addr))
|
||
4 years ago
|
|
||
4 years ago
|
cli := &echoClient{client: NewEchoServiceHTTPClient(cc)}
|
||
4 years ago
|
|
||
|
if out, err = cli.Echo(context.Background(), in); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
check("echo", &SimpleMessage{Id: "test_id"}, out)
|
||
4 years ago
|
|
||
|
if out, err = cli.EchoBody(context.Background(), in); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
check("echoBody", in, out)
|
||
4 years ago
|
|
||
|
if out, err = cli.EchoDelete(context.Background(), in); err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
check("echoDelete", in, out)
|
||
4 years ago
|
|
||
|
var (
|
||
|
din = &DynamicMessageUpdate{Body: &DynamicMessage{
|
||
|
ValueField: &_struct.Value{Kind: &_struct.Value_StringValue{StringValue: "test"}},
|
||
|
}}
|
||
|
dout *DynamicMessageUpdate
|
||
|
)
|
||
4 years ago
|
if dout, err = cli.EchoResponseBody(context.Background(), din); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
|
if din.Body.ValueField.GetStringValue() != dout.Body.ValueField.GetStringValue() {
|
||
4 years ago
|
t.Fatalf("EchoResponseBody expected %s got %s", din, dout)
|
||
4 years ago
|
}
|
||
4 years ago
|
if dout, err = cli.EchoPatch(context.Background(), din); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
if dout.Body == nil {
|
||
|
panic("dout.body is nil")
|
||
|
}
|
||
4 years ago
|
if din.Body.ValueField.GetStringValue() != dout.Body.ValueField.GetStringValue() {
|
||
4 years ago
|
t.Fatalf("EchoPatch expected %s got %s", din, dout)
|
||
4 years ago
|
}
|
||
4 years ago
|
fmt.Println("echo test success!")
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func TestEchoGRPCServer(t *testing.T) {
|
||
|
echo := &echoService{}
|
||
|
ctx := context.Background()
|
||
|
srv := grpc.NewServer(grpc.Address(":2233"))
|
||
|
RegisterEchoServiceServer(srv, echo)
|
||
|
go func() {
|
||
|
if err := srv.Start(ctx); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}()
|
||
|
time.Sleep(time.Second)
|
||
|
testEchoGRPCClient(t, fmt.Sprintf("127.0.0.1:2233"))
|
||
|
srv.Stop(ctx)
|
||
|
}
|
||
|
|
||
|
func testEchoGRPCClient(t *testing.T, addr string) {
|
||
|
ctx := context.Background()
|
||
|
cc, err := grpc.DialInsecure(ctx, grpc.WithEndpoint(addr))
|
||
4 years ago
|
if err != nil {
|
||
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
var (
|
||
|
in = &SimpleMessage{Id: "test_id", Num: 100}
|
||
|
out = &SimpleMessage{}
|
||
|
)
|
||
|
client := NewEchoServiceClient(cc)
|
||
|
ctx = grpcmd.NewOutgoingContext(ctx, grpcmd.New(md))
|
||
|
if out, err = client.Echo(ctx, in); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
4 years ago
|
if in.Id != out.Id || in.Num != out.Num {
|
||
|
t.Errorf("expected %v got %v", in, out)
|
||
|
}
|
||
4 years ago
|
}
|