From 5a77ce1d1f62205dba3fbfabb5a3725ad05b9a46 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Wed, 10 Nov 2021 15:58:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(Mock=E6=B5=8B=E8=AF=95):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E6=97=A7=E6=95=B0=E6=8D=AE=E5=8D=87=E7=BA=A7=E5=90=8E?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E6=96=B9=E5=BC=8F=E5=8F=98=E5=85=A8=E5=8C=B9?= =?UTF-8?q?=E9=85=8D=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复旧数据升级后匹配方式变全匹配的问题 --- .../api/dto/mock/MockApiUtils.java | 27 ++++++++++--------- .../api/service/MockConfigService.java | 13 ++++++--- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java index 911b47380e..ad82976c23 100644 --- a/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java +++ b/backend/src/main/java/io/metersphere/api/dto/mock/MockApiUtils.java @@ -64,8 +64,8 @@ public class MockApiUtils { } } - public static JSONArray getExpectBodyParams(JSONObject bodyObj) { - JSONArray returnJsonArray = new JSONArray(); + public static JSON getExpectBodyParams(JSONObject bodyObj) { + JSON returnJson = new JSONArray(); try { String type = bodyObj.getString("type"); @@ -92,17 +92,18 @@ public class MockApiUtils { } JSONValidator jsonValidator = JSONValidator.from(jsonString); if (StringUtils.equalsIgnoreCase("Array", jsonValidator.getType().name())) { - returnJsonArray = JSONArray.parseArray(jsonString); + returnJson = JSONArray.parseArray(jsonString); } else if (StringUtils.equalsIgnoreCase("Object", jsonValidator.getType().name())) { JSONObject jsonObject = JSONObject.parseObject(jsonString); - returnJsonArray.add(jsonObject); + returnJson = jsonObject; } } else if (StringUtils.equalsIgnoreCase(type, "XML")) { if (bodyObj.containsKey("raw")) { String xmlStr = bodyObj.getString("raw"); JSONObject matchObj = XMLUtils.XmlToJson(xmlStr); - returnJsonArray.add(matchObj); +// returnJsonArray.add(matchObj); + returnJson = matchObj; } } else if (StringUtils.equalsIgnoreCase(type, "Raw")) { if (bodyObj.containsKey("raw")) { @@ -110,7 +111,8 @@ public class MockApiUtils { if(StringUtils.isNotEmpty(raw)){ JSONObject rawObject = new JSONObject(); rawObject.put("raw",raw); - returnJsonArray.add(rawObject); +// returnJsonArray.add(rawObject); + returnJson = rawObject; } } } else if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM")) { @@ -132,13 +134,14 @@ public class MockApiUtils { bodyParamArr.put(kv.getString("name"), values); } } - returnJsonArray.add(bodyParamArr); +// returnJsonArray.add(bodyParamArr); + returnJson = bodyParamArr; } } }catch (Exception e){} - return returnJsonArray; + return returnJson; } public static JSONObject parseJsonSchema(JSONObject bodyReturnObj) { @@ -464,7 +467,7 @@ public class MockApiUtils { } public static JSON getPostParamMap(HttpServletRequest request) { - if (StringUtils.equalsIgnoreCase("application/JSON", request.getContentType())) { + if (StringUtils.startsWithIgnoreCase(request.getContentType(),"application/JSON")) { JSON returnJson = null; try { String param = getRequestPostStr(request); @@ -478,7 +481,7 @@ public class MockApiUtils { e.printStackTrace(); } return returnJson; - } else if (StringUtils.equalsIgnoreCase("text/xml", request.getContentType())) { + } else if (StringUtils.startsWithIgnoreCase(request.getContentType(),"text/xml")) { String xmlString = readXml(request); org.json.JSONObject xmlJSONObj = XML.toJSONObject(xmlString); @@ -489,7 +492,7 @@ public class MockApiUtils { } catch (Exception e) { } return object; - } else if (StringUtils.equalsIgnoreCase("application/x-www-form-urlencoded", request.getContentType())) { + } else if (StringUtils.startsWithIgnoreCase( request.getContentType(),"application/x-www-form-urlencoded")) { JSONObject object = new JSONObject(); Enumeration paramNameItor = request.getParameterNames(); while (paramNameItor.hasMoreElements()) { @@ -498,7 +501,7 @@ public class MockApiUtils { object.put(key, value); } return object; - } else if (StringUtils.equalsIgnoreCase("text/plain", request.getContentType())) { + } else if (StringUtils.startsWithIgnoreCase(request.getContentType(),"text/plain")) { JSONObject object = new JSONObject(); String bodyParam = readBody(request); if(StringUtils.isNotEmpty(bodyParam)){ diff --git a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java index 1685897c13..aac3cea52e 100644 --- a/backend/src/main/java/io/metersphere/api/service/MockConfigService.java +++ b/backend/src/main/java/io/metersphere/api/service/MockConfigService.java @@ -371,10 +371,17 @@ public class MockConfigService { if(expectParamsObj.containsKey("body")){ JSONObject expectBodyObject = expectParamsObj.getJSONObject("body"); - JSONArray mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject); + JSON mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject); JSONArray jsonArray = requestMockParams.getBodyParams(); - if(!JsonStructUtils.checkJsonArrayCompliance(jsonArray, mockExpectJsonArray)){ - return false; + + if (mockExpectJsonArray instanceof JSONObject) { + if(!JsonStructUtils.checkJsonArrayCompliance(jsonArray, (JSONObject) mockExpectJsonArray)){ + return false; + } + }else if (mockExpectJsonArray instanceof JSONArray) { + if(!JsonStructUtils.checkJsonArrayCompliance(jsonArray, (JSONArray)mockExpectJsonArray)){ + return false; + } } }