|
1 | 1 | /**
|
2 |
| - * |
| 2 | + * |
3 | 3 | * @param {[
|
4 |
| - [string, string] |
5 |
| - ]} configHeaders |
| 4 | + * [string, string | string[]] |
| 5 | + * ] | Record<string, string | string[]>} configHeaders |
6 | 6 | * @param {Headers | {
|
7 |
| - [string]: string |
8 |
| - }} wasmModuleHeaders |
| 7 | + * [string]: string |
| 8 | + * }} wasmModuleHeaders |
9 | 9 | * @returns
|
10 | 10 | */
|
11 |
| -const compareHeaders = (configHeaders, wasmModuleHeaders) => { |
12 | 11 |
|
| 12 | +const compareHeaders = (configHeaders, wasmModuleHeaders) => { |
13 | 13 | if (!configHeaders) {
|
14 | 14 | return;
|
15 | 15 | }
|
16 | 16 |
|
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); |
26 | 29 | }
|
| 30 | + } else { |
| 31 | + combinedHeaders[key] = [ |
| 32 | + combinedHeaders[key], |
| 33 | + ...(Array.isArray(val) ? val : [val]), |
| 34 | + ]; |
27 | 35 | }
|
28 |
| - } else { |
29 |
| - throw new Error(`[Header mismatch] Expected multiple headers with named "${configHeaderKey}" but got only one`); |
30 | 36 | }
|
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 | + ); |
33 | 66 | }
|
34 | 67 | }
|
35 | 68 | };
|
|
0 commit comments