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.
239 lines
6.5 KiB
239 lines
6.5 KiB
3 years ago
|
package echo
|
||
4 years ago
|
|
||
|
import (
|
||
|
context "context"
|
||
4 years ago
|
"errors"
|
||
4 years ago
|
"fmt"
|
||
3 years ago
|
stdhttp "net/http"
|
||
4 years ago
|
"testing"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-kratos/kratos/v2/encoding"
|
||
4 years ago
|
"github.com/go-kratos/kratos/v2/metadata"
|
||
4 years ago
|
mmd "github.com/go-kratos/kratos/v2/middleware/metadata"
|
||
3 years ago
|
"github.com/go-kratos/kratos/v2/transport"
|
||
4 years ago
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
||
|
"github.com/go-kratos/kratos/v2/transport/http"
|
||
|
|
||
3 years ago
|
stdgrpc "google.golang.org/grpc"
|
||
|
grpcmd "google.golang.org/grpc/metadata"
|
||
3 years ago
|
_struct "google.golang.org/protobuf/types/known/structpb"
|
||
4 years ago
|
)
|
||
|
|
||
4 years ago
|
var md = metadata.Metadata{"x-md-global-test": "test_value"}
|
||
4 years ago
|
|
||
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, _ := metadata.FromServerContext(ctx)
|
||
|
if v := md.Get("x-md-global-test"); v != "test_value" {
|
||
4 years ago
|
return nil, errors.New("md not match" + v)
|
||
|
}
|
||
3 years ago
|
|
||
|
if tr, ok := transport.FromServerContext(ctx); ok {
|
||
|
tr.ReplyHeader().Set("2233", "niang")
|
||
|
}
|
||
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}
|
||
3 years ago
|
func (c *echoClient) Echo(ctx context.Context, in *SimpleMessage, opts ...http.CallOption) (out *SimpleMessage, err error) {
|
||
|
return c.client.Echo(ctx, in, opts...)
|
||
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()
|
||
4 years ago
|
srv := http.NewServer(
|
||
|
http.Address(":2333"),
|
||
|
http.Middleware(mmd.Server()),
|
||
|
)
|
||
4 years ago
|
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
|
||
3 years ago
|
in = &SimpleMessage{Id: "test_id", Num: 100, Ext: &SimpleMessage_No{
|
||
|
No: &Embedded{Mark: &Embedded_Note{Note: "2233"}},
|
||
|
}}
|
||
4 years ago
|
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),
|
||
|
http.WithMiddleware(mmd.Client()),
|
||
|
)
|
||
4 years ago
|
|
||
4 years ago
|
cli := &echoClient{client: NewEchoServiceHTTPClient(cc)}
|
||
4 years ago
|
|
||
4 years ago
|
ctx := context.Background()
|
||
|
ctx = metadata.NewClientContext(ctx, md)
|
||
3 years ago
|
var header stdhttp.Header
|
||
|
if out, err = cli.Echo(ctx, in, http.Header(&header)); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
3 years ago
|
if header.Get("2233") != "niang" {
|
||
|
t.Errorf("[echo] header key 2233 expected niang got %v", header.Get("2233"))
|
||
|
}
|
||
3 years ago
|
check("echo", &SimpleMessage{Id: "test_id", Num: 100, Ext: &SimpleMessage_No{No: &Embedded{Mark: &Embedded_Note{Note: "2233"}}}}, 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()
|
||
4 years ago
|
srv := grpc.NewServer(
|
||
|
grpc.Address(":2233"),
|
||
|
grpc.Middleware(mmd.Server()),
|
||
|
)
|
||
4 years ago
|
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) {
|
||
4 years ago
|
cc, err := grpc.DialInsecure(
|
||
|
context.Background(),
|
||
|
grpc.WithEndpoint(addr),
|
||
|
grpc.WithMiddleware(mmd.Client()),
|
||
|
)
|
||
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)
|
||
4 years ago
|
ctx := context.Background()
|
||
|
ctx = metadata.NewClientContext(ctx, md)
|
||
3 years ago
|
var md grpcmd.MD
|
||
|
if out, err = client.Echo(ctx, in, stdgrpc.Header(&md)); err != nil {
|
||
4 years ago
|
t.Fatal(err)
|
||
|
}
|
||
3 years ago
|
if len(md.Get("2233")) != 1 || md.Get("2233")[0] != "niang" {
|
||
|
t.Errorf("[echo] header key 2233 expected niang got %v", md.Get("2233"))
|
||
|
}
|
||
4 years ago
|
if in.Id != out.Id || in.Num != out.Num {
|
||
|
t.Errorf("expected %v got %v", in, out)
|
||
|
}
|
||
4 years ago
|
}
|