Formats a HAR request into a Request object.
(har_request: Dict[str, Any])
| 39 | ) |
| 40 | |
| 41 | def format_request(har_request: Dict[str, Any]) -> Request: |
| 42 | """ |
| 43 | Formats a HAR request into a Request object. |
| 44 | """ |
| 45 | method = har_request.get("method", "GET") |
| 46 | url = har_request.get("url", "") |
| 47 | |
| 48 | # Store headers as a dictionary, excluding headers containing excluded keywords |
| 49 | headers = { |
| 50 | header.get("name", ""): header.get("value", "") |
| 51 | for header in har_request.get("headers", []) |
| 52 | if not any(keyword.lower() in header.get("name", "").lower() |
| 53 | for keyword in excluded_header_keywords) |
| 54 | } |
| 55 | |
| 56 | query_params_list = har_request.get("queryString", []) |
| 57 | query_params = {param["name"]: param["value"] for param in query_params_list} if query_params_list else None |
| 58 | |
| 59 | post_data = har_request.get("postData", {}) |
| 60 | body = post_data.get("text") if post_data else None |
| 61 | |
| 62 | # Try to parse body as JSON if Content-Type is application/json |
| 63 | if body: |
| 64 | headers_lower = {k.lower(): v for k, v in headers.items()} |
| 65 | content_type = headers_lower.get('content-type') |
| 66 | if content_type and 'application/json' in content_type.lower(): |
| 67 | try: |
| 68 | body = json.loads(body) |
| 69 | except json.JSONDecodeError: |
| 70 | pass # Keep body as is if not valid JSON |
| 71 | |
| 72 | return Request( |
| 73 | method=method, |
| 74 | url=url, |
| 75 | headers=headers, |
| 76 | query_params=query_params, |
| 77 | body=body |
| 78 | ) |
| 79 | |
| 80 | |
| 81 | def format_response(har_response: Dict[str, Any]) -> Dict[str, str]: |
no test coverage detected