0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/async-hooks/test-httpparser.response.js
Artur Hayrapetyan 2a1492a00d test: move tick.js from test/async-hooks to test/common
PR-URL: https://github.com/nodejs/node/pull/23551
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
2018-10-15 07:41:12 +02:00

71 lines
2.0 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const tick = require('../common/tick');
const initHooks = require('./init-hooks');
const { checkInvocations } = require('./hook-checks');
const hooks = initHooks();
hooks.enable();
// The hooks.enable() must come before require('internal/test/binding')
// because internal/test/binding schedules a process warning on nextTick.
// If this order is not preserved, the hooks check will fail because it
// will not be notified about the nextTick creation but will see the
// callback event.
const { internalBinding } = require('internal/test/binding');
const { HTTPParser } = internalBinding('http_parser');
const RESPONSE = HTTPParser.RESPONSE;
const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0;
const kOnBody = HTTPParser.kOnBody | 0;
const request = Buffer.from(
'HTTP/1.1 200 OK\r\n' +
'Content-Type: text/plain\r\n' +
'Content-Length: 4\r\n' +
'\r\n' +
'pong'
);
const parser = new HTTPParser(RESPONSE);
const as = hooks.activitiesOfTypes('HTTPPARSER');
const httpparser = as[0];
assert.strictEqual(as.length, 1);
assert.strictEqual(typeof httpparser.uid, 'number');
assert.strictEqual(typeof httpparser.triggerAsyncId, 'number');
checkInvocations(httpparser, { init: 1 }, 'when created new Httphttpparser');
parser[kOnHeadersComplete] = common.mustCall(onheadersComplete);
parser[kOnBody] = common.mustCall(onbody);
parser.execute(request, 0, request.length);
function onheadersComplete() {
checkInvocations(httpparser, { init: 1, before: 1 },
'when onheadersComplete called');
}
function onbody() {
checkInvocations(httpparser, { init: 1, before: 2, after: 1 },
'when onbody called');
tick(1, common.mustCall(tick1));
}
function tick1() {
parser.close();
tick(1);
}
process.on('exit', onexit);
function onexit() {
hooks.disable();
hooks.sanityCheck('HTTPPARSER');
checkInvocations(httpparser, { init: 1, before: 2, after: 2, destroy: 1 },
'when process exits');
}