@@ -163,11 +163,32 @@ def _redact_project_location_path(path: str) -> str:
163163 return path
164164
165165
166- def _redact_request_body (body : dict [ str , object ] ) -> None :
166+ def _redact_request_body (body : Any ) -> None :
167167 """Redacts fields in the request body in place."""
168- for key , value in body .items ():
169- if isinstance (value , str ):
170- body [key ] = _redact_project_location_path (value )
168+ if isinstance (body , dict ):
169+ for key , value in body .items ():
170+ if isinstance (value , str ):
171+ value = _redact_project_location_path (value )
172+ value = re .sub (
173+ r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' ,
174+ '{UUID}' ,
175+ value ,
176+ )
177+ body [key ] = value
178+ elif isinstance (value , (dict , list )):
179+ _redact_request_body (value )
180+ elif isinstance (body , list ):
181+ for i , value in enumerate (body ):
182+ if isinstance (value , str ):
183+ value = _redact_project_location_path (value )
184+ value = re .sub (
185+ r'[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}' ,
186+ '{UUID}' ,
187+ value ,
188+ )
189+ body [i ] = value
190+ elif isinstance (value , (dict , list )):
191+ _redact_request_body (value )
171192
172193
173194def redact_http_request (http_request : HttpRequest ) -> None :
@@ -433,7 +454,10 @@ def _match_request(
433454 _redact_request_body (request_data_copy )
434455
435456 actual_request_body = [request_data_copy ]
436- expected_request_body = interaction .request .body_segments
457+ expected_request_body = copy .deepcopy (interaction .request .body_segments )
458+ for segment in expected_request_body :
459+ if not isinstance (segment , bytes ):
460+ _redact_request_body (segment )
437461 assert _equals_ignore_key_case (actual_request_body , expected_request_body ), (
438462 'Request body mismatch:\n '
439463 f'Actual: { actual_request_body } \n '
0 commit comments