fix(Mock测试): 修复Mock匹配是json-schema格式时匹配不到的问题
--bug=1008274 --user=宋天阳 【接口测试】mock期望开启json-schema后,匹配不到 https://www.tapd.cn/55049933/s/1073930
This commit is contained in:
parent
b2b44ef40f
commit
f3d97eaaa0
|
@ -26,36 +26,36 @@ import java.util.*;
|
|||
* @Date 2021/10/14 3:00 下午
|
||||
*/
|
||||
public class MockApiUtils {
|
||||
public static Map<String,String> getHttpRequestHeader(HttpServletRequest request){
|
||||
Map<String,String> returnMap = new HashMap<>();
|
||||
public static Map<String, String> getHttpRequestHeader(HttpServletRequest request) {
|
||||
Map<String, String> returnMap = new HashMap<>();
|
||||
Enumeration<String> headers = request.getHeaderNames();
|
||||
while (headers.hasMoreElements()){
|
||||
while (headers.hasMoreElements()) {
|
||||
String header = headers.nextElement();
|
||||
String headerValue = request.getHeader(header);
|
||||
returnMap.put(header,headerValue);
|
||||
returnMap.put(header, headerValue);
|
||||
}
|
||||
return returnMap;
|
||||
}
|
||||
|
||||
public static boolean matchRequestHeader(JSONArray mockExpectHeaderArray, Map<String, String> requestHeaderMap) {
|
||||
Map<String,String> mockExpectHeaders = new HashMap<>();
|
||||
for(int i = 0; i < mockExpectHeaderArray.size(); i++){
|
||||
Map<String, String> mockExpectHeaders = new HashMap<>();
|
||||
for (int i = 0; i < mockExpectHeaderArray.size(); i++) {
|
||||
JSONObject obj = mockExpectHeaderArray.getJSONObject(i);
|
||||
if(obj.containsKey("name") && obj.containsKey("value")){
|
||||
mockExpectHeaders.put(obj.getString("name"),obj.getString("value"));
|
||||
if (obj.containsKey("name") && obj.containsKey("value")) {
|
||||
mockExpectHeaders.put(obj.getString("name"), obj.getString("value"));
|
||||
}
|
||||
}
|
||||
if(MapUtils.isEmpty(requestHeaderMap) && MapUtils.isNotEmpty(mockExpectHeaders)){
|
||||
if (MapUtils.isEmpty(requestHeaderMap) && MapUtils.isNotEmpty(mockExpectHeaders)) {
|
||||
return false;
|
||||
}else {
|
||||
for (Map.Entry<String, String> entry: mockExpectHeaders.entrySet()){
|
||||
} else {
|
||||
for (Map.Entry<String, String> entry : mockExpectHeaders.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
String value = entry.getValue();
|
||||
|
||||
if(!requestHeaderMap.containsKey(key)){
|
||||
if (!requestHeaderMap.containsKey(key)) {
|
||||
return false;
|
||||
}else {
|
||||
if(!StringUtils.equals(value,requestHeaderMap.get(key))){
|
||||
} else {
|
||||
if (!StringUtils.equals(value, requestHeaderMap.get(key))) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -81,8 +81,8 @@ public class MockApiUtils {
|
|||
|
||||
String jsonString = "";
|
||||
if (isJsonSchema) {
|
||||
if (bodyObj.containsKey("jsonSchema") && bodyObj.getJSONObject("jsonSchema").containsKey("properties")) {
|
||||
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").getJSONObject("properties").toJSONString();
|
||||
if (bodyObj.containsKey("jsonSchema")) {
|
||||
String bodyRetunStr = bodyObj.getJSONObject("jsonSchema").toJSONString();
|
||||
jsonString = JSONSchemaGenerator.getJson(bodyRetunStr);
|
||||
}
|
||||
} else {
|
||||
|
@ -102,15 +102,15 @@ public class MockApiUtils {
|
|||
if (bodyObj.containsKey("raw")) {
|
||||
String xmlStr = bodyObj.getString("raw");
|
||||
JSONObject matchObj = XMLUtils.XmlToJson(xmlStr);
|
||||
returnJson = matchObj;
|
||||
returnJson = matchObj;
|
||||
}
|
||||
} else if (StringUtils.equalsIgnoreCase(type, "Raw")) {
|
||||
} else if (StringUtils.equalsIgnoreCase(type, "Raw")) {
|
||||
if (bodyObj.containsKey("raw")) {
|
||||
String raw = bodyObj.getString("raw");
|
||||
if(StringUtils.isNotEmpty(raw)){
|
||||
if (StringUtils.isNotEmpty(raw)) {
|
||||
JSONObject rawObject = new JSONObject();
|
||||
rawObject.put("raw",raw);
|
||||
returnJson = rawObject;
|
||||
rawObject.put("raw", raw);
|
||||
returnJson = rawObject;
|
||||
}
|
||||
}
|
||||
} else if (StringUtils.equalsAnyIgnoreCase(type, "Form Data", "WWW_FORM")) {
|
||||
|
@ -132,13 +132,14 @@ public class MockApiUtils {
|
|||
bodyParamArr.put(kv.getString("name"), values);
|
||||
}
|
||||
}
|
||||
returnJson = bodyParamArr;
|
||||
returnJson = bodyParamArr;
|
||||
}
|
||||
}
|
||||
}catch (Exception e){}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
|
||||
|
||||
return returnJson;
|
||||
return returnJson;
|
||||
}
|
||||
|
||||
public static JSONObject parseJsonSchema(JSONObject bodyReturnObj) {
|
||||
|
@ -208,10 +209,10 @@ public class MockApiUtils {
|
|||
|
||||
public static JSONObject getParamsByJSONArray(JSONArray array) {
|
||||
JSONObject returnObject = new JSONObject();
|
||||
for(int i = 0; i < array.size();i ++){
|
||||
for (int i = 0; i < array.size(); i++) {
|
||||
JSONObject obj = array.getJSONObject(i);
|
||||
if(obj.containsKey("name") && obj.containsKey("value")){
|
||||
returnObject.put(obj.getString("name"),obj.getString("value"));
|
||||
if (obj.containsKey("name") && obj.containsKey("value")) {
|
||||
returnObject.put(obj.getString("name"), obj.getString("value"));
|
||||
}
|
||||
}
|
||||
return returnObject;
|
||||
|
@ -220,7 +221,7 @@ public class MockApiUtils {
|
|||
public static Map<String, String> getApiResponse(String response) {
|
||||
Map<String, String> returnMap = new HashMap<>();
|
||||
String returnStr = "";
|
||||
if(StringUtils.isNotEmpty(response)){
|
||||
if (StringUtils.isNotEmpty(response)) {
|
||||
try {
|
||||
JSONObject respObj = JSONObject.parseObject(response);
|
||||
if (respObj.containsKey("body")) {
|
||||
|
@ -278,26 +279,26 @@ public class MockApiUtils {
|
|||
}
|
||||
}
|
||||
}
|
||||
}catch (Exception e){
|
||||
} catch (Exception e) {
|
||||
MSException.throwException(e);
|
||||
}
|
||||
}
|
||||
returnMap.put("returnMsg",returnStr);
|
||||
returnMap.put("returnMsg", returnStr);
|
||||
return returnMap;
|
||||
}
|
||||
|
||||
public static String getResultByResponseResult(JSONObject bodyObj,String url, Map<String,String> headerMap,RequestMockParams requestMockParams) {
|
||||
if(headerMap == null){
|
||||
public static String getResultByResponseResult(JSONObject bodyObj, String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
|
||||
if (headerMap == null) {
|
||||
headerMap = new HashMap<>();
|
||||
}
|
||||
if(requestMockParams == null){
|
||||
if (requestMockParams == null) {
|
||||
requestMockParams = new RequestMockParams();
|
||||
}
|
||||
if(bodyObj == null && bodyObj.isEmpty()){
|
||||
if (bodyObj == null && bodyObj.isEmpty()) {
|
||||
return "";
|
||||
}else {
|
||||
} else {
|
||||
String returnStr = "";
|
||||
if(bodyObj.containsKey("type")){
|
||||
if (bodyObj.containsKey("type")) {
|
||||
String type = bodyObj.getString("type");
|
||||
if (StringUtils.equals(type, "JSON")) {
|
||||
//判断是否是JsonSchema
|
||||
|
@ -320,17 +321,17 @@ public class MockApiUtils {
|
|||
returnStr = bodyObj.getString("raw");
|
||||
}
|
||||
}
|
||||
} else if(StringUtils.equalsAnyIgnoreCase(type,"Raw")){
|
||||
} else if (StringUtils.equalsAnyIgnoreCase(type, "Raw")) {
|
||||
if (bodyObj.containsKey("raw")) {
|
||||
String raw = bodyObj.getString("raw");
|
||||
returnStr = raw;
|
||||
}
|
||||
} else if(StringUtils.equalsAnyIgnoreCase(type,"XML")){
|
||||
} else if (StringUtils.equalsAnyIgnoreCase(type, "XML")) {
|
||||
if (bodyObj.containsKey("xmlHeader")) {
|
||||
String xmlHeader = bodyObj.getString("xmlHeader");
|
||||
if(!StringUtils.startsWith(xmlHeader,"<?") && !StringUtils.endsWith(xmlHeader,"?>")){
|
||||
returnStr = "<?"+xmlHeader+"?>\r\n";
|
||||
}else {
|
||||
if (!StringUtils.startsWith(xmlHeader, "<?") && !StringUtils.endsWith(xmlHeader, "?>")) {
|
||||
returnStr = "<?" + xmlHeader + "?>\r\n";
|
||||
} else {
|
||||
returnStr = xmlHeader;
|
||||
}
|
||||
}
|
||||
|
@ -338,25 +339,25 @@ public class MockApiUtils {
|
|||
String raw = bodyObj.getString("xmlRaw");
|
||||
returnStr = returnStr + raw;
|
||||
}
|
||||
} else if(StringUtils.equalsAnyIgnoreCase(type,"fromApi")){
|
||||
} else if (StringUtils.equalsAnyIgnoreCase(type, "fromApi")) {
|
||||
if (bodyObj.containsKey("apiRspRaw")) {
|
||||
String raw = bodyObj.getString("apiRspRaw");
|
||||
returnStr = raw;
|
||||
}
|
||||
} else if(StringUtils.equalsAnyIgnoreCase(type,"script")){
|
||||
} else if (StringUtils.equalsAnyIgnoreCase(type, "script")) {
|
||||
if (bodyObj.containsKey("scriptObject")) {
|
||||
JSONObject scriptObj = bodyObj.getJSONObject("scriptObject");
|
||||
String script = scriptObj.getString("script");
|
||||
String scriptLanguage =scriptObj.getString("scriptLanguage");
|
||||
String scriptLanguage = scriptObj.getString("scriptLanguage");
|
||||
|
||||
String baseScript = parseScript(url,headerMap,requestMockParams);
|
||||
String baseScript = parseScript(url, headerMap, requestMockParams);
|
||||
try {
|
||||
script = baseScript + script;
|
||||
if(StringUtils.isEmpty(scriptLanguage)){
|
||||
if (StringUtils.isEmpty(scriptLanguage)) {
|
||||
scriptLanguage = "beanshell";
|
||||
}
|
||||
returnStr = runScript(script,scriptLanguage);
|
||||
}catch (Exception e){
|
||||
returnStr = runScript(script, scriptLanguage);
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
}
|
||||
}
|
||||
|
@ -367,58 +368,59 @@ public class MockApiUtils {
|
|||
}
|
||||
}
|
||||
|
||||
private static String parseScript(String url,Map<String,String> headerMap,RequestMockParams requestMockParams) {
|
||||
private static String parseScript(String url, Map<String, String> headerMap, RequestMockParams requestMockParams) {
|
||||
StringBuffer scriptStringBuffer = new StringBuffer();
|
||||
scriptStringBuffer.append("import java.util.HashMap;\n\n");
|
||||
scriptStringBuffer.append("HashMap requestParams = new HashMap();\n");
|
||||
scriptStringBuffer.append("requestParams.put(\"address\",\""+url+"\");\n");
|
||||
scriptStringBuffer.append("requestParams.put(\"address\",\"" + url + "\");\n");
|
||||
//写入请求头
|
||||
for (Map.Entry<String, String> headEntry: headerMap.entrySet()){
|
||||
for (Map.Entry<String, String> headEntry : headerMap.entrySet()) {
|
||||
String headerKey = headEntry.getKey();
|
||||
String headerValue = headEntry.getValue();
|
||||
scriptStringBuffer.append("requestParams.put(\"header."+headerKey+"\",\""+headerValue+"\");\n");
|
||||
scriptStringBuffer.append("requestParams.put(\"header." + headerKey + "\",\"" + headerValue + "\");\n");
|
||||
}
|
||||
//写入body参数
|
||||
if(requestMockParams.getBodyParams() != null){
|
||||
if(requestMockParams.getBodyParams().size() == 1){
|
||||
if (requestMockParams.getBodyParams() != null) {
|
||||
if (requestMockParams.getBodyParams().size() == 1) {
|
||||
//参数是jsonObject
|
||||
JSONObject bodyParamObj = requestMockParams.getBodyParams().getJSONObject(0);
|
||||
for(String key : bodyParamObj.keySet()){
|
||||
for (String key : bodyParamObj.keySet()) {
|
||||
String value = String.valueOf(bodyParamObj.get(key));
|
||||
value = StringUtils.replace(value,"\\","\\\\");
|
||||
value = StringUtils.replace(value,"\"","\\\"");
|
||||
scriptStringBuffer.append("requestParams.put(\"body."+key+"\",\""+value+"\");\n");
|
||||
if(StringUtils.equalsIgnoreCase(key,"raw")){
|
||||
scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\""+value+"\");\n");
|
||||
value = StringUtils.replace(value, "\\", "\\\\");
|
||||
value = StringUtils.replace(value, "\"", "\\\"");
|
||||
scriptStringBuffer.append("requestParams.put(\"body." + key + "\",\"" + value + "\");\n");
|
||||
if (StringUtils.equalsIgnoreCase(key, "raw")) {
|
||||
scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\"" + value + "\");\n");
|
||||
}
|
||||
}
|
||||
String jsonBody = bodyParamObj.toJSONString();
|
||||
jsonBody = StringUtils.replace(jsonBody,"\\","\\\\");
|
||||
jsonBody = StringUtils.replace(jsonBody,"\"","\\\"");
|
||||
scriptStringBuffer.append("requestParams.put(\"body.json\",\""+jsonBody+"\");\n");
|
||||
}else {
|
||||
scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\""+requestMockParams.getBodyParams().toJSONString()+"\");\n");
|
||||
jsonBody = StringUtils.replace(jsonBody, "\\", "\\\\");
|
||||
jsonBody = StringUtils.replace(jsonBody, "\"", "\\\"");
|
||||
scriptStringBuffer.append("requestParams.put(\"body.json\",\"" + jsonBody + "\");\n");
|
||||
} else {
|
||||
scriptStringBuffer.append("requestParams.put(\"bodyRaw\",\"" + requestMockParams.getBodyParams().toJSONString() + "\");\n");
|
||||
}
|
||||
|
||||
}
|
||||
//写入query参数
|
||||
if(requestMockParams.getQueryParamsObj() != null){
|
||||
if (requestMockParams.getQueryParamsObj() != null) {
|
||||
JSONObject queryParamsObj = requestMockParams.getQueryParamsObj();
|
||||
for(String key : queryParamsObj.keySet()){
|
||||
for (String key : queryParamsObj.keySet()) {
|
||||
String value = String.valueOf(queryParamsObj.get(key));
|
||||
scriptStringBuffer.append("requestParams.put(\"query."+key+"\",\""+value+"\");\n");
|
||||
scriptStringBuffer.append("requestParams.put(\"query." + key + "\",\"" + value + "\");\n");
|
||||
}
|
||||
}
|
||||
//写入rest参数
|
||||
if(requestMockParams.getRestParamsObj() != null){
|
||||
if (requestMockParams.getRestParamsObj() != null) {
|
||||
JSONObject restParamsObj = requestMockParams.getRestParamsObj();
|
||||
for(String key : restParamsObj.keySet()){
|
||||
for (String key : restParamsObj.keySet()) {
|
||||
String value = String.valueOf(restParamsObj.get(key));
|
||||
scriptStringBuffer.append("requestParams.put(\"rest."+key+"\",\""+value+"\");\n");
|
||||
scriptStringBuffer.append("requestParams.put(\"rest." + key + "\",\"" + value + "\");\n");
|
||||
}
|
||||
}
|
||||
return scriptStringBuffer.toString();
|
||||
}
|
||||
|
||||
private static String runScript(String script, String scriptLanguage) throws ScriptException {
|
||||
JSR223Sampler jmeterScriptSampler = new JSR223Sampler();
|
||||
jmeterScriptSampler.setScriptLanguage(scriptLanguage);
|
||||
|
@ -428,18 +430,18 @@ public class MockApiUtils {
|
|||
|
||||
}
|
||||
|
||||
public static RequestMockParams getParams(String urlParams, String apiPath, JSONObject queryParamsObject,JSON paramJson){
|
||||
RequestMockParams returnParams = getGetParamMap(urlParams,apiPath,queryParamsObject);
|
||||
if(paramJson != null){
|
||||
public static RequestMockParams getParams(String urlParams, String apiPath, JSONObject queryParamsObject, JSON paramJson) {
|
||||
RequestMockParams returnParams = getGetParamMap(urlParams, apiPath, queryParamsObject);
|
||||
if (paramJson != null) {
|
||||
if (paramJson instanceof JSONObject) {
|
||||
if(!((JSONObject) paramJson).isEmpty()){
|
||||
if (!((JSONObject) paramJson).isEmpty()) {
|
||||
JSONArray paramsArray = new JSONArray();
|
||||
paramsArray.add(paramJson);
|
||||
returnParams.setBodyParams(paramsArray);
|
||||
}
|
||||
} else if (paramJson instanceof JSONArray) {
|
||||
JSONArray paramArray = (JSONArray) paramJson;
|
||||
if(!paramArray.isEmpty()){
|
||||
if (!paramArray.isEmpty()) {
|
||||
returnParams.setBodyParams(paramArray);
|
||||
}
|
||||
}
|
||||
|
@ -447,7 +449,7 @@ public class MockApiUtils {
|
|||
return returnParams;
|
||||
}
|
||||
|
||||
public static JSONObject getParameterJsonObject(HttpServletRequest request){
|
||||
public static JSONObject getParameterJsonObject(HttpServletRequest request) {
|
||||
JSONObject queryParamsObject = new JSONObject();
|
||||
Enumeration<String> paramNameItor = request.getParameterNames();
|
||||
while (paramNameItor.hasMoreElements()) {
|
||||
|
@ -469,11 +471,11 @@ public class MockApiUtils {
|
|||
}
|
||||
|
||||
public static JSON getPostParamMap(HttpServletRequest request) {
|
||||
if (StringUtils.startsWithIgnoreCase(request.getContentType(),"application/JSON")) {
|
||||
if (StringUtils.startsWithIgnoreCase(request.getContentType(), "application/JSON")) {
|
||||
JSON returnJson = null;
|
||||
try {
|
||||
String param = getRequestPostStr(request);
|
||||
if(StringUtils.isNotEmpty(param)){
|
||||
if (StringUtils.isNotEmpty(param)) {
|
||||
JSONValidator jsonValidator = JSONValidator.from(param);
|
||||
if (StringUtils.equalsIgnoreCase("Array", jsonValidator.getType().name())) {
|
||||
returnJson = JSONArray.parseArray(param);
|
||||
|
@ -485,7 +487,7 @@ public class MockApiUtils {
|
|||
LogUtil.error(e);
|
||||
}
|
||||
return returnJson;
|
||||
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(),"text/xml")) {
|
||||
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "text/xml")) {
|
||||
String xmlString = readXml(request);
|
||||
|
||||
org.json.JSONObject xmlJSONObj = XML.toJSONObject(xmlString);
|
||||
|
@ -496,7 +498,7 @@ public class MockApiUtils {
|
|||
} catch (Exception e) {
|
||||
}
|
||||
return object;
|
||||
} else if (StringUtils.startsWithIgnoreCase( request.getContentType(),"application/x-www-form-urlencoded")) {
|
||||
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "application/x-www-form-urlencoded")) {
|
||||
JSONObject object = new JSONObject();
|
||||
Enumeration<String> paramNameItor = request.getParameterNames();
|
||||
while (paramNameItor.hasMoreElements()) {
|
||||
|
@ -505,19 +507,19 @@ public class MockApiUtils {
|
|||
object.put(key, value);
|
||||
}
|
||||
return object;
|
||||
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(),"text/plain")) {
|
||||
} else if (StringUtils.startsWithIgnoreCase(request.getContentType(), "text/plain")) {
|
||||
JSONObject object = new JSONObject();
|
||||
String bodyParam = readBody(request);
|
||||
if(StringUtils.isNotEmpty(bodyParam)){
|
||||
object.put("raw",bodyParam);
|
||||
if (StringUtils.isNotEmpty(bodyParam)) {
|
||||
object.put("raw", bodyParam);
|
||||
}
|
||||
return object;
|
||||
|
||||
} else {
|
||||
JSONObject object = new JSONObject();
|
||||
String bodyParam = readBody(request);
|
||||
if(StringUtils.isNotEmpty(bodyParam)){
|
||||
object.put("raw",bodyParam);
|
||||
if (StringUtils.isNotEmpty(bodyParam)) {
|
||||
object.put("raw", bodyParam);
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
@ -548,6 +550,7 @@ public class MockApiUtils {
|
|||
}
|
||||
return returnJson;
|
||||
}
|
||||
|
||||
private static String readBody(HttpServletRequest request) {
|
||||
String result = "";
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue