Created
January 30, 2019 21:22
-
-
Save bojand/dc4577ebf7191e16e454e6312be2eb97 to your computer and use it in GitHub Desktop.
gRPC Reflection Client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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