11from datetime import timedelta
2- from typing import Mapping
2+ from typing import Mapping , Any , cast
3+ import pytest
4+ import re
35
46from google .protobuf .empty_pb2 import Empty
57from google .protobuf .timestamp_pb2 import Timestamp
@@ -153,7 +155,7 @@ async def test_grpc_metadata():
153155 # Binary metadata should be configurable on the client:
154156 client .rpc_metadata = {
155157 "my-binary-key-bin" : b"\x00 \x01 " ,
156- "my-binary-key-bin2 " : b"\x02 \x03 " ,
158+ "my-binary-key2-bin " : b"\x02 \x03 " ,
157159 }
158160 await client .workflow_service .get_system_info (
159161 GetSystemInfoRequest (),
@@ -165,7 +167,100 @@ async def test_grpc_metadata():
165167 {
166168 "authorization" : "Bearer my-api-key" ,
167169 "my-binary-key-bin" : b"abc" ,
168- "my-binary-key-bin2" : b"\x02 \x03 " ,
170+ "my-binary-key2-bin" : b"\x02 \x03 " ,
171+ }
172+ )
173+
174+ # Setting invalid RPC metadata should raise:
175+ with pytest .raises (
176+ ValueError ,
177+ match = "Invalid binary header key 'my-ascii-key': invalid gRPC metadata key name" ,
178+ ):
179+ client .rpc_metadata = {
180+ "my-ascii-key" : b"binary-value" ,
181+ }
182+ with pytest .raises (
183+ ValueError ,
184+ match = "Invalid ASCII header key 'my-binary-key-bin': invalid gRPC metadata key name" ,
185+ ):
186+ client .rpc_metadata = {
187+ "my-binary-key-bin" : "ascii-value" ,
188+ }
189+
190+ # Making a request with invalid RPC metadata should raise:
191+ with pytest .raises (
192+ ValueError ,
193+ match = "Invalid metadata value for ASCII key my-ascii-key: expected str" ,
194+ ):
195+ await client .workflow_service .get_system_info (
196+ GetSystemInfoRequest (),
197+ metadata = {
198+ "my-ascii-key" : b"binary-value" ,
199+ },
200+ )
201+ with pytest .raises (
202+ ValueError ,
203+ match = "Invalid metadata value for binary key my-binary-key-bin: expected bytes" ,
204+ ):
205+ await client .workflow_service .get_system_info (
206+ GetSystemInfoRequest (),
207+ metadata = {
208+ "my-binary-key-bin" : "ascii-value" ,
209+ },
210+ )
211+
212+ # Passing in non-`str | bytes` should raise:
213+ with pytest .raises (TypeError ) as err :
214+ await client .workflow_service .get_system_info (
215+ GetSystemInfoRequest (),
216+ metadata = {
217+ # Not a valid header:
218+ "my-int-key" : cast (Any , 256 ),
219+ },
220+ )
221+ cause = err .value .__cause__
222+ assert isinstance (cause , TypeError )
223+ assert re .match (
224+ re .escape (r"failed to extract enum RpcMetadataValue ('str | bytes')" ),
225+ str (cause ),
226+ )
227+ with pytest .raises (
228+ TypeError ,
229+ match = re .escape (r"failed to extract enum RpcMetadataValue ('str | bytes')" ),
230+ ) as err :
231+ client .rpc_metadata = {
232+ "my-binary-key-bin" : cast (Any , 256 ),
233+ }
234+
235+ # Setting invalid RPC metadata in a mixed client will partially fail:
236+ client .rpc_metadata = {
237+ "x-my-binary-bin" : b"\x00 " ,
238+ "x-my-ascii" : "foo" ,
239+ }
240+ assert client .rpc_metadata == {
241+ "x-my-binary-bin" : b"\x00 " ,
242+ "x-my-ascii" : "foo" ,
243+ }
244+ with pytest .raises (
245+ ValueError ,
246+ match = "Invalid binary header key 'x-invalid-ascii-with-bin-value': invalid gRPC metadata key name" ,
247+ ):
248+ client .rpc_metadata = {
249+ "x-invalid-ascii-with-bin-value" : b"not-ascii" ,
250+ "x-my-ascii" : "bar" ,
251+ }
252+ assert client .rpc_metadata == {
253+ "x-my-binary-bin" : b"\x00 " ,
254+ "x-my-ascii" : "foo" ,
255+ }
256+ await client .workflow_service .get_system_info (GetSystemInfoRequest ())
257+ workflow_server .assert_last_metadata (
258+ {
259+ "authorization" : "Bearer my-api-key" ,
260+ # This is inconsistent with what `client.rpc_metadata` returns
261+ # (`x-my-ascii` was updated):
262+ "x-my-binary-bin" : b"\x00 " ,
263+ "x-my-ascii" : "bar" ,
169264 }
170265 )
171266
0 commit comments