MCPcopy
hub / github.com/grpc/grpc-go / TestForceCodecName

Method TestForceCodecName

encoding/encoding_test.go:339–374  ·  encoding/encoding_test.go::s.TestForceCodecName

TestForceCodecName confirms that the ForceCodec call option sets the subtype in the content-type header according to the Name() of the codec provided. Verifies that the name is converted to lowercase before transmitting.

(t *testing.T)

Source from the content-addressed store, hash-verified

337// in the content-type header according to the Name() of the codec provided.
338// Verifies that the name is converted to lowercase before transmitting.
339func (s) TestForceCodecName(t *testing.T) {
340 wantContentTypeCh := make(chan []string, 1)
341 defer close(wantContentTypeCh)
342
343 // Create a test service backend that pushes the received content-type on a
344 // channel for the test to inspect.
345 ss := &stubserver.StubServer{
346 EmptyCallF: func(ctx context.Context, _ *testpb.Empty) (*testpb.Empty, error) {
347 md, ok := metadata.FromIncomingContext(ctx)
348 if !ok {
349 return nil, status.Errorf(codes.Internal, "no metadata in context")
350 }
351 if got, want := md["content-type"], <-wantContentTypeCh; !cmp.Equal(got, want) {
352 return nil, status.Errorf(codes.Internal, "got content-type=%q; want [%q]", got, want)
353 }
354 return &testpb.Empty{}, nil
355 },
356 }
357 // Since we don't specify a codec as a server option, it will end up
358 // automatically using the proto codec.
359 if err := ss.Start(nil); err != nil {
360 t.Fatalf("Error starting endpoint server: %v", err)
361 }
362 defer ss.Stop()
363
364 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout)
365 defer cancel()
366
367 // Force the use of the custom codec on the client with the ForceCodec call
368 // option. Confirm the name is converted to lowercase before transmitting.
369 codec := &renameProtoCodec{CodecV2: encoding.GetCodecV2(proto.Name), name: t.Name()}
370 wantContentTypeCh <- []string{fmt.Sprintf("application/grpc+%s", strings.ToLower(t.Name()))}
371 if _, err := ss.Client.EmptyCall(ctx, &testpb.Empty{}, grpc.ForceCodecV2(codec)); err != nil {
372 t.Fatalf("ss.Client.EmptyCall(_, _) = _, %v; want _, nil", err)
373 }
374}
375
376// Tests the case where the client uses a codec (one that uses proto encoding
377// but uses a different content-subtype name) that the server does not support.

Callers

nothing calls this directly

Calls 10

StartMethod · 0.95
StopMethod · 0.95
FromIncomingContextFunction · 0.92
ErrorfFunction · 0.92
GetCodecV2Function · 0.92
ForceCodecV2Function · 0.92
EqualMethod · 0.65
FatalfMethod · 0.65
NameMethod · 0.65
EmptyCallMethod · 0.65

Tested by

no test coverage detected