fix(接口测试): 修复TCP协议,未按照选取的编码格式动态生成xml表头的缺陷

--bug=1024121 --user=王孝刚 【接口测试】github#22540
TCP协议接口定义,未按照选取的编码格式动态生成xml表头 https://www.tapd.cn/55049933/s/1348133
This commit is contained in:
wxg0103 2023-03-10 15:14:44 +08:00 committed by wxg0103
parent 1d14afdb62
commit 04f4499f9d
3 changed files with 11 additions and 7 deletions

View File

@ -106,7 +106,7 @@ public class MsTCPSampler extends MsTestElement {
break;
case "xml":
if (CollectionUtils.isNotEmpty(this.xmlDataStruct)) {
request = TcpTreeTableDataParser.treeTableData2Xml(this.xmlDataStruct);
request = TcpTreeTableDataParser.treeTableData2Xml(this.xmlDataStruct, this.connectEncoding);
}
break;
case "raw":

View File

@ -34,7 +34,7 @@ public class TcpTreeTableDataParser {
public static final String DATA_TYPE_STRING = PropertyConstant.STRING;
public static final String DATA_TYPE_OBJECT = PropertyConstant.OBJECT;
public static String treeTableData2Xml(List<TcpTreeTableDataStruct> treeDataList) {
public static String treeTableData2Xml(List<TcpTreeTableDataStruct> treeDataList, String encoding) {
String xmlString = StringUtils.EMPTY;
try {
if (treeDataList == null || treeDataList.isEmpty()) {
@ -56,7 +56,12 @@ public class TcpTreeTableDataParser {
// 设置XML文档格式
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
// 设置XML编码方式,即是用指定的编码方式保存XML文档到字符串(String),这里也可以指定为GBK或是ISO8859-1
outputFormat.setEncoding(StandardCharsets.UTF_8.name());
if (StringUtils.equals(encoding, StandardCharsets.UTF_8.name())) {
outputFormat.setEncoding(StandardCharsets.UTF_8.name());
} else {
//目前只有UTF-8和GBK两种编码
outputFormat.setEncoding(encoding);
}
//outputFormat.setSuppressDeclaration(true); //是否生产xml头
outputFormat.setIndent(true); //设置是否缩进
outputFormat.setNewlines(true); //设置是否换行

View File

@ -11,7 +11,6 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import jakarta.annotation.Resource;
import java.util.List;
/**
@ -59,7 +58,7 @@ public class TcpApiParamService {
tcpSampler.setRequest(tcpSampler.getRawDataStruct());
break;
case "xml":
String xmlDataStruct = this.genValueFromEsbDataStructByParam(tcpSampler.getXmlDataStruct());
String xmlDataStruct = this.genValueFromEsbDataStructByParam(tcpSampler.getXmlDataStruct(), tcpSampler.getConnectEncoding());
tcpSampler.setRequest(xmlDataStruct);
break;
case "json":
@ -75,8 +74,8 @@ public class TcpApiParamService {
}
//通过报文模版中的变量参数解析报文数据结构生成对应的xml数据
private String genValueFromEsbDataStructByParam(List<TcpTreeTableDataStruct> dataStructRequestList) {
String returnValue = TcpTreeTableDataParser.treeTableData2Xml(dataStructRequestList);
private String genValueFromEsbDataStructByParam(List<TcpTreeTableDataStruct> dataStructRequestList, String connectEncoding) {
String returnValue = TcpTreeTableDataParser.treeTableData2Xml(dataStructRequestList, connectEncoding);
return returnValue;
}