fix(接口测试): 修复har文件导入时multipart类型的接口参数无法导入的问题

--bug=1016948 --user=宋天阳
[接口测试]#17975post,form-data的接口,har格式导入metersphere,接口请求参数是空的,没有导入成功
https://www.tapd.cn/55049933/s/1246752
This commit is contained in:
song-tianyang 2022-09-20 15:38:59 +08:00 committed by 建国
parent 285ccdcf1d
commit 188a273a51
1 changed files with 44 additions and 6 deletions

View File

@ -203,14 +203,36 @@ public class HarParser extends HarAbstractParser {
body.getKvs().add(kv);
}
} else if (contentType.startsWith(org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE)) {
contentType = org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
List<HarPostParam> postParams = content.params;
if (CollectionUtils.isNotEmpty(postParams)) {
for (HarPostParam postParam : postParams) {
KeyValue kv = new KeyValue(postParam.name, postParam.value);
body.getKvs().add(kv);
if (contentType.contains("boundary=") && StringUtils.contains(content.text, this.getBoundaryFromContentType(contentType))) {
contentType = org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
String[] textArr = StringUtils.split(content.text, "\r\n");
String paramData = this.parseMultipartByTextArr(textArr);
JSONObject obj = null;
try {
obj = JSONObject.parseObject(paramData);
if (obj != null) {
for (String key : obj.keySet()) {
KeyValue kv = new KeyValue(key, obj.getString(key));
body.getKvs().add(kv);
}
}
} catch (Exception e) {
obj = null;
}
if (obj == null) {
body.setRaw(paramData);
}
} else {
contentType = org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;
List<HarPostParam> postParams = content.params;
if (CollectionUtils.isNotEmpty(postParams)) {
for (HarPostParam postParam : postParams) {
KeyValue kv = new KeyValue(postParam.name, postParam.value);
body.getKvs().add(kv);
}
}
}
} else if (contentType.startsWith(org.springframework.http.MediaType.APPLICATION_JSON_VALUE)) {
contentType = org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
body.setRaw(content.text);
@ -231,6 +253,22 @@ public class HarParser extends HarAbstractParser {
body.setType(getBodyType(contentType));
}
private String getBoundaryFromContentType(String contentType) {
if (StringUtils.contains(contentType, "boundary=")) {
String[] strArr = StringUtils.split(contentType, "boundary=");
return strArr[strArr.length - 1];
}
return null;
}
private String parseMultipartByTextArr(String[] textArr) {
String data = null;
if (textArr != null && textArr.length > 2) {
data = textArr[textArr.length - 2];
}
return data;
}
private void parseResponseBody(HarContent content, Body body) {
if (content == null) {
return;