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.api.tcp.server.TCPServer;
|
||||||
import io.metersphere.commons.exception.MSException;
|
import io.metersphere.commons.exception.MSException;
|
||||||
import io.metersphere.commons.utils.LogUtil;
|
import io.metersphere.commons.utils.LogUtil;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author song.tianyang
|
* @author song.tianyang
|
||||||
|
@ -14,15 +12,14 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class TCPPool {
|
public class TCPPool {
|
||||||
|
|
||||||
private static HashMap<Integer, TCPServer> serverSockedMap = new HashMap<>();
|
private static final HashMap<Integer, TCPServer> serverSockedMap = new HashMap<>();
|
||||||
|
|
||||||
private TCPPool() {
|
private TCPPool() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String createTcp(int port) {
|
public static void createTcp(int port) {
|
||||||
String returnString = StringUtils.EMPTY;
|
|
||||||
if (port > 0) {
|
if (port > 0) {
|
||||||
TCPServer tcpServer = null;
|
TCPServer tcpServer;
|
||||||
if (serverSockedMap.containsKey(port)) {
|
if (serverSockedMap.containsKey(port)) {
|
||||||
tcpServer = serverSockedMap.get(port);
|
tcpServer = serverSockedMap.get(port);
|
||||||
} else {
|
} else {
|
||||||
|
@ -34,15 +31,12 @@ public class TCPPool {
|
||||||
Thread t = new Thread(tcpServer);
|
Thread t = new Thread(tcpServer);
|
||||||
t.start();
|
t.start();
|
||||||
}
|
}
|
||||||
returnString = "OK";
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
returnString = e.getMessage();
|
|
||||||
LogUtil.error(e);
|
LogUtil.error(e);
|
||||||
MSException.throwException(e.getMessage());
|
MSException.throwException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnString;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isTcpOpen(int port) {
|
public static boolean isTcpOpen(int port) {
|
||||||
|
@ -53,40 +47,14 @@ public class TCPPool {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getTcpStatus() {
|
public static void closeTcp(int portNum) {
|
||||||
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) {
|
|
||||||
TCPServer server = serverSockedMap.get(portNum);
|
TCPServer server = serverSockedMap.get(portNum);
|
||||||
if (server == null) {
|
if (server != null) {
|
||||||
return "Tcp Is not create!";
|
|
||||||
} else {
|
|
||||||
String returnMsg = null;
|
|
||||||
try {
|
try {
|
||||||
server.closeSocket();
|
server.closeSocket();
|
||||||
returnMsg = "OK";
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
returnMsg = e.getMessage();
|
throw new RuntimeException(e);
|
||||||
LogUtil.error(e);
|
}
|
||||||
}
|
|
||||||
return returnMsg;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,10 +10,10 @@ import java.net.Socket;
|
||||||
* @Date 2021/8/11 10:35 上午
|
* @Date 2021/8/11 10:35 上午
|
||||||
*/
|
*/
|
||||||
public class TCPServer implements Runnable {
|
public class TCPServer implements Runnable {
|
||||||
private int port;
|
private final int port;
|
||||||
private ServerSocket serverSocket;
|
private ServerSocket serverSocket;
|
||||||
|
|
||||||
private TCPService servicer;
|
private TCPService server;
|
||||||
|
|
||||||
public TCPServer(int port) {
|
public TCPServer(int port) {
|
||||||
this.port = port;
|
this.port = port;
|
||||||
|
@ -22,30 +22,23 @@ public class TCPServer implements Runnable {
|
||||||
public void openSocket() throws Exception {
|
public void openSocket() throws Exception {
|
||||||
this.serverSocket = new ServerSocket(this.port);
|
this.serverSocket = new ServerSocket(this.port);
|
||||||
|
|
||||||
while (true) {
|
do {
|
||||||
if (!this.serverSocket.isClosed()) {
|
if (!this.serverSocket.isClosed()) {
|
||||||
Socket socket = this.serverSocket.accept();
|
Socket socket = this.serverSocket.accept();
|
||||||
servicer = new TCPService(socket, port);
|
server = new TCPService(socket, port);
|
||||||
servicer.run();
|
server.run();
|
||||||
}
|
|
||||||
if (this.serverSocket.isClosed()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
} while (!this.serverSocket.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSocketOpen() {
|
public boolean isSocketOpen() {
|
||||||
if (this.serverSocket != null && !this.serverSocket.isClosed()) {
|
return this.serverSocket != null && !this.serverSocket.isClosed();
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void closeSocket() throws Exception {
|
public void closeSocket() throws Exception {
|
||||||
if (this.serverSocket != null && !this.serverSocket.isClosed()) {
|
if (this.serverSocket != null && !this.serverSocket.isClosed()) {
|
||||||
if (servicer != null) {
|
if (server != null) {
|
||||||
servicer.close();
|
server.close();
|
||||||
}
|
}
|
||||||
this.serverSocket.close();
|
this.serverSocket.close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,30 +9,51 @@ import io.metersphere.commons.utils.LogUtil;
|
||||||
import io.metersphere.commons.utils.mock.MockApiUtils;
|
import io.metersphere.commons.utils.mock.MockApiUtils;
|
||||||
import io.metersphere.service.MockConfigService;
|
import io.metersphere.service.MockConfigService;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.jmeter.protocol.tcp.sampler.ReadException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.*;
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class TCPService {
|
public class TCPService {
|
||||||
private Socket s;
|
private final Socket socket;
|
||||||
private InputStream is;
|
private final int port;
|
||||||
private OutputStream os;
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public TCPService(Socket s, int port) {
|
public TCPService(Socket socket, int port) {
|
||||||
this.s = s;
|
this.socket = socket;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void run() {
|
public String read(InputStream is) throws ReadException {
|
||||||
byte[] b = new byte[1024];
|
try (ByteArrayOutputStream w = new ByteArrayOutputStream()) {
|
||||||
|
final int size = 1024;
|
||||||
|
byte[] buffer = new byte[size];
|
||||||
|
while (true) {
|
||||||
try {
|
try {
|
||||||
is = s.getInputStream();
|
int x = is.read(buffer);
|
||||||
os = s.getOutputStream();
|
if (x < size) {
|
||||||
int len = is.read(b);
|
w.write(buffer, 0, x);
|
||||||
String message = new String(b, 0, len);
|
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);
|
TCPMockReturnDTO returnDTO = this.getReturnMsg(message);
|
||||||
|
|
||||||
if (StringUtils.isNotEmpty(returnDTO.getEncode())) {
|
if (StringUtils.isNotEmpty(returnDTO.getEncode())) {
|
||||||
|
@ -106,25 +127,9 @@ public class TCPService {
|
||||||
return returnDTO;
|
return returnDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() throws IOException {
|
||||||
//关闭资源
|
if (socket != null) {
|
||||||
try {
|
socket.close();
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
|
@ -1084,7 +1084,7 @@ public class MockConfigService {
|
||||||
builder.parse(new InputSource(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))));
|
builder.parse(new InputSource(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))));
|
||||||
isXml = true;
|
isXml = true;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
LogUtil.error(e);
|
||||||
}
|
}
|
||||||
return isXml;
|
return isXml;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue