mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-02 12:09:13 +08:00
fix(接口测试): 修复tcp mock请求内容超过1024个字节失败问题
https://www.tapd.cn/55049933/bugtrace/bugs/view?bug_id=1155049933001030652&from=wxnotification&corpid=ww918354e3468dc0cc&agentid=1000014&jump_count=1 Signed-off-by: fit2-zhao <yong.zhao@fit2cloud.com>
This commit is contained in:
parent
0483653abe
commit
0fd091c878
@ -3,10 +3,8 @@ package io.metersphere.api.tcp;
|
||||
import io.metersphere.api.tcp.server.TCPServer;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author song.tianyang
|
||||
@ -14,15 +12,14 @@ import java.util.Map;
|
||||
*/
|
||||
public class TCPPool {
|
||||
|
||||
private static HashMap<Integer, TCPServer> serverSockedMap = new HashMap<>();
|
||||
private static final HashMap<Integer, TCPServer> serverSockedMap = new HashMap<>();
|
||||
|
||||
private TCPPool() {
|
||||
}
|
||||
|
||||
public static String createTcp(int port) {
|
||||
String returnString = StringUtils.EMPTY;
|
||||
public static void createTcp(int port) {
|
||||
if (port > 0) {
|
||||
TCPServer tcpServer = null;
|
||||
TCPServer tcpServer;
|
||||
if (serverSockedMap.containsKey(port)) {
|
||||
tcpServer = serverSockedMap.get(port);
|
||||
} else {
|
||||
@ -34,15 +31,12 @@ public class TCPPool {
|
||||
Thread t = new Thread(tcpServer);
|
||||
t.start();
|
||||
}
|
||||
returnString = "OK";
|
||||
} catch (Exception e) {
|
||||
returnString = e.getMessage();
|
||||
LogUtil.error(e);
|
||||
MSException.throwException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return returnString;
|
||||
}
|
||||
|
||||
public static boolean isTcpOpen(int port) {
|
||||
@ -53,40 +47,14 @@ public class TCPPool {
|
||||
return false;
|
||||
}
|
||||
|
||||
public static String getTcpStatus() {
|
||||
if (serverSockedMap.isEmpty()) {
|
||||
return "null";
|
||||
} else {
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
for (Map.Entry<Integer, TCPServer> entry : serverSockedMap.entrySet()) {
|
||||
int port = entry.getKey();
|
||||
TCPServer tcpServer = entry.getValue();
|
||||
if (tcpServer == null) {
|
||||
stringBuffer.append("Port is " + port + ";");
|
||||
stringBuffer.append("Server is null;");
|
||||
} else {
|
||||
stringBuffer.append("Port is " + port + ";");
|
||||
stringBuffer.append("Server is open: " + tcpServer.isSocketOpen() + ";");
|
||||
}
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static String closeTcp(int portNum) {
|
||||
public static void closeTcp(int portNum) {
|
||||
TCPServer server = serverSockedMap.get(portNum);
|
||||
if (server == null) {
|
||||
return "Tcp Is not create!";
|
||||
} else {
|
||||
String returnMsg = null;
|
||||
if (server != null) {
|
||||
try {
|
||||
server.closeSocket();
|
||||
returnMsg = "OK";
|
||||
} catch (Exception e) {
|
||||
returnMsg = e.getMessage();
|
||||
LogUtil.error(e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return returnMsg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -10,10 +10,10 @@ import java.net.Socket;
|
||||
* @Date 2021/8/11 10:35 上午
|
||||
*/
|
||||
public class TCPServer implements Runnable {
|
||||
private int port;
|
||||
private final int port;
|
||||
private ServerSocket serverSocket;
|
||||
|
||||
private TCPService servicer;
|
||||
private TCPService server;
|
||||
|
||||
public TCPServer(int port) {
|
||||
this.port = port;
|
||||
@ -22,30 +22,23 @@ public class TCPServer implements Runnable {
|
||||
public void openSocket() throws Exception {
|
||||
this.serverSocket = new ServerSocket(this.port);
|
||||
|
||||
while (true) {
|
||||
do {
|
||||
if (!this.serverSocket.isClosed()) {
|
||||
Socket socket = this.serverSocket.accept();
|
||||
servicer = new TCPService(socket, port);
|
||||
servicer.run();
|
||||
server = new TCPService(socket, port);
|
||||
server.run();
|
||||
}
|
||||
if (this.serverSocket.isClosed()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} while (!this.serverSocket.isClosed());
|
||||
}
|
||||
|
||||
public boolean isSocketOpen() {
|
||||
if (this.serverSocket != null && !this.serverSocket.isClosed()) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return this.serverSocket != null && !this.serverSocket.isClosed();
|
||||
}
|
||||
|
||||
public void closeSocket() throws Exception {
|
||||
if (this.serverSocket != null && !this.serverSocket.isClosed()) {
|
||||
if (servicer != null) {
|
||||
servicer.close();
|
||||
if (server != null) {
|
||||
server.close();
|
||||
}
|
||||
this.serverSocket.close();
|
||||
}
|
||||
|
@ -9,30 +9,51 @@ import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.commons.utils.mock.MockApiUtils;
|
||||
import io.metersphere.service.MockConfigService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.jmeter.protocol.tcp.sampler.ReadException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class TCPService {
|
||||
private Socket s;
|
||||
private InputStream is;
|
||||
private OutputStream os;
|
||||
private int port;
|
||||
private final Socket socket;
|
||||
private final int port;
|
||||
|
||||
public TCPService(Socket s, int port) {
|
||||
this.s = s;
|
||||
public TCPService(Socket socket, int port) {
|
||||
this.socket = socket;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
byte[] b = new byte[1024];
|
||||
try {
|
||||
is = s.getInputStream();
|
||||
os = s.getOutputStream();
|
||||
int len = is.read(b);
|
||||
String message = new String(b, 0, len);
|
||||
public String read(InputStream is) throws ReadException {
|
||||
try (ByteArrayOutputStream w = new ByteArrayOutputStream()) {
|
||||
final int size = 1024;
|
||||
byte[] buffer = new byte[size];
|
||||
while (true) {
|
||||
try {
|
||||
int x = is.read(buffer);
|
||||
if (x < size) {
|
||||
w.write(buffer, 0, x);
|
||||
break;
|
||||
}
|
||||
w.write(buffer, 0, x);
|
||||
} catch (IOException e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return w.toString(StandardCharsets.UTF_8);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new ReadException("Error decoding bytes from server with " + StandardCharsets.UTF_8 + ", bytes read: ",
|
||||
e, "<Read bytes with bad encoding>");
|
||||
} catch (IOException e) {
|
||||
throw new ReadException("Error reading from server, bytes read: ", e, StringUtils.EMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
public void run() throws IOException {
|
||||
try (InputStream is = socket.getInputStream();
|
||||
OutputStream os = socket.getOutputStream()) {
|
||||
String message = this.read(is);
|
||||
TCPMockReturnDTO returnDTO = this.getReturnMsg(message);
|
||||
|
||||
if (StringUtils.isNotEmpty(returnDTO.getEncode())) {
|
||||
@ -106,25 +127,9 @@ public class TCPService {
|
||||
return returnDTO;
|
||||
}
|
||||
|
||||
public void close() {
|
||||
//关闭资源
|
||||
try {
|
||||
is.close();
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
} finally {
|
||||
try {
|
||||
os.close();
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
} finally {
|
||||
try {
|
||||
s.close();
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
}
|
||||
}
|
||||
public void close() throws IOException {
|
||||
if (socket != null) {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1084,7 +1084,7 @@ public class MockConfigService {
|
||||
builder.parse(new InputSource(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))));
|
||||
isXml = true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
LogUtil.error(e);
|
||||
}
|
||||
return isXml;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user