def _before_record_request(request: VcrRequest) -> VcrRequest: """This function formats and cleans request data to make it more readable and idempotent when re-snapshotting""" request.uri = re.sub(r"\d{4}-\d{2}-\d{2}_\d{2}-\d{2}-\d{2}", "2000-01-01_00-00-00", request.uri) try: body_str = request.body.decode("utf-8") except Exception: body_str = None if "content-type" in request.headers: request.headers["content-type"] = re.sub( r"boundary=[0-9a-f]+", "boundary=fffffff0000000", request.headers["content-type"], ) if (request.headers["content-type"].startswith("multipart/form-data") and body_str is not None): for regex_to_replace, replace_with in _REPLACE_IN_REQUEST_MULTIFORM.items( ): body_str = re.sub(regex_to_replace, replace_with, body_str) if body_str is not None: for regex_to_replace, replace_with in _REPLACE_IN_REQUEST_CONTENT.items( ): body_str = re.sub(regex_to_replace, replace_with, body_str) request.body = body_str return request
def remove_creds(req: request.Request) -> request.Request: if req.path.endswith(Resource._urls['login']) and req.method == 'POST': username_key = 'j_id6:j_id20' password_key = 'j_id6:j_id22' body = parse.parse_qs(req.body.decode('utf-8')) body[username_key] = ['USERNAME'] body[password_key] = ['PASSWORD'] req.body = parse.urlencode(body) return req
def scrub_body_request(request: Request) -> dict: body = request.body.decode('utf-8') body_dict = json.loads(body) try: body_dict['data']['systemRequest']['user'] = '******' body_dict['data']['systemRequest']['password'] = '******' except KeyError: pass request.body = json.dumps(body_dict) return request