Skip to content

Commit 124293f

Browse files
committed
录制接口用例时忽略不需要的请求头
1 parent dc375a0 commit 124293f

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

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

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,13 +1313,19 @@ public void remove(String key) {
13131313
public static final String ADD_COOKIE = "Add-Cookie";
13141314
public static final String APIJSON_DELEGATE_ID = "Apijson-Delegate-Id"; // 有些 Web 框架会强制把全大写改为全小写或大驼峰
13151315
public static final List<String> EXCEPT_HEADER_LIST;
1316+
public static final List<String> IGNORE_HEADER_LIST;
13161317
static {
13171318
SESSION_MAP = new SessionMap();
13181319

13191320
EXCEPT_HEADER_LIST = Arrays.asList( //accept-encoding 在某些情况下导致乱码,origin 和 sec-fetch-mode 等 CORS 信息导致服务器代理失败
13201321
"accept-encoding", "accept-language", "content-length", "dnt", // "accept", "connection"
13211322
"host", "origin", "referer", "user-agent", "sec-fetch-mode", "sec-fetch-site", "sec-fetch-dest", "sec-fetch-user"
13221323
);
1324+
IGNORE_HEADER_LIST = Arrays.asList(
1325+
"accept-encoding", "accept-language", "content-length", "dnt", "accept", "connection", "cookie", "token",
1326+
"host", "origin", "referer", "user-agent", "sec-fetch-mode", "sec-fetch-site", "sec-fetch-dest", "sec-fetch-user",
1327+
"authorization", "x-authorization", "authentication", "x-authentication"
1328+
);
13231329
}
13241330

13251331
public static final int REQUEST_RECORD_TYPE_NULL = 0;
@@ -1463,18 +1469,20 @@ else if (body != null && "DATA".equals(type)) { // if (StringUtil.isNotEmpty(bod
14631469
}
14641470

14651471
String name = line.substring(0, ind).trim();
1472+
String n = name == null ? null : name.toLowerCase();
14661473
String h = line.substring(ind + 1).trim();
14671474

1468-
if (SET_COOKIE.toLowerCase().equals(name.toLowerCase())) { //接收到时就已经被强制小写
1475+
if (SET_COOKIE.toLowerCase().equals(n)) { //接收到时就已经被强制小写
14691476
setCookie = Arrays.asList(h); // JSON.parseArray(request.getHeader(name), String.class);
1470-
} else if (ADD_COOKIE.toLowerCase().equals(name.toLowerCase())) {
1477+
} else if (ADD_COOKIE.toLowerCase().equals(n)) {
14711478
addCookie = Arrays.asList(h);
1472-
} else if (APIJSON_DELEGATE_ID.toLowerCase().equals(name.toLowerCase())) {
1479+
} else if (APIJSON_DELEGATE_ID.toLowerCase().equals(n)) {
14731480
apijsonDelegateId = Arrays.asList(h);
14741481
} else {
14751482
headers.put(name, Arrays.asList(h));
14761483

1477-
if (recordType != 0) {
1484+
if (recordType != 0 && ! (n.equals("cookie") || n.equals("token") || n.equals("authorization")
1485+
|| n.equals("authentication") || n.startsWith("x-") || (isSQL != true && IGNORE_HEADER_LIST.contains(n)))) {
14781486
if (isSQL) {
14791487
hm.put(name, h);
14801488

@@ -1503,20 +1511,22 @@ else if (names != null) {
15031511

15041512
while (names.hasMoreElements()) {
15051513
String name = names.nextElement();
1506-
if (name != null && exceptHeaderList.contains(name.toLowerCase()) == false) {
1514+
String n = name == null ? null : name.toLowerCase();
1515+
if (n != null && exceptHeaderList.contains(n) == false) {
15071516
//APIAuto 是一定精准发送 Set-Cookie 名称过来的,预留其它命名可实现覆盖原 Cookie Header 等更多可能
15081517

15091518
String h = httpServletRequest.getHeader(name);
1510-
if (SET_COOKIE.toLowerCase().equals(name.toLowerCase())) { //接收到时就已经被强制小写
1519+
if (SET_COOKIE.toLowerCase().equals(n)) { //接收到时就已经被强制小写
15111520
setCookie = Arrays.asList(h); // JSON.parseArray(request.getHeader(name), String.class);
1512-
} else if (ADD_COOKIE.toLowerCase().equals(name.toLowerCase())) {
1521+
} else if (ADD_COOKIE.toLowerCase().equals(n)) {
15131522
addCookie = Arrays.asList(h);
1514-
} else if (APIJSON_DELEGATE_ID.toLowerCase().equals(name.toLowerCase())) {
1523+
} else if (APIJSON_DELEGATE_ID.toLowerCase().equals(n)) {
15151524
apijsonDelegateId = Arrays.asList(h);
15161525
} else {
15171526
headers.add(name, h);
15181527

1519-
if (recordType != 0) {
1528+
if (recordType != 0 && ! (n.equals("cookie") || n.equals("token") || n.equals("authorization")
1529+
|| n.equals("authentication") || n.startsWith("x-") || (isSQL != true && IGNORE_HEADER_LIST.contains(n)))) {
15201530
if (isSQL) {
15211531
hm.put(name, h);
15221532

0 commit comments

Comments
 (0)