@@ -5284,6 +5284,53 @@ func (s) TestGRPCMethod(t *testing.T) {
52845284 }
52855285}
52865286
5287+ // renameProtoCodec is an encoding.Codec wrapper that allows customizing the
5288+ // Name() of another codec.
5289+ type renameProtoCodec struct {
5290+ encoding.Codec
5291+ name string
5292+ }
5293+
5294+ func (r * renameProtoCodec ) Name () string { return r .name }
5295+
5296+ func (s ) TestForceCodecName (t * testing.T ) {
5297+ wantContentTypeCh := make (chan string , 1 )
5298+ defer close (wantContentTypeCh )
5299+
5300+ ss := & stubserver.StubServer {
5301+ EmptyCallF : func (ctx context.Context , in * testpb.Empty ) (* testpb.Empty , error ) {
5302+ md , ok := metadata .FromIncomingContext (ctx )
5303+ if ! ok {
5304+ return nil , status .Errorf (codes .Internal , "no metadata in context" )
5305+ }
5306+ if got , want := md ["content-type" ], <- wantContentTypeCh ; len (got ) != 1 || got [0 ] != want {
5307+ return nil , status .Errorf (codes .Internal , "got content-type=%q; want [%q]" , got , want )
5308+ }
5309+ return & testpb.Empty {}, nil
5310+ },
5311+ }
5312+ protoCodec := encoding .GetCodec ("proto" )
5313+ if err := ss .Start ([]grpc.ServerOption {grpc .ForceServerCodec (encoding .GetCodec ("proto" ))}); err != nil {
5314+ t .Fatalf ("Error starting endpoint server: %v" , err )
5315+ }
5316+ defer ss .Stop ()
5317+
5318+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
5319+ defer cancel ()
5320+
5321+ codec := & renameProtoCodec {Codec : protoCodec , name : "some-test-name" }
5322+ wantContentTypeCh <- "application/grpc+some-test-name"
5323+ if _ , err := ss .Client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (codec )); err != nil {
5324+ t .Fatalf ("ss.Client.EmptyCall(_, _) = _, %v; want _, nil" , err )
5325+ }
5326+
5327+ codec .name = "aNoTHeRNaME"
5328+ wantContentTypeCh <- "application/grpc+anothername"
5329+ if _ , err := ss .Client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (codec )); err != nil {
5330+ t .Fatalf ("ss.Client.EmptyCall(_, _) = _, %v; want _, nil" , err )
5331+ }
5332+ }
5333+
52875334func (s ) TestForceServerCodec (t * testing.T ) {
52885335 ss := & stubserver.StubServer {
52895336 EmptyCallF : func (ctx context.Context , in * testpb.Empty ) (* testpb.Empty , error ) {
0 commit comments