Repository navigation
Expand file tree
/
Copy pathgm_xhr_cookie_test.js
More file actions
414 lines (343 loc) · 11 KB
/
Copy pathgm_xhr_cookie_test.js
File metadata and controls
414 lines (343 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// ==UserScript==
// @name GM_xmlhttpRequest cookie 覆盖测试 - tampermonkey/tampermonkey#2754 #2829
// @namespace tm-gmxhr-cookie-test
// @version 1.0.3
// @description 验证 GM_xmlhttpRequest 的 cookie 参数语义:脚本指定的名称完全覆盖,未指定的名称原样保留(含同名多值场景)
// @match https://mockhttp.org/*?GM_XHR_COOKIE_TEST_SC
// @grant GM_xmlhttpRequest
// @require https://cdn.jsdelivr.net/gh/scriptscat/scriptcat@36ab4ce5ff23c820a32cd13ac5a04d8834ab4d82/example/tests/lib/sctest.js
// @connect mockhttp.org
// @noframes
// ==/UserScript==
(async function () {
"use strict";
const { describe, check, expect, run } = SCTest.create({ name: "GM_xmlhttpRequest cookie 覆盖测试" });
const MOCKHTTP = "https://mockhttp.org";
function gmRequest(details) {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
...details,
onload: (response) => {
if (response.status >= 200 && response.status < 300) {
resolve(response);
} else {
reject(new Error(`HTTP 请求失败: ${response.status} ${response.statusText || ""}`));
}
},
onerror: (response) => {
reject(new Error(`GM_xmlhttpRequest 网络错误: ${response?.error || response?.statusText || "未知错误"}`));
},
ontimeout: () => {
reject(new Error("GM_xmlhttpRequest 请求超时"));
},
});
});
}
/**
* 解析 mockhttp.org/headers 响应。
*/
function getResponseHeadersBody(response) {
let body;
try {
body = JSON.parse(response.responseText);
} catch (error) {
throw new Error(`mockhttp.org/headers 返回的内容不是有效 JSON: ${response.responseText}`);
}
if (!body || typeof body !== "object" || Array.isArray(body)) {
throw new Error(`mockhttp.org/headers 返回了非预期响应: ${response.responseText}`);
}
if (body.headers && typeof body.headers === "object" && !Array.isArray(body.headers)) {
return body.headers;
}
return body;
}
function getHeaderCaseInsensitive(headers, headerName) {
const targetName = headerName.toLowerCase();
for (const [name, value] of Object.entries(headers)) {
if (name.toLowerCase() === targetName) {
return value;
}
}
return undefined;
}
function getCookieHeader(response) {
const headers = getResponseHeadersBody(response);
const cookieHeader = getHeaderCaseInsensitive(headers, "cookie");
if (cookieHeader == null) {
return "";
}
if (Array.isArray(cookieHeader)) {
return cookieHeader.join("; ");
}
return String(cookieHeader);
}
/**
* 解析 Cookie 请求头字符串为 name -> value[] 的多重映射。
*
* 同名 Cookie 多值时保留全部值,不假设顺序。
*/
function parseCookieMultiMap(cookieHeader) {
const map = new Map();
for (const part of String(cookieHeader || "").split(";")) {
const trimmed = part.trim();
if (!trimmed) {
continue;
}
const separatorIndex = trimmed.indexOf("=");
if (separatorIndex === -1) {
continue;
}
const name = trimmed.slice(0, separatorIndex).trim();
const value = trimmed.slice(separatorIndex + 1).trim();
if (!name) {
continue;
}
if (!map.has(name)) {
map.set(name, []);
}
map.get(name).push(value);
}
return map;
}
function assertCookieValues(map, name, expectedValues, message) {
const actual = (map.get(name) || []).slice().sort();
const expected = expectedValues.slice().sort();
expect(JSON.stringify(actual)).toBe(JSON.stringify(expected));
}
// ---------- Cookie 辅助函数 ----------
//
// 借由不同的 path 属性,在同一名称下制造多值 Cookie。
// 浏览器允许同名 Cookie 因 path 不同而共存。
//
// path=/ 和 path=/headers 都会匹配:
// https://mockhttp.org/headers
const ROOT = "/";
const SUB = "/headers";
function setCookie(name, value, path) {
document.cookie = `${name}=${value}; path=${path}; SameSite=Lax`;
}
function clearCookie(name, path) {
document.cookie = `${name}=; path=${path}; max-age=0; SameSite=Lax`;
}
// 测试 Cookie 名称使用 mBS 格式:
//
// B = 浏览器已有的同名 Cookie 数量:0、1、2
// S = GM_xmlhttpRequest cookie 参数指定的值数量:
// 0 表示未指定,1 表示单值,2 表示多值
const NAMES = ["m00", "m01", "m02", "m10", "m11", "m12", "m20", "m21", "m22"];
function resetCookies() {
for (const name of NAMES) {
clearCookie(name, ROOT);
clearCookie(name, SUB);
}
clearCookie("data", ROOT);
clearCookie("data", SUB);
clearCookie("data1", ROOT);
clearCookie("data1", SUB);
clearCookie("data2", ROOT);
clearCookie("data2", SUB);
}
function setupCookies() {
// 浏览器已有 1 个同名值:只在 path=/ 设置。
setCookie("m10", "old", ROOT);
setCookie("m11", "old", ROOT);
setCookie("m12", "old", ROOT);
// 浏览器已有 2 个同名值:
// path=/ 与 path=/headers 各设置一个。
setCookie("m20", "old1", ROOT);
setCookie("m20", "old2", SUB);
setCookie("m21", "old1", ROOT);
setCookie("m21", "old2", SUB);
setCookie("m22", "old1", ROOT);
setCookie("m22", "old2", SUB);
}
resetCookies();
setupCookies();
let lastCookieMap = null;
// 复刻原 `if (matrixRequestPassed && lastCookieMap)` 门控:矩阵请求失败时,其依赖用例应跳过
// 而非各自级联失败(声明式框架无法条件注册用例,故用运行时 skip 表达同一依赖关系)。
let matrixOk = false;
describe("TM #2754: 同名 cookie 应覆盖而非追加", () => {
check(
"自动断言",
"document.cookie=data=1,GM_xhr cookie: data=2 时应只送出 data=2",
async () => {
setCookie("data", "1", ROOT);
try {
const response = await gmRequest({
method: "GET",
url: `${MOCKHTTP}/headers`,
cookie: "data=2",
timeout: 15000,
});
const cookieHeader = getCookieHeader(response);
const cookieMap = parseCookieMultiMap(cookieHeader);
assertCookieValues(cookieMap, "data", ["2"], "data 应只保留脚本指定的值");
} finally {
clearCookie("data", ROOT);
}
},
null,
null,
null
);
});
describe("TM #2829: 多个不同名 cookie 不应被截断", () => {
check(
"自动断言",
'GM_xhr cookie: "data1=1; data2=2" 应两者都送出,而非只剩第一个',
async () => {
const response = await gmRequest({
method: "GET",
url: `${MOCKHTTP}/headers`,
cookie: "data1=1; data2=2",
timeout: 15000,
});
const cookieHeader = getCookieHeader(response);
const cookieMap = parseCookieMultiMap(cookieHeader);
assertCookieValues(cookieMap, "data1", ["1"], "data1 应存在");
assertCookieValues(cookieMap, "data2", ["2"], "data2 不应被截断丢失");
},
null,
null,
null
);
});
describe("完整矩阵:浏览器已有(0/1/2) × 脚本指定(0/1/2)", () => {
check(
"自动断言",
"发送带完整矩阵 cookie 参数的请求",
async () => {
const customCookie = [
"m01=new",
"m02=new1",
"m02=new2",
"m11=new",
"m12=new1",
"m12=new2",
"m21=new",
"m22=new1",
"m22=new2",
].join("; ");
const response = await gmRequest({
method: "GET",
url: `${MOCKHTTP}/headers`,
cookie: customCookie,
timeout: 15000,
});
const cookieHeader = getCookieHeader(response);
lastCookieMap = parseCookieMultiMap(cookieHeader);
expect(cookieHeader.length > 0).toBeTruthy();
expect(lastCookieMap.size > 0).toBeTruthy();
matrixOk = true;
},
null,
null,
null
);
check(
"自动断言",
"m00:浏览器无、脚本未指定 → 不应出现",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m00", []);
},
null,
null,
null
);
check(
"自动断言",
"m01:浏览器无、脚本指定单值 → 应为脚本值",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m01", ["new"]);
},
null,
null,
null
);
check(
"自动断言",
"m02:浏览器无、脚本指定多值 → 应为脚本两个值",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m02", ["new1", "new2"]);
},
null,
null,
null
);
check(
"自动断言",
"m10:浏览器单值、脚本未指定 → 应保留浏览器原值",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m10", ["old"]);
},
null,
null,
null
);
check(
"自动断言",
"m11:浏览器单值、脚本指定单值 → 应覆盖为脚本值",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m11", ["new"]);
},
null,
null,
null
);
check(
"自动断言",
"m12:浏览器单值、脚本指定多值 → 应覆盖为脚本两个值",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m12", ["new1", "new2"]);
},
null,
null,
null
);
check(
"自动断言",
"m20:浏览器多值(同名不同path)、脚本未指定 → 应保留浏览器全部值",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m20", ["old1", "old2"]);
},
null,
null,
null
);
check(
"自动断言",
"m21:浏览器多值、脚本指定单值 → 应完全覆盖为脚本单一值",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m21", ["new"]);
},
null,
null,
null
);
check(
"自动断言",
"m22:浏览器多值、脚本指定多值 → 应完全覆盖为脚本两个值",
() => {
if (!matrixOk) SCTest.skip("矩阵请求未通过,跳过依赖断言");
assertCookieValues(lastCookieMap, "m22", ["new1", "new2"]);
},
null,
null,
null
);
});
try {
await run();
} finally {
resetCookies();
}
})();