Skip to content

Instantly share code, notes, and snippets.

@bojand
Created January 30, 2019 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bojand/dc4577ebf7191e16e454e6312be2eb97 to your computer and use it in GitHub Desktop.
Save bojand/dc4577ebf7191e16e454e6312be2eb97 to your computer and use it in GitHub Desktop.
gRPC Reflection Client
func TestRunReflection(t *testing.T) {
var cc *grpc.ClientConn
var opts []grpc.DialOption
var mtd *desc.MethodDescriptor
opts = append(opts, grpc.WithInsecure())
dialCtx := context.Background()
cc, err = grpc.DialContext(dialCtx, "host", opts...)
if err != nil {
assert.FailNow(t, err.Error())
}
refCtx := context.Background()
refClient := grpcreflect.NewClient(refCtx, reflectpb.NewServerReflectionClient(cc))
mtd, err = protodesc.GetMethodDescFromReflect("helloworld.Greeter.SayHello", refClient)
if err != nil {
assert.FailNow(t, err.Error())
}
stub := grpcdynamic.NewStub(cc)
var input *dynamic.Message
md := mtd.GetInputType()
input = dynamic.NewMessage(md)
err = jsonpb.UnmarshalString(`{"name":"Bob"}`, input)
if err != nil {
assert.FailNow(t, err.Error())
}
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
res, err := stub.InvokeRpc(ctx, mtd, input)
if err != nil {
assert.FailNow(t, err.Error())
}
fmt.Printf("%+v\n\n", res)
assert.False(t, true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment