11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst tick = require('../common/tick'); 61cb0ef41Sopenharmony_ciconst initHooks = require('./init-hooks'); 71cb0ef41Sopenharmony_ciconst { checkInvocations } = require('./hook-checks'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst hooks = initHooks(); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cihooks.enable(); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciconst { HTTPParser } = require('_http_common'); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciconst RESPONSE = HTTPParser.RESPONSE; 161cb0ef41Sopenharmony_ciconst kOnHeadersComplete = HTTPParser.kOnHeadersComplete | 0; 171cb0ef41Sopenharmony_ciconst kOnBody = HTTPParser.kOnBody | 0; 181cb0ef41Sopenharmony_ci 191cb0ef41Sopenharmony_ciconst request = Buffer.from( 201cb0ef41Sopenharmony_ci 'HTTP/1.1 200 OK\r\n' + 211cb0ef41Sopenharmony_ci 'Content-Type: text/plain\r\n' + 221cb0ef41Sopenharmony_ci 'Content-Length: 4\r\n' + 231cb0ef41Sopenharmony_ci '\r\n' + 241cb0ef41Sopenharmony_ci 'pong', 251cb0ef41Sopenharmony_ci); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ciconst parser = new HTTPParser(); 281cb0ef41Sopenharmony_ciparser.initialize(RESPONSE, {}); 291cb0ef41Sopenharmony_ciconst as = hooks.activitiesOfTypes('HTTPCLIENTREQUEST'); 301cb0ef41Sopenharmony_ciconst httpparser = as[0]; 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ciassert.strictEqual(as.length, 1); 331cb0ef41Sopenharmony_ciassert.strictEqual(typeof httpparser.uid, 'number'); 341cb0ef41Sopenharmony_ciassert.strictEqual(typeof httpparser.triggerAsyncId, 'number'); 351cb0ef41Sopenharmony_cicheckInvocations(httpparser, { init: 1 }, 'when created new Httphttpparser'); 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ciparser[kOnHeadersComplete] = common.mustCall(onheadersComplete); 381cb0ef41Sopenharmony_ciparser[kOnBody] = common.mustCall(onbody); 391cb0ef41Sopenharmony_ciparser.execute(request, 0, request.length); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cifunction onheadersComplete() { 421cb0ef41Sopenharmony_ci checkInvocations(httpparser, { init: 1, before: 1 }, 431cb0ef41Sopenharmony_ci 'when onheadersComplete called'); 441cb0ef41Sopenharmony_ci} 451cb0ef41Sopenharmony_ci 461cb0ef41Sopenharmony_cifunction onbody() { 471cb0ef41Sopenharmony_ci checkInvocations(httpparser, { init: 1, before: 2, after: 1 }, 481cb0ef41Sopenharmony_ci 'when onbody called'); 491cb0ef41Sopenharmony_ci tick(1, common.mustCall(tick1)); 501cb0ef41Sopenharmony_ci} 511cb0ef41Sopenharmony_ci 521cb0ef41Sopenharmony_cifunction tick1() { 531cb0ef41Sopenharmony_ci parser.close(); 541cb0ef41Sopenharmony_ci tick(1); 551cb0ef41Sopenharmony_ci} 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ciprocess.on('exit', onexit); 581cb0ef41Sopenharmony_ci 591cb0ef41Sopenharmony_cifunction onexit() { 601cb0ef41Sopenharmony_ci hooks.disable(); 611cb0ef41Sopenharmony_ci hooks.sanityCheck('HTTPCLIENTREQUEST'); 621cb0ef41Sopenharmony_ci checkInvocations(httpparser, { init: 1, before: 2, after: 2, destroy: 1 }, 631cb0ef41Sopenharmony_ci 'when process exits'); 641cb0ef41Sopenharmony_ci} 65