Compare commits

...

2 Commits

Author SHA1 Message Date
Jarred-Sumner
dcda307ecd Sync Node.js tests with upstream 2025-05-30 00:28:30 +00:00
Jarred Sumner
0f6161af81 test: add http same map test and intrinsics 2025-05-29 17:27:17 -07:00
2 changed files with 98 additions and 0 deletions

View File

@@ -159,6 +159,48 @@ if (process.argv.length === 2 &&
}
}
// Support for V8 native syntax used in some tests when the
// `--allow-natives-syntax` flag is provided. Bun does not
// natively parse these intrinsics so emulate a subset needed by
// tests through an eval wrapper.
if (process.execArgv.some((f) => f === '--allow_natives_syntax' || f === '--allow-natives-syntax')) {
const originalEval = global.eval;
function haveSameMap(a, b) {
if (a === b) return true;
if (Object.getPrototypeOf(a) !== Object.getPrototypeOf(b)) return false;
const keysA = Reflect.ownKeys(a);
const keysB = Reflect.ownKeys(b);
if (keysA.length !== keysB.length) return false;
for (let i = 0; i < keysA.length; i++) {
if (keysA[i] !== keysB[i]) return false;
}
return true;
}
global.eval = function nativeEval(code) {
if (typeof code === 'string' && code.trim().startsWith('%')) {
const match = /^%([A-Za-z0-9_]+)\((.*)\)$/.exec(code.trim());
if (match) {
const args = originalEval(`[${match[2]}]`);
switch (match[1]) {
case 'HaveSameMap':
return haveSameMap(args[0], args[1]);
case 'CollectGarbage':
if (typeof global.gc === 'function') {
global.gc();
} else if (typeof Bun?.gc === 'function') {
Bun.gc(true);
}
return;
case 'DebugPrint':
console.debug(args[0]);
return;
}
}
}
return originalEval(code);
};
}
const isWindows = process.platform === 'win32';
const isSunOS = process.platform === 'sunos';
const isFreeBSD = process.platform === 'freebsd';

View File

@@ -0,0 +1,56 @@
// Flags: --allow_natives_syntax
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const server =
http.createServer(onrequest).listen(0, common.localhostIPv4, () => next(0));
function onrequest(req, res) {
res.end('ok');
onrequest.requests.push(req);
onrequest.responses.push(res);
}
onrequest.requests = [];
onrequest.responses = [];
function next(n) {
const { address: host, port } = server.address();
const req = http.get({ host, port });
req.once('response', (res) => onresponse(n, req, res));
}
function onresponse(n, req, res) {
res.resume();
if (n < 3) {
res.once('end', () => next(n + 1));
} else {
server.close();
}
onresponse.requests.push(req);
onresponse.responses.push(res);
}
onresponse.requests = [];
onresponse.responses = [];
function allSame(list) {
assert(list.length >= 2);
// eslint-disable-next-line no-unused-vars
for (const elt of list) eval('%DebugPrint(elt)');
// eslint-disable-next-line no-unused-vars
for (const elt of list) assert(eval('%HaveSameMap(list[0], elt)'));
}
process.on('exit', () => {
eval('%CollectGarbage(0)');
// TODO(bnoordhuis) Investigate why the first IncomingMessage ends up
// with a deprecated map. The map is stable after the first request.
allSame(onrequest.requests.slice(1));
allSame(onrequest.responses);
allSame(onresponse.requests);
allSame(onresponse.responses);
});