@@ -119,6 +119,21 @@ def test_invoke_wrapped_method_with_metadata_as_none():
119119 assert len (metadata ) == 1
120120
121121
122+ def test_extract_metrics_header_duplicate_tokens ():
123+ metadata = [
124+ ("x-goog-api-client" , "token1 token2" ),
125+ ("x-goog-api-client" , "token2 token3 token1" ),
126+ ("other-header" , "value" ),
127+ ("x-goog-api-client" , "token4 token2" ),
128+ ]
129+
130+ metric_str , arbitrary_metadata = google .api_core .gapic_v1 .method ._extract_metrics_header (metadata )
131+
132+ # Should maintain order of first appearance and eliminate duplicates
133+ assert metric_str == "token1 token2 token3 token4"
134+ assert arbitrary_metadata == [("other-header" , "value" )]
135+
136+
122137def test_invoke_wrapped_method_with_duplicate_x_goog_api_client_metadata ():
123138 method = mock .Mock (spec = ["__call__" ])
124139
@@ -134,11 +149,13 @@ def test_invoke_wrapped_method_with_duplicate_x_goog_api_client_metadata():
134149 method , client_info = client_info
135150 )
136151
137- # Invoke the wrapped method with an explicit user-provided custom header
152+ # Invoke the wrapped method with an explicit user-provided custom header that contains duplicates
153+ # both within its own items and overlapping with the default client_info
138154 wrapped_method (
139155 mock .sentinel .request ,
140156 metadata = [
141157 ("x-goog-api-client" , "override-client/2.0" ),
158+ ("x-goog-api-client" , "override-client/2.0 grpc/1.76.0 custom-user-agent/1.0" ),
142159 ("other-header" , "value" ),
143160 ],
144161 )
@@ -156,7 +173,7 @@ def test_invoke_wrapped_method_with_duplicate_x_goog_api_client_metadata():
156173 # Verify both the user-provided override value and the library system telemetry are merged explicitly
157174 assert (
158175 metadata_dict ["x-goog-api-client" ]
159- == "override-client/2.0 custom-user-agent/1.0 gl-python/3.14.0 grpc/1.76.0 gax/2.29.0"
176+ == "custom-user-agent/1.0 gl-python/3.14.0 grpc/1.76.0 gax/2.29.0 override-client/2 .0"
160177 )
161178
162179
@@ -289,3 +306,24 @@ def test_wrap_method_with_call_not_supported():
289306 with pytest .raises (ValueError ) as exc_info :
290307 google .api_core .gapic_v1 .method .wrap_method (method , with_call = True )
291308 assert "with_call=True is only supported for unary calls" in str (exc_info .value )
309+
310+
311+ @pytest .mark .parametrize (
312+ "headers,expected" ,
313+ [
314+ ((), "" ),
315+ (("" ,), "" ),
316+ ((None ,), "" ),
317+ (("" , None , "" ), "" ),
318+ (("token1" ,), "token1" ),
319+ (("token1 token1" ,), "token1" ),
320+ (("token1" , "token1" ), "token1" ),
321+ (("token1 token2 token1" ,), "token1 token2" ),
322+ (("token1" , "token2" , "token1" ), "token1 token2" ),
323+ (("token1 token2" , "token2 token3" ), "token1 token2 token3" ),
324+ (("token1" , None , "token2" , "" , "token1" ), "token1 token2" ),
325+ ],
326+ )
327+ def test__deduplicate_metadata_tokens (headers , expected ):
328+ dedup = google .api_core .gapic_v1 .method ._deduplicate_metadata_tokens
329+ assert dedup (* headers ) == expected
0 commit comments