Class GrpcServerReflectionHandler

java.lang.Object
org.mockserver.grpc.GrpcServerReflectionHandler

public class GrpcServerReflectionHandler extends Object
Handles gRPC Server Reflection requests without a generated proto stub. Decodes ServerReflectionRequest and encodes ServerReflectionResponse manually using protobuf's CodedInputStream/CodedOutputStream, mirroring the approach used by GrpcHealthCheckHandler.

Supports both the v1 and v1alpha reflection service paths:

  • /grpc.reflection.v1.ServerReflection/ServerReflectionInfo
  • /grpc.reflection.v1alpha.ServerReflection/ServerReflectionInfo

Canonical field numbers (from grpc/reflection/v1/reflection.proto)

ServerReflectionRequest:
   field 1 = host (string)
   field 3 = file_by_filename (string)        [oneof message_request]
   field 4 = file_containing_symbol (string)   [oneof message_request]
   field 7 = list_services (string)            [oneof message_request]
 
ServerReflectionResponse:
   field 1 = valid_host (string)
   field 2 = original_request (ServerReflectionRequest)
   field 4 = file_descriptor_response (FileDescriptorResponse)  [oneof message_response]
   field 6 = list_services_response (ListServiceResponse)       [oneof message_response]
 
FileDescriptorResponse:
   field 1 = file_descriptor_proto (repeated bytes)
 
ListServiceResponse:
   field 1 = service (repeated ServiceResponse)
 
ServiceResponse:
   field 1 = name (string)
 

Limitation

The current gRPC path in MockServer is buffered-unary: each HTTP/2 request carries exactly one gRPC message. This implementation therefore handles a single ServerReflectionRequest per HTTP/2 request, which is sufficient for tools like grpcurl list and single symbol/file lookups. Fully-interactive bidi-streaming reflection (a long-lived stream with multiple back-and-forth messages) is not supported by the buffered pipeline.

  • Field Details

  • Constructor Details

  • Method Details

    • isReflectionRequest

      public boolean isReflectionRequest(String path)
      Returns true if the given path matches either the v1 or v1alpha gRPC Server Reflection method path.
    • isReflectionPath

      public static boolean isReflectionPath(String path)
      Static variant of isReflectionRequest(String) for use from routing code that does not hold a handler instance.
    • handleReflectionRequest

      public byte[] handleReflectionRequest(byte[] grpcFramedBody)
      Processes a gRPC-framed ServerReflectionRequest and returns a gRPC-framed ServerReflectionResponse.
      Parameters:
      grpcFramedBody - the raw gRPC-framed request bytes (5-byte header + protobuf)
      Returns:
      gRPC-framed response bytes
    • grpcFrame

      public static byte[] grpcFrame(byte[] proto)
      Wraps a protobuf message in a gRPC frame (5-byte header: 1 byte compression flag + 4-byte big-endian message length).