1'use strict'; 2 3const common = require('../common'); 4const assert = require('assert'); 5const tick = require('../common/tick'); 6const initHooks = require('./init-hooks'); 7const { checkInvocations } = require('./hook-checks'); 8 9const hooks = initHooks(); 10 11hooks.enable(); 12 13const { HTTPParser } = require('_http_common'); 14 15const RESPONSE = HTTPParser.RESPONSE; 16const kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0; 17const kOnBody = HTTPParser.kOnBody | 0; 18 19const request = Buffer.from( 20 'HTTP/1.1 200 OK\r\n' + 21 'Content-Type: text/plain\r\n' + 22 'Content-Length: 4\r\n' + 23 '\r\n' + 24 'pong', 25); 26 27const parser = new HTTPParser(); 28parser.initialize(RESPONSE, {}); 29const as = hooks.activitiesOfTypes('HTTPCLIENTREQUEST'); 30const httpparser = as[0]; 31 32assert.strictEqual(as.length, 1); 33assert.strictEqual(typeof httpparser.uid, 'number'); 34assert.strictEqual(typeof httpparser.triggerAsyncId, 'number'); 35checkInvocations(httpparser, { init: 1 }, 'when created new Httphttpparser'); 36 37parser[kOnHeadersComplete] = common.mustCall(onheadersComplete); 38parser[kOnBody] = common.mustCall(onbody); 39parser.execute(request, 0, request.length); 40 41function onheadersComplete() { 42 checkInvocations(httpparser, { init: 1, before: 1 }, 43 'when onheadersComplete called'); 44} 45 46function onbody() { 47 checkInvocations(httpparser, { init: 1, before: 2, after: 1 }, 48 'when onbody called'); 49 tick(1, common.mustCall(tick1)); 50} 51 52function tick1() { 53 parser.close(); 54 tick(1); 55} 56 57process.on('exit', onexit); 58 59function onexit() { 60 hooks.disable(); 61 hooks.sanityCheck('HTTPCLIENTREQUEST'); 62 checkInvocations(httpparser, { init: 1, before: 2, after: 2, destroy: 1 }, 63 'when process exits'); 64} 65