Skip to content

Commit 517c45b

Browse files
add test cases for interceptor (#282)
add test cases for interceptor
1 parent f7071e4 commit 517c45b

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

packages/gapic-generator/tests/system/conftest.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import collections
1516
import pytest
1617

1718
from 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)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from google import showcase
16+
17+
18+
# intercetped_metadata will be added by the interceptor automatically, and
19+
# showcase server will echo it (since it has key 'showcase-trailer') as trailing
20+
# metadata.
21+
intercepted_metadata = (('showcase-trailer', 'intercepted'),)
22+
23+
24+
def test_unary_stream(intercepted_echo):
25+
content = 'The hail in Wales falls mainly on the snails.'
26+
responses = intercepted_echo.expand({
27+
'content': content,
28+
})
29+
30+
for ground_truth, response in zip(content.split(' '), responses):
31+
assert response.content == ground_truth
32+
assert ground_truth == 'snails.'
33+
34+
assert responses.trailing_metadata() == intercepted_metadata
35+
36+
37+
def test_stream_stream(intercepted_echo):
38+
requests = []
39+
requests.append(showcase.EchoRequest(content="hello"))
40+
requests.append(showcase.EchoRequest(content="world!"))
41+
responses = intercepted_echo.chat(iter(requests))
42+
43+
contents = [response.content for response in responses]
44+
assert contents == ['hello', 'world!']
45+
46+
assert responses.trailing_metadata() == intercepted_metadata

0 commit comments

Comments
 (0)