File: domain_replacer_processor.py

package info (click to toggle)
python-azure 20230112%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 749,544 kB
  • sloc: python: 6,815,827; javascript: 287; makefile: 195; xml: 109; sh: 105
file content (22 lines) | stat: -rw-r--r-- 806 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import re

from azure_devtools.scenario_tests import RecordingProcessor

class DomainReplacerProcessor(RecordingProcessor):
    """Sanitize the domain name in both request and response"""

    def __init__(self, replacement=".sanitized.com", regex_pattern="\.[0-9a-fA-F]{32}\.com"):
        self._replacement = replacement
        self._regex_pattern = regex_pattern
        
    def process_request(self, request):
        if request.body is not None:
            request.body = re.sub(self._regex_pattern, self._replacement, request.body.decode()).encode()

        return request

    def process_response(self, response):
        if response['body']['string']:
            response['body']['string'] = re.sub(self._regex_pattern, self._replacement, response['body']['string'])

        return response