fix(Mock测试): 修复旧数据升级后匹配方式变全匹配的问题

修复旧数据升级后匹配方式变全匹配的问题
This commit is contained in:
song-tianyang 2021-11-10 15:58:36 +08:00 committed by song-tianyang
parent ece1f7a4aa
commit 5a77ce1d1f
2 changed files with 25 additions and 15 deletions

View File

@ -64,8 +64,8 @@ public class MockApiUtils {
} }
} }
public static JSONArray getExpectBodyParams(JSONObject bodyObj) { public static JSON getExpectBodyParams(JSONObject bodyObj) {
JSONArray returnJsonArray = new JSONArray(); JSON returnJson = new JSONArray();
try { try {
String type = bodyObj.getString("type"); String type = bodyObj.getString("type");
@ -92,17 +92,18 @@ public class MockApiUtils {
} }
JSONValidator jsonValidator = JSONValidator.from(jsonString); JSONValidator jsonValidator = JSONValidator.from(jsonString);
if (StringUtils.equalsIgnoreCase("Array", jsonValidator.getType().name())) { if (StringUtils.equalsIgnoreCase("Array", jsonValidator.getType().name())) {
returnJsonArray = JSONArray.parseArray(jsonString); returnJson = JSONArray.parseArray(jsonString);
} else if (StringUtils.equalsIgnoreCase("Object", jsonValidator.getType().name())) { } else if (StringUtils.equalsIgnoreCase("Object", jsonValidator.getType().name())) {
JSONObject jsonObject = JSONObject.parseObject(jsonString); JSONObject jsonObject = JSONObject.parseObject(jsonString);
returnJsonArray.add(jsonObject); returnJson = jsonObject;
} }
} else if (StringUtils.equalsIgnoreCase(type, "XML")) { } else if (StringUtils.equalsIgnoreCase(type, "XML")) {
if (bodyObj.containsKey("raw")) { if (bodyObj.containsKey("raw")) {
String xmlStr = bodyObj.getString("raw"); String xmlStr = bodyObj.getString("raw");
JSONObject matchObj = XMLUtils.XmlToJson(xmlStr); JSONObject matchObj = XMLUtils.XmlToJson(xmlStr);
returnJsonArray.add(matchObj); // returnJsonArray.add(matchObj);
returnJson = matchObj;
} }
} else if (StringUtils.equalsIgnoreCase(type, "Raw")) { } else if (StringUtils.equalsIgnoreCase(type, "Raw")) {
if (bodyObj.containsKey("raw")) { if (bodyObj.containsKey("raw")) {
@ -110,7 +111,8 @@ public class MockApiUtils {
if(StringUtils.isNotEmpty(raw)){ if(StringUtils.isNotEmpty(raw)){
JSONObject rawObject = new JSONObject(); JSONObject rawObject = new JSONObject();
rawObject.put("raw",raw); rawObject.put("raw",raw);
returnJsonArray.add(rawObject); // returnJsonArray.add(rawObject);
returnJson = rawObject;
} }
} }
} else if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM")) { } else if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM")) {
@ -132,13 +134,14 @@ public class MockApiUtils {
bodyParamArr.put(kv.getString("name"), values); bodyParamArr.put(kv.getString("name"), values);
} }
} }
returnJsonArray.add(bodyParamArr); // returnJsonArray.add(bodyParamArr);
returnJson = bodyParamArr;
} }
} }
}catch (Exception e){} }catch (Exception e){}
return returnJsonArray; return returnJson;
} }
public static JSONObject parseJsonSchema(JSONObject bodyReturnObj) { public static JSONObject parseJsonSchema(JSONObject bodyReturnObj) {
@ -464,7 +467,7 @@ public class MockApiUtils {
} }
public static JSON getPostParamMap(HttpServletRequest request) { public static JSON getPostParamMap(HttpServletRequest request) {
if (StringUtils.equalsIgnoreCase("application/JSON", request.getContentType())) { if (StringUtils.startsWithIgnoreCase(request.getContentType(),"application/JSON")) {
JSON returnJson = null; JSON returnJson = null;
try { try {
String param = getRequestPostStr(request); String param = getRequestPostStr(request);
@ -478,7 +481,7 @@ public class MockApiUtils {
e.printStackTrace(); e.printStackTrace();
} }
return returnJson; return returnJson;
} else if (StringUtils.equalsIgnoreCase("text/xml", request.getContentType())) { } else if (StringUtils.startsWithIgnoreCase(request.getContentType(),"text/xml")) {
String xmlString = readXml(request); String xmlString = readXml(request);
org.json.JSONObject xmlJSONObj = XML.toJSONObject(xmlString); org.json.JSONObject xmlJSONObj = XML.toJSONObject(xmlString);
@ -489,7 +492,7 @@ public class MockApiUtils {
} catch (Exception e) { } catch (Exception e) {
} }
return object; 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(); JSONObject object = new JSONObject();
Enumeration<String> paramNameItor = request.getParameterNames(); Enumeration<String> paramNameItor = request.getParameterNames();
while (paramNameItor.hasMoreElements()) { while (paramNameItor.hasMoreElements()) {
@ -498,7 +501,7 @@ public class MockApiUtils {
object.put(key, value); object.put(key, value);
} }
return object; return object;
} else if (StringUtils.equalsIgnoreCase("text/plain", request.getContentType())) { } else if (StringUtils.startsWithIgnoreCase(request.getContentType(),"text/plain")) {
JSONObject object = new JSONObject(); JSONObject object = new JSONObject();
String bodyParam = readBody(request); String bodyParam = readBody(request);
if(StringUtils.isNotEmpty(bodyParam)){ if(StringUtils.isNotEmpty(bodyParam)){

View File

@ -371,10 +371,17 @@ public class MockConfigService {
if(expectParamsObj.containsKey("body")){ if(expectParamsObj.containsKey("body")){
JSONObject expectBodyObject = expectParamsObj.getJSONObject("body"); JSONObject expectBodyObject = expectParamsObj.getJSONObject("body");
JSONArray mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject); JSON mockExpectJsonArray = MockApiUtils.getExpectBodyParams(expectBodyObject);
JSONArray jsonArray = requestMockParams.getBodyParams(); 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;
}
} }
} }