server: add grpc.Method function for extracting method from context (#1961)

This commit is contained in:
dfawley
2018-04-02 13:08:04 -07:00
committed by GitHub
parent d354447dc6
commit 2eae9d0c74
3 changed files with 39 additions and 5 deletions

View File

@ -1359,3 +1359,13 @@ func SetTrailer(ctx context.Context, md metadata.MD) error {
}
return stream.SetTrailer(md)
}
// Method returns the method string for the server context. The returned
// string is in the format of "/service/method".
func Method(ctx context.Context) (string, bool) {
s := serverTransportStreamFromContext(ctx)
if s == nil {
return "", false
}
return s.Method(), true
}

View File

@ -732,9 +732,5 @@ func (ss *serverStream) RecvMsg(m interface{}) (err error) {
// MethodFromServerStream returns the method string for the input stream.
// The returned string is in the format of "/service/method".
func MethodFromServerStream(stream ServerStream) (string, bool) {
s := serverTransportStreamFromContext(stream.Context())
if s == nil {
return "", false
}
return s.Method(), true
return Method(stream.Context())
}

View File

@ -4868,6 +4868,34 @@ func (ss *stubServer) Stop() {
}
}
func TestGRPCMethod(t *testing.T) {
defer leakcheck.Check(t)
var method string
var ok bool
ss := &stubServer{
emptyCall: func(ctx context.Context, in *testpb.Empty) (*testpb.Empty, error) {
method, ok = grpc.Method(ctx)
return &testpb.Empty{}, nil
},
}
if err := ss.Start(nil); err != nil {
t.Fatalf("Error starting endpoint server: %v", err)
}
defer ss.Stop()
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if _, err := ss.client.EmptyCall(ctx, &testpb.Empty{}); err != nil {
t.Fatalf("ss.client.EmptyCall(_, _) = _, %v; want _, nil", err)
}
if want := "/grpc.testing.TestService/EmptyCall"; !ok || method != want {
t.Fatalf("grpc.Method(_) = %q, %v; want %q, true", method, ok, want)
}
}
func TestUnaryProxyDoesNotForwardMetadata(t *testing.T) {
const mdkey = "somedata"