feat(接口测试): TCP-MOCK服务增加按照制定编码返回内容的功能

--story=1011852 --user=宋天阳 #23875【接口测试】-tcp mock 返回字符串支持设置编码格式
https://www.tapd.cn/55049933/s/1365656
This commit is contained in:
song-tianyang 2023-04-23 14:36:30 +08:00 committed by 建国
parent 6374900d0c
commit 73f380febd
3 changed files with 63 additions and 14 deletions

View File

@ -0,0 +1,9 @@
package io.metersphere.api.dto;
import lombok.Data;
@Data
public class TCPMockReturnDTO {
private String encode;
private String returnMessage;
}

View File

@ -1,5 +1,6 @@
package io.metersphere.api.tcp.server;
import io.metersphere.api.dto.TCPMockReturnDTO;
import io.metersphere.api.dto.mock.MockExpectConfigDTO;
import io.metersphere.api.dto.mock.RequestMockParams;
import io.metersphere.commons.utils.CommonBeanFactory;
@ -27,15 +28,19 @@ public class TCPService {
public void run() {
byte[] b = new byte[1024];
String returnMsg = StringUtils.EMPTY;
String message = StringUtils.EMPTY;
try {
is = s.getInputStream();
os = s.getOutputStream();
int len = is.read(b);
message = new String(b, 0, len);
returnMsg = this.getReturnMsg(message);
os.write(returnMsg.getBytes());
String message = new String(b, 0, len);
TCPMockReturnDTO returnDTO = this.getReturnMsg(message);
if (StringUtils.isNotEmpty(returnDTO.getEncode())) {
os.write(returnDTO.getReturnMessage().getBytes(returnDTO.getEncode()));
} else {
os.write(returnDTO.getReturnMessage().getBytes());
}
} catch (Exception e) {
LogUtil.error(e);
} finally {
@ -43,13 +48,14 @@ public class TCPService {
}
}
private String getReturnMsg(String message) {
private TCPMockReturnDTO getReturnMsg(String message) {
LogUtil.info("TCP-Mock start. port: " + this.port + "; Message:" + message);
MockConfigService mockConfigService = CommonBeanFactory.getBean(MockConfigService.class);
MockExpectConfigDTO matchdMockExpectDTO = mockConfigService.matchTcpMockExpect(message, this.port);
String returnMsg = StringUtils.EMPTY;
if (matchdMockExpectDTO != null && matchdMockExpectDTO.getMockExpectConfig() != null) {
String response = matchdMockExpectDTO.getMockExpectConfig().getResponse();
MockExpectConfigDTO matchMockExpectDTO = mockConfigService.matchTcpMockExpect(message, this.port);
TCPMockReturnDTO returnDTO = new TCPMockReturnDTO();
returnDTO.setReturnMessage(StringUtils.EMPTY);
if (matchMockExpectDTO != null && matchMockExpectDTO.getMockExpectConfig() != null) {
String response = matchMockExpectDTO.getMockExpectConfig().getResponse();
JSONObject responseObj = JSONUtil.parseObject(response);
int delayed = 0;
try {
@ -69,7 +75,15 @@ public class TCPService {
}
RequestMockParams requestMockParams = new RequestMockParams();
requestMockParams.setTcpParam(message);
returnMsg = mockApiUtils.getResultByResponseResult(matchdMockExpectDTO.getProjectId(), respResultObj.optJSONObject("body"), StringUtils.EMPTY, null, requestMockParams, useScript);
returnDTO.setReturnMessage(
mockApiUtils.getResultByResponseResult(matchMockExpectDTO.getProjectId(), respResultObj.optJSONObject("body"), StringUtils.EMPTY, null, requestMockParams, useScript)
);
}
if (respResultObj.has("rsp_encode")) {
String encode = respResultObj.getString("rsp_encode");
if (StringUtils.isNotBlank(encode)) {
returnDTO.setEncode(encode);
}
}
try {
if (respResultObj.has("delayed")) {
@ -79,7 +93,7 @@ public class TCPService {
LogUtil.error(e);
}
} else {
returnMsg = responseObj.optString("body");
returnDTO.setReturnMessage(responseObj.optString("body"));
}
try {
@ -88,8 +102,8 @@ public class TCPService {
LogUtil.error(e);
}
}
LogUtil.info("TCP-Mock start. port: " + this.port + "; Message:" + message + "; response:" + returnMsg);
return returnMsg;
LogUtil.info("TCP-Mock start. port: " + this.port + "; Message:" + message + "; response:" + returnDTO.getReturnMessage());
return returnDTO;
}
public void close() {

View File

@ -46,6 +46,18 @@
</el-input-number>
</el-row>
</el-tab-pane>
<el-tab-pane v-if="isTcp" :label="$t('commons.encode')" name="rsp_encode" class="pane">
<el-row>
<el-col :span="2" />
<el-col :span="20">
<el-select v-model="response.rsp_encode" size="mini" class="mode-row" style="width: 200px">
<el-option v-for="item in encodeArr" :key="item.id" :label="item.name" :value="item.id">
</el-option>
</el-select>
</el-col>
<el-col :span="2" />
</el-row>
</el-tab-pane>
</el-tabs>
</el-collapse-transition>
</el-form>
@ -119,6 +131,20 @@ export default {
isMsCodeEditShow: true,
reqMessages: '',
headerSuggestions: REQUEST_HEADERS,
encodeArr: [
{
id: 'UTF-8',
name: 'UTF-8',
},
{
id: 'GBK',
name: 'GBK',
},
{
id: 'ISO-8859-1',
name: 'ISO-8859-1',
},
],
};
},
watch: {