-
Notifications
You must be signed in to change notification settings - Fork 71
feat: SDKE-528 Implement Script To Preprocess Request Recordings #452
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
denischilik
merged 9 commits into
main
from
feat/SDKE-528-Implement-Script-To-Preprocess-Request-Recordings
Nov 17, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dc3ea09
feat(SDKE-528): Add WireMock recording files for integration tests
denischilik f9b35ab
Add script to extract request bodies from WireMock mappings
denischilik 83d2b97
Add script to update WireMock mappings from extracted request bodies
denischilik b7bb9ca
Add field replacement functions to extract_request_body script
denischilik d382d73
Add --replace parameter to extract_request_body script
denischilik bd39445
Update integration tests documentation with detailed workflow
denischilik f9118e1
Add extracted request bodies for log_event and identify mappings
denischilik 4a8513a
Remove header comments from main.swift
denischilik 4c84a72
Update IntegrationTests/README.md
denischilik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| #!/usr/bin/env python3 | ||
| """ | ||
| Script for extracting JSON request bodies from WireMock mappings. | ||
|
|
||
| Usage: | ||
| python3 extract_request_body.py <mapping_file> <test_name> [--replace] | ||
|
|
||
| Example: | ||
| python3 extract_request_body.py wiremock-recordings/mappings/mapping-v1-identify.json identify_test | ||
| python3 extract_request_body.py wiremock-recordings/mappings/mapping-v1-identify.json identify_test --replace | ||
| """ | ||
|
|
||
| import json | ||
| import sys | ||
| import os | ||
| import argparse | ||
| from pathlib import Path | ||
| from typing import Dict, Any, List, Union | ||
|
|
||
|
|
||
| # Default list of fields to replace with ${json-unit.ignore} | ||
| # Based on existing WireMock mappings that contain dynamic/timestamp values | ||
| DEFAULT_REPLACE_FIELDS = [ | ||
| 'a', # App ID | ||
| 'bid', # Bundle ID / Build ID | ||
| 'bsv', # Build System Version | ||
| 'ct', # Creation Time / Current Time | ||
| 'das', # Device Application Stamp | ||
| 'dfs', # Device Fingerprint String | ||
| 'dlc', # Device Locale | ||
| 'dn', # Device Name | ||
| 'dosv', # Device OS Version | ||
| 'est', # Event Start Time | ||
| 'ict', # Init Config Time | ||
| 'id', # ID (various message/event IDs) | ||
| 'lud', # Last Update Date | ||
| 'sct', # Session Creation Time | ||
| 'sid', # Session ID | ||
| 'vid', # Vendor ID | ||
| ] | ||
|
|
||
|
|
||
| def replace_field_value(data: Union[Dict, List, Any], field_name: str, replacement_value: str) -> Union[Dict, List, Any]: | ||
| """ | ||
| Recursively replaces the value of a specified field in a JSON structure. | ||
|
|
||
| Args: | ||
| data: JSON data (dict, list, or primitive value) | ||
| field_name: Name of the field to replace | ||
| replacement_value: New value to set for the field | ||
|
|
||
| Returns: | ||
| Modified data structure with replaced field values | ||
|
|
||
| Example: | ||
| data = {"id": "123", "name": "test", "nested": {"id": "456"}} | ||
| result = replace_field_value(data, "id", "${json-unit.ignore}") | ||
| # result = {"id": "${json-unit.ignore}", "name": "test", "nested": {"id": "${json-unit.ignore}"}} | ||
| """ | ||
| if isinstance(data, dict): | ||
| # For dictionaries, check each key | ||
| result = {} | ||
| for key, value in data.items(): | ||
| if key == field_name: | ||
| # Replace the value for this field | ||
| result[key] = replacement_value | ||
| else: | ||
| # Recursively process the value | ||
| result[key] = replace_field_value(value, field_name, replacement_value) | ||
| return result | ||
| elif isinstance(data, list): | ||
| # For lists, recursively process each item | ||
| return [replace_field_value(item, field_name, replacement_value) for item in data] | ||
| else: | ||
| # For primitive values, return as is | ||
| return data | ||
|
|
||
|
|
||
| def replace_fields_from_list(data: Union[Dict, List, Any], field_names: List[str], replacement_value: str = "${json-unit.ignore}") -> Union[Dict, List, Any]: | ||
| """ | ||
| Replaces values of multiple fields in a JSON structure with a specified value. | ||
|
|
||
| Args: | ||
| data: JSON data (dict, list, or primitive value) | ||
| field_names: List of field names to replace | ||
| replacement_value: Value to use for replacement (default: "${json-unit.ignore}") | ||
|
|
||
| Returns: | ||
| Modified data structure with all specified fields replaced | ||
|
|
||
| Example: | ||
| data = {"id": "123", "ct": "1234567890", "name": "test", "nested": {"id": "456", "ct": "0987654321"}} | ||
| result = replace_fields_from_list(data, ["id", "ct"]) | ||
| # result = {"id": "${json-unit.ignore}", "ct": "${json-unit.ignore}", "name": "test", | ||
| # "nested": {"id": "${json-unit.ignore}", "ct": "${json-unit.ignore}"}} | ||
|
|
||
| # Or with custom replacement value: | ||
| result = replace_fields_from_list(data, ["id", "ct"], "IGNORED") | ||
| # result = {"id": "IGNORED", "ct": "IGNORED", "name": "test", "nested": {"id": "IGNORED", "ct": "IGNORED"}} | ||
| """ | ||
| result = data | ||
|
|
||
| # Apply replacement for each field in the list | ||
| for field_name in field_names: | ||
| result = replace_field_value(result, field_name, replacement_value) | ||
|
|
||
| return result | ||
|
|
||
| def extract_request_body(mapping_file: str, test_name: str, replace_fields: bool = False) -> None: | ||
| """ | ||
| Extracts JSON body from WireMock mapping and saves it to a separate file. | ||
|
|
||
| Args: | ||
| mapping_file: Path to mapping file | ||
| test_name: Test name | ||
| replace_fields: If True, replaces known dynamic fields with ${json-unit.ignore} | ||
| """ | ||
| # Check if mapping file exists | ||
| mapping_path = Path(mapping_file) | ||
| if not mapping_path.exists(): | ||
| print(f"❌ Error: mapping file not found: {mapping_file}") | ||
| sys.exit(1) | ||
|
|
||
| # Read mapping file | ||
| try: | ||
| with open(mapping_path, 'r', encoding='utf-8') as f: | ||
| mapping_data = json.load(f) | ||
| except json.JSONDecodeError as e: | ||
| print(f"❌ Error: failed to parse JSON from mapping file: {e}") | ||
| sys.exit(1) | ||
| except Exception as e: | ||
| print(f"❌ Error reading mapping file: {e}") | ||
| sys.exit(1) | ||
|
|
||
| # Extract request information | ||
| try: | ||
| request_data = mapping_data.get('request', {}) | ||
| method = request_data.get('method', 'UNKNOWN') | ||
| url = request_data.get('url', 'UNKNOWN') | ||
|
|
||
| body_patterns = request_data.get('bodyPatterns', []) | ||
|
|
||
| # Check for body presence | ||
| if not body_patterns: | ||
| # This might be a GET request or another method without body | ||
| print(f"⚠️ Warning: bodyPatterns not found in mapping") | ||
| print(f" Request method: {method}") | ||
| print(f" URL: {url}") | ||
| print(" (GET requests usually don't have a body)") | ||
| sys.exit(1) | ||
|
|
||
| # Get escaped JSON string | ||
| equal_to_json = body_patterns[0].get('equalToJson') | ||
| if equal_to_json is None: | ||
| print("❌ Error: equalToJson not found in bodyPatterns") | ||
| sys.exit(1) | ||
|
|
||
| # Parse escaped JSON string to get the actual JSON object | ||
| # (unescape) | ||
| request_body = json.loads(equal_to_json) | ||
|
|
||
| # Apply field replacements if requested | ||
| if replace_fields: | ||
| print(f"🔄 Replacing {len(DEFAULT_REPLACE_FIELDS)} known dynamic fields with ${{json-unit.ignore}}") | ||
| request_body = replace_fields_from_list(request_body, DEFAULT_REPLACE_FIELDS) | ||
|
|
||
| except json.JSONDecodeError as e: | ||
| print(f"❌ Error: failed to parse JSON from equalToJson: {e}") | ||
| sys.exit(1) | ||
| except Exception as e: | ||
| print(f"❌ Error extracting body from mapping: {e}") | ||
| sys.exit(1) | ||
|
|
||
| # Form output structure | ||
| output_data = { | ||
| "test_name": test_name, | ||
| "source_mapping": str(mapping_path), | ||
| "request_method": method, | ||
| "request_url": url, | ||
| "request_body": request_body | ||
| } | ||
|
|
||
| # Create directory for saving extracted bodies | ||
| output_dir = Path("wiremock-recordings/requests") | ||
| output_dir.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| # Form output filename | ||
| output_file = output_dir / f"{test_name}.json" | ||
|
|
||
| # Save to file with pretty-print for readability | ||
| try: | ||
| with open(output_file, 'w', encoding='utf-8') as f: | ||
| json.dump(output_data, f, indent=2, ensure_ascii=False) | ||
|
|
||
| print(f"✅ JSON body successfully extracted and saved to: {output_file}") | ||
| print(f"📝 Test name: {test_name}") | ||
| print(f"🔗 Source mapping: {mapping_path}") | ||
|
|
||
| except Exception as e: | ||
| print(f"❌ Error saving file: {e}") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def main(): | ||
| # Set up command line argument parser | ||
| parser = argparse.ArgumentParser( | ||
| description='Extract JSON request bodies from WireMock mappings', | ||
| formatter_class=argparse.RawDescriptionHelpFormatter | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| 'mapping_file', | ||
| help='Path to WireMock mapping file' | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| 'test_name', | ||
| help='Test name for the output file' | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| '--replace', | ||
| action='store_true', | ||
| help='Replace known dynamic fields with ${json-unit.ignore}' | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| extract_request_body(args.mapping_file, args.test_name, replace_fields=args.replace) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
|
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add python as well to the
Prerequisites?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don’t need to list Python in the prerequisites because macOS already includes a system Python installation out of the box. Since the script is lightweight and doesn’t rely on any external libraries, it will run fine with the default Python that comes pre-installed on every Mac