@@ -121,6 +121,39 @@ def test_invoke_wrapped_method_with_metadata_as_none():
121121 assert len (metadata ) == 1
122122
123123
124+ def test_invoke_wrapped_method_with_duplicate_x_goog_api_client_metadata ():
125+ method = mock .Mock (spec = ["__call__" ])
126+
127+ # Create a custom ClientInfo with defined properties so we know exactly what is returned
128+ client_info = google .api_core .gapic_v1 .client_info .ClientInfo (
129+ user_agent = "custom-user-agent/1.0" ,
130+ python_version = "3.14.0" ,
131+ grpc_version = "1.76.0" ,
132+ api_core_version = "2.29.0"
133+ )
134+
135+ wrapped_method = google .api_core .gapic_v1 .method .wrap_method (method , client_info = client_info )
136+
137+ # Invoke the wrapped method with an explicit user-provided custom header
138+ wrapped_method (
139+ mock .sentinel .request ,
140+ metadata = [("x-goog-api-client" , "override-client/2.0" ), ("other-header" , "value" )]
141+ )
142+
143+ method .assert_called_once_with (mock .sentinel .request , metadata = mock .ANY )
144+ metadata = method .call_args [1 ]["metadata" ]
145+
146+ # There should only be one "x-goog-api-client" header, containing both values joined by space,
147+ # plus the other-header.
148+ assert len (metadata ) == 2
149+ metadata_dict = dict (metadata )
150+ assert "other-header" in metadata_dict
151+ assert metadata_dict ["other-header" ] == "value"
152+ assert "x-goog-api-client" in metadata_dict
153+ # Verify both the user-provided override value and the library system telemetry are merged explicitly
154+ assert metadata_dict ["x-goog-api-client" ] == "override-client/2.0 custom-user-agent/1.0 gl-python/3.14.0 grpc/1.76.0 gax/2.29.0"
155+
156+
124157@mock .patch ("time.sleep" )
125158def test_wrap_method_with_default_retry_and_timeout_and_compression (unused_sleep ):
126159 method = mock .Mock (
0 commit comments