def _make_request_data(self, teststep_dict, entry_json): """ parse HAR entry request data, and make teststep request data Args: entry_json (dict): { "request": { "method": "POST", "postData": { "mimeType": "application/x-www-form-urlencoded; charset=utf-8", "params": [ {"name": "a", "value": 1}, {"name": "b", "value": "2"} } }, }, "response": {...} } Returns: { "request": { "method": "POST", "data": {"v": "1", "w": "2"} } } """ method = entry_json["request"].get("method") if method in ["POST", "PUT", "PATCH"]: postData = entry_json["request"].get("postData", {}) mimeType = postData.get("mimeType") # Note that text and params fields are mutually exclusive. if "text" in postData: post_data = postData.get("text") else: params = postData.get("params", []) post_data = utils.convert_list_to_dict(params) request_data_key = "data" if not mimeType: pass elif mimeType.startswith("application/json"): try: post_data = json.loads(post_data) request_data_key = "json" except JSONDecodeError: pass elif mimeType.startswith("application/x-www-form-urlencoded"): post_data = utils.convert_x_www_form_urlencoded_to_dict( post_data) else: # TODO: make compatible with more mimeType pass teststep_dict["request"][request_data_key] = post_data
def __make_request_url(self, teststep_dict, entry_json): """ parse HAR entry request url and queryString, and make teststep url and params Args: entry_json (dict): { "request": { "url": "https://httprunner.top/home?v=1&w=2", "queryString": [ {"name": "v", "value": "1"}, {"name": "w", "value": "2"} ], }, "response": {} } Returns: { "name: "/home", "request": { url: "https://httprunner.top/home", params: {"v": "1", "w": "2"} } } """ request_params = utils.convert_list_to_dict( entry_json["request"].get("queryString", []) ) url = entry_json["request"].get("url") if not url: logger.exception("url missed in request.") sys.exit(1) parsed_object = urlparse.urlparse(url) if request_params: parsed_object = parsed_object._replace(query="") teststep_dict["request"]["url"] = parsed_object.geturl() teststep_dict["request"]["params"] = request_params else: teststep_dict["request"]["url"] = url teststep_dict["name"] = parsed_object.path
def _make_validate(self, teststep_dict, entry_json): """ parse HAR entry response and make teststep validate. Args: entry_json (dict): { "request": {}, "response": { "status": 200, "headers": [ { "name": "Content-Type", "value": "application/json; charset=utf-8" }, ], "content": { "size": 71, "mimeType": "application/json; charset=utf-8", "text": "eyJJc1N1Y2Nlc3MiOnRydWUsIkNvZGUiOjIwMCwiTWVzc2FnZSI6bnVsbCwiVmFsdWUiOnsiQmxuUmVzdWx0Ijp0cnVlfX0=", "encoding": "base64" } } } Returns: { "validate": [ {"eq": ["status_code", 200]} ] } """ teststep_dict["validate"].append( {"eq": ["status_code", entry_json["response"].get("status")]}) resp_content_dict = entry_json["response"].get("content") headers_mapping = utils.convert_list_to_dict( entry_json["response"].get("headers", [])) if "Content-Type" in headers_mapping: teststep_dict["validate"].append({ "eq": ["headers.Content-Type", headers_mapping["Content-Type"]] }) text = resp_content_dict.get("text") if not text: return mime_type = resp_content_dict.get("mimeType") if mime_type and mime_type.startswith("application/json"): encoding = resp_content_dict.get("encoding") if encoding and encoding == "base64": content = base64.b64decode(text) try: content = content.decode("utf-8") except UnicodeDecodeError: logger.warning( f"failed to decode base64 content with utf-8 !") return else: content = text try: resp_content_json = json.loads(content) except JSONDecodeError: logger.warning( f"response content can not be loaded as json: {content}") return if not isinstance(resp_content_json, dict): # e.g. ['a', 'b'] return for key, value in resp_content_json.items(): if isinstance(value, (dict, list)): continue teststep_dict["validate"].append( {"eq": ["body.{}".format(key), value]})
def test_convert_list_to_dict(self): origin_list = [{"name": "v", "value": "1"}, {"name": "w", "value": "2"}] self.assertEqual(utils.convert_list_to_dict(origin_list), {"v": "1", "w": "2"})