Parcourir la source

fix: 修改bug

zhaoxingrui il y a 5 ans
Parent
commit
64fdb14939

+ 2 - 1
web/src/main/java/seecoder/devcloud/web/controller/APITest/APITestController.java

@@ -53,7 +53,8 @@ public class APITestController {
     @GetMapping("/execute/{testId}")
     @ApiOperation(value = "执行制定测试任务", httpMethod = "GET")
     @ApiImplicitParam(name = "testID", dataType = "int", paramType = "query")
-    public void executeAPITestByTestId(@PathVariable("testId") Integer testId) throws InterruptedException {
+    public Response executeAPITestByTestId(@PathVariable("testId") Integer testId) throws InterruptedException {
         apiTestService.executeAPITest(testId);
+        return Response.buildSuccess();
     }
 }

+ 86 - 11
web/src/main/java/seecoder/devcloud/web/service/impl/APITest/APITestServiceImpl.java

@@ -1,5 +1,6 @@
 package seecoder.devcloud.web.service.impl.APITest;
 
+import com.google.gson.Gson;
 import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -14,6 +15,7 @@ import seecoder.devcloud.web.service.APITest.APITestService;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLEncoder;
@@ -72,7 +74,9 @@ public class APITestServiceImpl implements APITestService {
 
         System.out.println(method);
         if (method.equals("GET")) { // GET方法
-            simulateHttpGet(url, params, qps, testId);
+            handleRequest(url, params, qps, testId, "GET");
+        } else if (method.equals("POST")) { // POST方法
+            handleRequest(url, params, qps, testId, "POST");
         }
     }
 
@@ -130,8 +134,8 @@ public class APITestServiceImpl implements APITestService {
         return timeList;
     }
 
-    // 处理GET方法
-    public void simulateHttpGet(String url, Map<String, Object> params, int qps, int testId) throws InterruptedException {
+    // 处理请求
+    public void handleRequest(String url, Map<String, Object> params, int qps, int testId, String method) throws InterruptedException {
         ArrayList<Integer> timeList = new ArrayList<>();
         final int[] success = {0};
         final int[] failure = {0};
@@ -139,7 +143,16 @@ public class APITestServiceImpl implements APITestService {
             @Override
             public void run() {
                 for (int i = 0; i < qps / 10; i++) {
-                    int[] arr = doGet(url, params);
+                    int[] arr = new int[2];
+                    if (method.equals("GET")) {
+                        arr = doGet(url, params);
+                    } else if (method.equals("POST")) {
+                        try {
+                            arr = doPost(url, params);
+                        } catch (IOException e) {
+                            e.printStackTrace();
+                        }
+                    }
                     timeList.add(arr[1]);
                     if (arr[0] == 1) success[0]++;
                     else failure[0]++;
@@ -165,7 +178,7 @@ public class APITestServiceImpl implements APITestService {
         apiTestMapper.update("完成", executeTime, success[0], failure[0], tl, testId);
     }
 
-    //
+    // 处理GET请求
     public int[] doGet(String url, Map<String, Object> params) {
         HttpURLConnection connection = null;
         BufferedReader br = null;
@@ -195,12 +208,14 @@ public class APITestServiceImpl implements APITestService {
                 full_url = url;
             }
 
-//            System.out.println(full_url);
+
+            if (full_url.startsWith("localhost")) {
+                full_url = "http://" + full_url;
+            }
             URL connURL = new URL(full_url);
 
             connection = (HttpURLConnection) connURL.openConnection();
             // 设置一些属性
-//            connection.setRequestMethod("GET");
             connection.setRequestProperty("Accept", "*/*");
             connection.setRequestProperty("Connection", "Keep-Alive");
             connection.setRequestProperty("User-Agent",
@@ -215,10 +230,6 @@ public class APITestServiceImpl implements APITestService {
             } else {
                 flag = 0;
             }
-//            Map<String, List<String>> headers = connection.getHeaderFields();
-//            for (String key:headers.keySet()) {
-//                System.out.println(key + "\t: \t" + headers.get(key));
-//            }
             br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
             String line;
             while ((line = br.readLine()) != null) {
@@ -242,6 +253,70 @@ public class APITestServiceImpl implements APITestService {
         return arr;
     }
 
+    // 处理POST请求
+    public int[] doPost(String url, Map<String, Object> params) throws IOException{
+        Gson gson = new Gson();
+        String param = gson.toJson(params);
+        OutputStreamWriter out = null;
+        BufferedReader reader = null;
+        String response = "";
+        long start = System.currentTimeMillis();
+        int[] arr = new int[2];
+        int flag = 0;
+        try {
+            URL httpUrl = null;
+            httpUrl = new URL(url);
+            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
+            conn.setRequestMethod("POST");
+            conn.setRequestProperty("Content-Type", "application/json");
+            conn.setRequestProperty("connection", "keep-alive");
+            conn.setUseCaches(false);//设置不要缓存
+            conn.setInstanceFollowRedirects(true);
+            conn.setDoOutput(true);
+            conn.setDoInput(true);
+            conn.connect();
+            // POST请求
+            out = new OutputStreamWriter(conn.getOutputStream());
+            out.write(param);
+            out.flush();
+            if (conn.getResponseCode() == 200) {
+                flag = 1;
+            } else {
+                flag = 0;
+            }
+            // 读取响应
+            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
+            String lines;
+            while ((lines = reader.readLine()) != null) {
+                lines = new String(lines.getBytes(), "utf-8");
+                response += lines;
+            }
+            reader.close();
+            // 断开连接
+            conn.disconnect();
+        } catch (Exception e) {
+            System.out.println("发送 POST 请求出现异常!"+e);
+            e.printStackTrace();
+        } finally{
+            try{
+                if(out!=null){
+                    out.close();
+                }
+                if(reader!=null){
+                    reader.close();
+                }
+            }
+            catch(IOException ex){
+                ex.printStackTrace();
+            }
+        }
+        long end = System.currentTimeMillis();
+        int time = Long.valueOf(end - start).intValue();
+        arr[0] = flag;
+        arr[1] = time;
+        return arr;
+    }
+
     public long startTaskAllInOnce(int threadNums, final Runnable task) throws InterruptedException {
         final CountDownLatch startGate = new CountDownLatch(1);
         final CountDownLatch endGate = new CountDownLatch(threadNums);