@@ -5293,7 +5293,7 @@ func (s) TestGRPCMethod(t *testing.T) {
5293
5293
}
5294
5294
defer ss .Stop ()
5295
5295
5296
- ctx , cancel := context .WithTimeout (context .Background (), 2 * time . Second )
5296
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
5297
5297
defer cancel ()
5298
5298
5299
5299
if _ , err := ss .Client .EmptyCall (ctx , & testpb.Empty {}); err != nil {
@@ -5305,6 +5305,55 @@ func (s) TestGRPCMethod(t *testing.T) {
5305
5305
}
5306
5306
}
5307
5307
5308
+ // renameProtoCodec is an encoding.Codec wrapper that allows customizing the
5309
+ // Name() of another codec.
5310
+ type renameProtoCodec struct {
5311
+ encoding.Codec
5312
+ name string
5313
+ }
5314
+
5315
+ func (r * renameProtoCodec ) Name () string { return r .name }
5316
+
5317
+ // TestForceCodecName confirms that the ForceCodec call option sets the subtype
5318
+ // in the content-type header according to the Name() of the codec provided.
5319
+ func (s ) TestForceCodecName (t * testing.T ) {
5320
+ wantContentTypeCh := make (chan []string , 1 )
5321
+ defer close (wantContentTypeCh )
5322
+
5323
+ ss := & stubserver.StubServer {
5324
+ EmptyCallF : func (ctx context.Context , in * testpb.Empty ) (* testpb.Empty , error ) {
5325
+ md , ok := metadata .FromIncomingContext (ctx )
5326
+ if ! ok {
5327
+ return nil , status .Errorf (codes .Internal , "no metadata in context" )
5328
+ }
5329
+ if got , want := md ["content-type" ], <- wantContentTypeCh ; ! reflect .DeepEqual (got , want ) {
5330
+ return nil , status .Errorf (codes .Internal , "got content-type=%q; want [%q]" , got , want )
5331
+ }
5332
+ return & testpb.Empty {}, nil
5333
+ },
5334
+ }
5335
+ if err := ss .Start ([]grpc.ServerOption {grpc .ForceServerCodec (encoding .GetCodec ("proto" ))}); err != nil {
5336
+ t .Fatalf ("Error starting endpoint server: %v" , err )
5337
+ }
5338
+ defer ss .Stop ()
5339
+
5340
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
5341
+ defer cancel ()
5342
+
5343
+ codec := & renameProtoCodec {Codec : encoding .GetCodec ("proto" ), name : "some-test-name" }
5344
+ wantContentTypeCh <- []string {"application/grpc+some-test-name" }
5345
+ if _ , err := ss .Client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (codec )); err != nil {
5346
+ t .Fatalf ("ss.Client.EmptyCall(_, _) = _, %v; want _, nil" , err )
5347
+ }
5348
+
5349
+ // Confirm the name is converted to lowercase before transmitting.
5350
+ codec .name = "aNoTHeRNaME"
5351
+ wantContentTypeCh <- []string {"application/grpc+anothername" }
5352
+ if _ , err := ss .Client .EmptyCall (ctx , & testpb.Empty {}, grpc .ForceCodec (codec )); err != nil {
5353
+ t .Fatalf ("ss.Client.EmptyCall(_, _) = _, %v; want _, nil" , err )
5354
+ }
5355
+ }
5356
+
5308
5357
func (s ) TestForceServerCodec (t * testing.T ) {
5309
5358
ss := & stubserver.StubServer {
5310
5359
EmptyCallF : func (ctx context.Context , in * testpb.Empty ) (* testpb.Empty , error ) {
@@ -5317,7 +5366,7 @@ func (s) TestForceServerCodec(t *testing.T) {
5317
5366
}
5318
5367
defer ss .Stop ()
5319
5368
5320
- ctx , cancel := context .WithTimeout (context .Background (), 2 * time . Second )
5369
+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
5321
5370
defer cancel ()
5322
5371
5323
5372
if _ , err := ss .Client .EmptyCall (ctx , & testpb.Empty {}); err != nil {
0 commit comments