Skip to content

Commit 6b4b5c0

Browse files
committed
录制 URL query 自动转 JSON,解决有时录制失败
1 parent e0c76ca commit 6b4b5c0

1 file changed

Lines changed: 52 additions & 9 deletions

File tree

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/DemoController.java‎

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,10 +1621,51 @@ else if (names != null) {
16211621
String branch = isSQL ? req.getString("uri") : (isBodyEmpty ? url : rawUrl);
16221622
branch = index < 0 ? branch : (index2 < 0 ? branch.substring(index) : branch.substring(index + 3 + index2));
16231623

1624-
String reqType = isBodyEmpty ? (method == HttpMethod.PUT || method == HttpMethod.DELETE ?
1625-
method.toString() : (method == HttpMethod.POST ? "PARAM"
1624+
int index3 = branch.indexOf("?");
1625+
String query = index3 < 0 ? null : branch.substring(index3 + 1);
1626+
branch = index3 < 0 ? branch : branch.substring(0, index3);
1627+
1628+
try {
1629+
query = URLDecoder.decode(query, StringUtil.UTF_8);
1630+
} catch (Throwable e) {
1631+
e.printStackTrace();
1632+
}
1633+
1634+
String[] kvs = StringUtil.split(query, "&");
1635+
1636+
Map<String, Object> reqBody = new LinkedHashMap<>();
1637+
if (kvs != null) {
1638+
for (int i = 0; i < kvs.length; i++) {
1639+
String kv = kvs[i];
1640+
int ind = kv.indexOf("=");
1641+
String k = ind < 0 ? kv : kv.substring(0, ind);
1642+
Object v = ind < 0 ? null : kv.substring(ind + 1);
1643+
1644+
try {
1645+
v = JSON.parse(v);
1646+
} catch (Throwable e) {
1647+
e.printStackTrace();
1648+
}
1649+
1650+
reqBody.put(k, v);
1651+
}
1652+
}
1653+
1654+
JSONObject bodyObj = null;
1655+
if (! isBodyEmpty) {
1656+
try {
1657+
bodyObj = JSON.parseObject(body);
1658+
if (bodyObj != null && ! bodyObj.isEmpty()) {
1659+
reqBody.putAll(bodyObj);
1660+
}
1661+
} catch (Throwable e) {
1662+
e.printStackTrace();
1663+
}
1664+
}
1665+
1666+
String reqType = isBodyEmpty ? (method == null || method == HttpMethod.GET ? "PARAM"
16261667
: (MediaType.APPLICATION_FORM_URLENCODED.equals(contentType) ? "FORM"
1627-
: (MediaType.MULTIPART_FORM_DATA.equals(contentType) ? "DATA" : "JSON"))) // FIXME 考虑 XML, PNG 等格式?
1668+
: (MediaType.MULTIPART_FORM_DATA.equals(contentType) ? "DATA" : "JSON")) // FIXME 考虑 XML, PNG 等格式?
16281669
) : "JSON";
16291670

16301671
String sql = isSQL ? StringUtil.trim(req.getString("sql")) : null;
@@ -1732,7 +1773,7 @@ else if (names != null) {
17321773
// Document >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
17331774
}
17341775

1735-
JSONObject existRsp = newParser(session, GET).parseResponse(existReq);
1776+
JSONObject existRsp = newParser(session, GET).setNeedVerify(false).parseResponse(existReq);
17361777
JSONResponse existRsp2 = new JSONResponse(existRsp).getJSONResponse("Document");
17371778
long documentId = existRsp2 == null ? 0 : existRsp2.getId();
17381779

@@ -1768,9 +1809,10 @@ else if (recordType > 0) {
17681809
document.put("from", 2); // 0-测试工具,1-CI/CD,2-流量录制
17691810
document.put("name", "[Record] " + new java.util.Date().toLocaleString());
17701811
document.put("type", reqType);
1812+
document.put("method", method);
17711813
document.put("url", branch);
17721814
document.put("header", StringUtil.isEmpty(hs, true) ? null : hs.trim());
1773-
document.put("request", isSQL ? "{}" : (isBodyEmpty ? JSON.toJSONString(map) : body));
1815+
document.put("request", isSQL ? "{}" : reqBody);
17741816
if (isSQL) {
17751817
// 没有名称,除非 args 传对象而不是数组
17761818
// JSONList args = req.getJSONArray("args");
@@ -1794,7 +1836,7 @@ else if (recordType > 0) {
17941836
}
17951837
else {
17961838
// Random <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
1797-
Map<String, ?> m = isSQL ? null : (isBodyEmpty ? map : JSON.parseObject(body));
1839+
Map<String, ?> m = isSQL ? null : reqBody;
17981840
String config = isSQL ? hs : parseRandomConfig("", m);
17991841

18001842
JSONObject random = JSON.newJSONObject();
@@ -1838,7 +1880,7 @@ else if (recordType > 0) {
18381880
JSONObject randomReq = JSON.newJSONObject();
18391881
randomReq.put("Random", random);
18401882

1841-
JSONObject rsp = newParser(session, GET).parseResponse(randomReq);
1883+
JSONObject rsp = newParser(session, GET).setNeedVerify(false).parseResponse(randomReq);
18421884
JSONObject rsp2 = rsp == null ? null : rsp.getJSONObject("Random");
18431885
randomId = rsp2 == null ? 0 : rsp2.getLongValue("id");
18441886
}
@@ -1862,11 +1904,12 @@ else if (recordType > 0) {
18621904
else { // TestRecord <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
18631905
testRecord.put("from", 2); // 0-接口工具,1-CI/CD,2-流量录制
18641906
testRecord.put("host", host);
1865-
testRecord.put("response", rspBody); // 用 JSONRequest.put 会转为 JSONMap
1907+
testRecord.put("response", rspBody == null ? "" : rspBody); // 用 JSONRequest.put 会转为 JSONMap
18661908
} // TestRecord >>>>>>>>>>>>>>>>>>>>>>>>>>>>>
18671909

18681910
request.put("TestRecord", testRecord);
1869-
JSONObject rsp = newParser(session, recordType < 0 ? GET : POST).parseResponse(request);
1911+
JSONObject rsp = newParser(session, recordType < 0 ? GET : POST)
1912+
.setNeedVerify(false).setNeedVerifyContent(true).parseResponse(request);
18701913
if (recordType < 0) {
18711914
JSONObject rsp2 = rsp == null ? null : rsp.getJSONObject("TestRecord");
18721915
String response = rsp2 == null ? null : rsp2.getString("response");

0 commit comments

Comments
 (0)