1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15+ import collections
1516import pytest
1617
1718from google .showcase import EchoClient
@@ -34,3 +35,54 @@ def identity():
3435 channel = grpc .insecure_channel ('localhost:7469' ),
3536 )
3637 return IdentityClient (transport = transport )
38+
39+
40+ class MetadataClientInterceptor (grpc .UnaryUnaryClientInterceptor ,
41+ grpc .UnaryStreamClientInterceptor ,
42+ grpc .StreamUnaryClientInterceptor ,
43+ grpc .StreamStreamClientInterceptor ):
44+
45+ def __init__ (self , key , value ):
46+ self ._key = key
47+ self ._value = value
48+
49+ def _add_metadata (self , client_call_details ):
50+ if client_call_details .metadata is not None :
51+ client_call_details .metadata .append ((self ._key , self ._value ,))
52+
53+ def intercept_unary_unary (self , continuation , client_call_details , request ):
54+ self ._add_metadata (client_call_details )
55+ response = continuation (client_call_details , request )
56+ return response
57+
58+ def intercept_unary_stream (self , continuation , client_call_details ,
59+ request ):
60+ self ._add_metadata (client_call_details )
61+ response_it = continuation (client_call_details , request )
62+ return response_it
63+
64+ def intercept_stream_unary (self , continuation , client_call_details ,
65+ request_iterator ):
66+ self ._add_metadata (client_call_details )
67+ response = continuation (client_call_details , request_iterator )
68+ return response
69+
70+ def intercept_stream_stream (self , continuation , client_call_details ,
71+ request_iterator ):
72+ self ._add_metadata (client_call_details )
73+ response_it = continuation (client_call_details , request_iterator )
74+ return response_it
75+
76+
77+ @pytest .fixture
78+ def intercepted_echo ():
79+ # The interceptor adds 'showcase-trailer' client metadata. Showcase server
80+ # echos any metadata with key 'showcase-trailer', so the same metadata
81+ # should appear as trailing metadata in the response.
82+ interceptor = MetadataClientInterceptor ('showcase-trailer' , 'intercepted' )
83+ channel = grpc .insecure_channel ('localhost:7469' )
84+ intercept_channel = grpc .intercept_channel (channel , interceptor )
85+ transport = EchoClient .get_transport_class ('grpc' )(
86+ channel = intercept_channel ,
87+ )
88+ return EchoClient (transport = transport )
0 commit comments