Skip to content

Commit a40389a

Browse files
author
Guy Bedford
authored
tests: fix header comparison and multiple-set-cookie tests (#846)
1 parent 17d213a commit a40389a

File tree

3 files changed

+64
-42
lines changed

3 files changed

+64
-42
lines changed

integration-tests/js-compute/compare-headers.js

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,68 @@
11
/**
2-
*
2+
*
33
* @param {[
4-
[string, string]
5-
]} configHeaders
4+
* [string, string | string[]]
5+
* ] | Record<string, string | string[]>} configHeaders
66
* @param {Headers | {
7-
[string]: string
8-
}} wasmModuleHeaders
7+
* [string]: string
8+
* }} wasmModuleHeaders
99
* @returns
1010
*/
11-
const compareHeaders = (configHeaders, wasmModuleHeaders) => {
1211

12+
const compareHeaders = (configHeaders, wasmModuleHeaders) => {
1313
if (!configHeaders) {
1414
return;
1515
}
1616

17-
for (const [configHeaderKey, configHeaderValue] of Object.entries(configHeaders)) {
18-
let wasmModuleHeaderValue = wasmModuleHeaders[configHeaderKey.toLowerCase()]
19-
if (wasmModuleHeaderValue === null) {
20-
throw new Error(`[Header Key mismatch] Expected: ${configHeaderKey} - Got: ${null}`);
21-
} else if (Array.isArray(configHeaderValue)) {
22-
if (Array.isArray(configHeaderValue)) {
23-
for (let value of configHeaderValue) {
24-
if (!configHeaderValue.includes(value)) {
25-
throw new Error(`[Header mismatch] Missing header named "${configHeaderKey}" with value "${value}"`);
17+
// convert an array of entries into an object of arrays for easier asserting
18+
if (Array.isArray(configHeaders)) {
19+
const combinedHeaders = Object.create(null);
20+
for (const [key, val] of configHeaders) {
21+
if (!Object.hasOwnProperty.call(combinedHeaders, key)) {
22+
combinedHeaders[key] = val;
23+
} else {
24+
if (Array.isArray(combinedHeaders[key])) {
25+
if (Array.isArray(val)) {
26+
combinedHeaders[key] = combinedHeaders[key].concat(val);
27+
} else {
28+
combinedHeaders[key].push(val);
2629
}
30+
} else {
31+
combinedHeaders[key] = [
32+
combinedHeaders[key],
33+
...(Array.isArray(val) ? val : [val]),
34+
];
2735
}
28-
} else {
29-
throw new Error(`[Header mismatch] Expected multiple headers with named "${configHeaderKey}" but got only one`);
3036
}
31-
} else if (wasmModuleHeaderValue !== configHeaderValue) {
32-
throw new Error(`[Header Value mismatch] Expected: ${configHeaderValue} - Got: ${wasmModuleHeaderValue}`);
37+
}
38+
configHeaders = combinedHeaders;
39+
}
40+
41+
for (let [configHeaderKey, configHeaderValue] of Object.entries(
42+
configHeaders
43+
)) {
44+
let wasmModuleHeaderValue =
45+
wasmModuleHeaders[configHeaderKey.toLowerCase()];
46+
if (Array.isArray(configHeaderValue) && configHeaderValue.length === 1) {
47+
configHeaderValue = configHeaderValue[0];
48+
}
49+
if (Array.isArray(configHeaderValue)) {
50+
if (!Array.isArray(wasmModuleHeaderValue)) {
51+
throw new Error(`[Header Value mismatch] Expected multiple headers for '${configHeaderKey}', but ony got one.`);
52+
}
53+
if (configHeaderValue.length !== wasmModuleHeaderValue.length) {
54+
throw new Error(`[Header Value mismatch] Expected ${configHeaderValue.length} headers for '${configHeaderKey}', but got ${wasmModuleHeaderValue.length}.`);
55+
}
56+
for (const [idx, configValue] of configHeaderValue.entries()) {
57+
if (wasmModuleHeaderValue[idx] !== configValue) {
58+
throw new Error(`[Header Value mismatch] Expected '${configValue}' for header item ${idx} of '${configHeaderKey}', but got '${wasmModuleHeaderValue[idx]}'.`);
59+
}
60+
}
61+
}
62+
else if (wasmModuleHeaderValue !== configHeaderValue) {
63+
throw new Error(
64+
`[Header Value mismatch] Expected: '${configHeaderKey}: ${configHeaderValue}' (${configHeaderValue.length}), got '${configHeaderKey}: ${wasmModuleHeaderValue}' (${wasmModuleHeaderValue.length})`
65+
);
3366
}
3467
}
3568
};

integration-tests/js-compute/fixtures/app/tests.json

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4367,28 +4367,17 @@
43674367
"downstream_response": {
43684368
"status": 302,
43694369
"headers": [
4370-
[
4371-
"Set-Cookie",
4372-
"test=1; expires=Tue, 06-Dec-2022 12:34:56 GMT; Max-Age=60; Path=/; HttpOnly; Secure"
4373-
],
4374-
[
4375-
"Set-Cookie",
4376-
"test_id=1; Max-Age=60; Path=/; expires=Tue, 06-Dec-2022 12:34:56 GMT"
4377-
],
4378-
[
4379-
"Set-Cookie",
4380-
"test_id=1; Max-Age=60; Path=/; expires=Tue, 06-Dec-2022 12:34:56 GMT"
4381-
],
4382-
["Set-Cookie", "test2=2"],
4383-
["Set-Cookie", "test3=3"],
4384-
["Set-Cookie", "test4=4"],
4385-
["Set-Cookie", "test5=5"],
4386-
["Set-Cookie", "test6=6"],
4387-
["Set-Cookie", "test7=7"],
4388-
["Set-Cookie", "test8=8"],
4389-
["Set-Cookie", "test9=9"],
4390-
["Set-Cookie", "test10=10"],
4391-
["Set-Cookie", "test11=11"]
4370+
["Set-Cookie", "1=1; Path=/"],
4371+
["Set-Cookie", "2=2; Path=/"],
4372+
["Set-Cookie", "3=3; Path=/"],
4373+
["Set-Cookie", "4=4; Path=/"],
4374+
["Set-Cookie", "5=5; Path=/"],
4375+
["Set-Cookie", "6=6; Path=/"],
4376+
["Set-Cookie", "7=7; Path=/"],
4377+
["Set-Cookie", "8=8; Path=/"],
4378+
["Set-Cookie", "9=9; Path=/"],
4379+
["Set-Cookie", "10=10; Path=/"],
4380+
["Set-Cookie", "11=11; Path=/"]
43924381
]
43934382
}
43944383
},

integration-tests/js-compute/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ for (const chunk of chunks(Object.entries(tests), 100)) {
149149
skipped: false
150150
};
151151
} catch (error) {
152-
throw new Error(`${title} ${error.message}`);
152+
throw new Error(`${title} ${error.message}`, { cause: error });
153153
}
154154
} else {
155155
return {

0 commit comments

Comments
 (0)