|
3 | 3 | const { createServer } = require('node:http') |
4 | 4 | const { describe, test, after } = require('node:test') |
5 | 5 | const { once } = require('node:events') |
6 | | -const { strictEqual } = require('node:assert') |
| 6 | +const { strictEqual, notStrictEqual } = require('node:assert') |
7 | 7 | const { setTimeout: sleep } = require('node:timers/promises') |
8 | 8 | const diagnosticsChannel = require('node:diagnostics_channel') |
9 | 9 | const { Client, interceptors } = require('../../index') |
| 10 | +const { makeDeduplicationKey } = require('../../lib/util/cache') |
10 | 11 |
|
11 | 12 | describe('Deduplicate Interceptor', () => { |
12 | 13 | test('deduplicates concurrent requests for the same resource', async () => { |
@@ -1291,4 +1292,62 @@ describe('Deduplicate Interceptor', () => { |
1291 | 1292 | message: 'expected opts.excludeHeaderNames to be an array, got string' |
1292 | 1293 | }) |
1293 | 1294 | }) |
| 1295 | + |
| 1296 | + test('makeDeduplicationKey does not collide when header values contain delimiters', () => { |
| 1297 | + // Regression test for https://github.com/nodejs/undici/issues/5012 |
| 1298 | + // Previously, headers {a:"x:b=y"} and {a:"x", b:"y"} produced the same key |
| 1299 | + const key1 = makeDeduplicationKey({ |
| 1300 | + origin: 'https://example.com', |
| 1301 | + method: 'GET', |
| 1302 | + path: '/', |
| 1303 | + headers: { a: 'x:b=y' } |
| 1304 | + }) |
| 1305 | + |
| 1306 | + const key2 = makeDeduplicationKey({ |
| 1307 | + origin: 'https://example.com', |
| 1308 | + method: 'GET', |
| 1309 | + path: '/', |
| 1310 | + headers: { a: 'x', b: 'y' } |
| 1311 | + }) |
| 1312 | + |
| 1313 | + notStrictEqual(key1, key2) |
| 1314 | + }) |
| 1315 | + |
| 1316 | + test('makeDeduplicationKey produces same key for identical headers', () => { |
| 1317 | + const key1 = makeDeduplicationKey({ |
| 1318 | + origin: 'https://example.com', |
| 1319 | + method: 'GET', |
| 1320 | + path: '/', |
| 1321 | + headers: { b: '2', a: '1' } |
| 1322 | + }) |
| 1323 | + |
| 1324 | + const key2 = makeDeduplicationKey({ |
| 1325 | + origin: 'https://example.com', |
| 1326 | + method: 'GET', |
| 1327 | + path: '/', |
| 1328 | + headers: { a: '1', b: '2' } |
| 1329 | + }) |
| 1330 | + |
| 1331 | + strictEqual(key1, key2) |
| 1332 | + }) |
| 1333 | + |
| 1334 | + test('makeDeduplicationKey respects excludeHeaders', () => { |
| 1335 | + const excludeHeaders = new Set(['x-request-id']) |
| 1336 | + |
| 1337 | + const key1 = makeDeduplicationKey({ |
| 1338 | + origin: 'https://example.com', |
| 1339 | + method: 'GET', |
| 1340 | + path: '/', |
| 1341 | + headers: { accept: 'text/html', 'x-request-id': '111' } |
| 1342 | + }, excludeHeaders) |
| 1343 | + |
| 1344 | + const key2 = makeDeduplicationKey({ |
| 1345 | + origin: 'https://example.com', |
| 1346 | + method: 'GET', |
| 1347 | + path: '/', |
| 1348 | + headers: { accept: 'text/html', 'x-request-id': '222' } |
| 1349 | + }, excludeHeaders) |
| 1350 | + |
| 1351 | + strictEqual(key1, key2) |
| 1352 | + }) |
1294 | 1353 | }) |
0 commit comments