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 9acc790ecb
commit adad09779d
1 changed files with 44 additions and 6 deletions

View File

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