1- // Copyright 2019 The OpenZipkin Authors
1+ // Copyright 2020 The OpenZipkin Authors
22//
33// Licensed under the Apache License, Version 2.0 (the "License");
44// you may not use this file except in compliance with the License.
@@ -77,30 +77,56 @@ func TestGRPCServerCreatesASpanAndContext(t *testing.T) {
7777 }
7878}
7979
80- func TestGRPCServerCanAccessToHeaders (t * testing.T ) {
80+ func TestGRPCServerCanAccessToPayloadAndMetadata (t * testing.T ) {
8181 tracer , flusher := createTracer (false )
8282
8383 s := grpc .NewServer (
8484 grpc .StatsHandler (
8585 zipkingrpc .NewServerHandler (
8686 tracer ,
8787 zipkingrpc .ServerTags (map [string ]string {"default" : "tag" }),
88- zipkingrpc .WithServerInHeaderParser (func (inHeader * stats.InHeader , span zipkin.Span ) {
88+ zipkingrpc .WithServerInPayloadParser (func (inPayload * stats.InPayload , span zipkin.SpanCustomizer ) {
89+ m , ok := inPayload .Payload .(* service.HelloRequest )
90+ if ! ok {
91+ t .Fatal ("failed to cast the payload as a service.HelloRequest" )
92+ }
93+ if want , have := "Hello" , m .Payload ; want != have {
94+ t .Errorf ("incorrect payload: want %q, have %q" , want , have )
95+ }
96+ }),
97+ zipkingrpc .WithServerInHeaderParser (func (inHeader * stats.InHeader , span zipkin.SpanCustomizer ) {
8998 if want , have := "test_value" , inHeader .Header .Get ("test_key" )[0 ]; want != have {
90- t .Errorf ("unexpected metadata value in header, want: %q, have %q" , want , have )
99+ t .Errorf ("incorrect header value, want %q, have %q" , want , have )
100+ }
101+ }),
102+ zipkingrpc .WithServerOutPayloadParser (func (outPayload * stats.OutPayload , span zipkin.SpanCustomizer ) {
103+ m , ok := outPayload .Payload .(* service.HelloResponse )
104+ if ! ok {
105+ t .Fatal ("failed to cast the payload as a service.HelloResponse" )
106+ }
107+ if want , have := "World" , m .Payload ; want != have {
108+ t .Errorf ("incorrect payload: want %q, have %q" , want , have )
109+ }
110+ }),
111+ zipkingrpc .WithServerOutHeaderParser (func (outHeader * stats.OutHeader , span zipkin.SpanCustomizer ) {
112+ if want , have := "test_value_1" , outHeader .Header .Get ("test_key" )[0 ]; want != have {
113+ t .Errorf ("incorrect header value, want %q, have %q" , want , have )
91114 }
92115 }),
93- zipkingrpc .WithServerInTrailerParser (func (inTrailer * stats.InTrailer , span zipkin.Span ) {
94- if want , have := "test_value " , inTrailer .Trailer .Get ("test_key" )[0 ]; want != have {
95- t .Errorf ("unexpected metadata value in header , want: %q, have %q" , want , have )
116+ zipkingrpc .WithServerOutTrailerParser (func (outTrailer * stats.OutTrailer , span zipkin.SpanCustomizer ) {
117+ if want , have := "test_value_2 " , outTrailer .Trailer .Get ("test_key" )[0 ]; want != have {
118+ t .Errorf ("incorrect trailer value, want %q, have %q" , want , have )
96119 }
97120 }),
98121 ),
99122 ),
100123 )
101124 defer s .Stop ()
102125
103- service .RegisterHelloServiceServer (s , & TestHelloService {})
126+ service .RegisterHelloServiceServer (s , & TestHelloService {
127+ responseHeader : metadata .Pairs ("test_key" , "test_value_1" ),
128+ responseTrailer : metadata .Pairs ("test_key" , "test_value_2" ),
129+ })
104130
105131 dialer := initListener (s )
106132
@@ -118,7 +144,7 @@ func TestGRPCServerCanAccessToHeaders(t *testing.T) {
118144
119145 client := service .NewHelloServiceClient (conn )
120146
121- ctx = metadata .AppendToOutgoingContext (ctx , "test_key" , "test_value" )
147+ ctx = metadata .NewOutgoingContext (ctx , metadata . Pairs ( "test_key" , "test_value" ) )
122148 _ , err = client .Hello (ctx , & service.HelloRequest {
123149 Payload : "Hello" ,
124150 })
@@ -130,9 +156,4 @@ func TestGRPCServerCanAccessToHeaders(t *testing.T) {
130156 if want , have := 1 , len (spans ); want != have {
131157 t .Errorf ("unexpected number of spans, want %d, have %d" , want , have )
132158 }
133-
134- span := spans [0 ]
135- if want , have := model .Server , span .Kind ; want != have {
136- t .Errorf ("unexpected kind, want %q, have %q" , want , have )
137- }
138159}
0 commit comments