server: add grpc.Method function for extracting method from context (#1961)
This commit is contained in:
10
server.go
10
server.go
@ -1359,3 +1359,13 @@ func SetTrailer(ctx context.Context, md metadata.MD) error {
|
|||||||
}
|
}
|
||||||
return stream.SetTrailer(md)
|
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
|
||||||
|
}
|
||||||
|
@ -732,9 +732,5 @@ func (ss *serverStream) RecvMsg(m interface{}) (err error) {
|
|||||||
// MethodFromServerStream returns the method string for the input stream.
|
// MethodFromServerStream returns the method string for the input stream.
|
||||||
// The returned string is in the format of "/service/method".
|
// The returned string is in the format of "/service/method".
|
||||||
func MethodFromServerStream(stream ServerStream) (string, bool) {
|
func MethodFromServerStream(stream ServerStream) (string, bool) {
|
||||||
s := serverTransportStreamFromContext(stream.Context())
|
return Method(stream.Context())
|
||||||
if s == nil {
|
|
||||||
return "", false
|
|
||||||
}
|
|
||||||
return s.Method(), true
|
|
||||||
}
|
}
|
||||||
|
@ -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) {
|
func TestUnaryProxyDoesNotForwardMetadata(t *testing.T) {
|
||||||
const mdkey = "somedata"
|
const mdkey = "somedata"
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user