11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst assert = require('assert'); 51cb0ef41Sopenharmony_ciconst http = require('http'); 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ciconst { PerformanceObserver } = require('perf_hooks'); 81cb0ef41Sopenharmony_ciconst entries = []; 91cb0ef41Sopenharmony_ciconst obs = new PerformanceObserver(common.mustCallAtLeast((items) => { 101cb0ef41Sopenharmony_ci entries.push(...items.getEntries()); 111cb0ef41Sopenharmony_ci})); 121cb0ef41Sopenharmony_ci 131cb0ef41Sopenharmony_ciobs.observe({ type: 'http' }); 141cb0ef41Sopenharmony_ci 151cb0ef41Sopenharmony_ciconst expected = 'Post Body For Test'; 161cb0ef41Sopenharmony_ciconst makeRequest = (options) => { 171cb0ef41Sopenharmony_ci return new Promise((resolve, reject) => { 181cb0ef41Sopenharmony_ci http.request(options, common.mustCall((res) => { 191cb0ef41Sopenharmony_ci resolve(); 201cb0ef41Sopenharmony_ci })).on('error', reject).end(options.data); 211cb0ef41Sopenharmony_ci }); 221cb0ef41Sopenharmony_ci}; 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciconst server = http.Server(common.mustCall((req, res) => { 251cb0ef41Sopenharmony_ci let result = ''; 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci req.setEncoding('utf8'); 281cb0ef41Sopenharmony_ci req.on('data', function(chunk) { 291cb0ef41Sopenharmony_ci result += chunk; 301cb0ef41Sopenharmony_ci }); 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci req.on('end', common.mustCall(function() { 331cb0ef41Sopenharmony_ci assert.strictEqual(result, expected); 341cb0ef41Sopenharmony_ci res.writeHead(200); 351cb0ef41Sopenharmony_ci res.end('hello world\n'); 361cb0ef41Sopenharmony_ci })); 371cb0ef41Sopenharmony_ci}, 2)); 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(async () => { 401cb0ef41Sopenharmony_ci await Promise.all([ 411cb0ef41Sopenharmony_ci makeRequest({ 421cb0ef41Sopenharmony_ci port: server.address().port, 431cb0ef41Sopenharmony_ci path: '/', 441cb0ef41Sopenharmony_ci method: 'POST', 451cb0ef41Sopenharmony_ci data: expected 461cb0ef41Sopenharmony_ci }), 471cb0ef41Sopenharmony_ci makeRequest({ 481cb0ef41Sopenharmony_ci port: server.address().port, 491cb0ef41Sopenharmony_ci path: '/', 501cb0ef41Sopenharmony_ci method: 'POST', 511cb0ef41Sopenharmony_ci data: expected 521cb0ef41Sopenharmony_ci }), 531cb0ef41Sopenharmony_ci ]); 541cb0ef41Sopenharmony_ci server.close(); 551cb0ef41Sopenharmony_ci})); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ciprocess.on('exit', () => { 581cb0ef41Sopenharmony_ci let numberOfHttpClients = 0; 591cb0ef41Sopenharmony_ci let numberOfHttpRequests = 0; 601cb0ef41Sopenharmony_ci for (const entry of entries) { 611cb0ef41Sopenharmony_ci assert.strictEqual(entry.entryType, 'http'); 621cb0ef41Sopenharmony_ci assert.strictEqual(typeof entry.startTime, 'number'); 631cb0ef41Sopenharmony_ci assert.strictEqual(typeof entry.duration, 'number'); 641cb0ef41Sopenharmony_ci if (entry.name === 'HttpClient') { 651cb0ef41Sopenharmony_ci numberOfHttpClients++; 661cb0ef41Sopenharmony_ci } else if (entry.name === 'HttpRequest') { 671cb0ef41Sopenharmony_ci numberOfHttpRequests++; 681cb0ef41Sopenharmony_ci } 691cb0ef41Sopenharmony_ci assert.strictEqual(typeof entry.detail.req.method, 'string'); 701cb0ef41Sopenharmony_ci assert.strictEqual(typeof entry.detail.req.url, 'string'); 711cb0ef41Sopenharmony_ci assert.strictEqual(typeof entry.detail.req.headers, 'object'); 721cb0ef41Sopenharmony_ci assert.strictEqual(typeof entry.detail.res.statusCode, 'number'); 731cb0ef41Sopenharmony_ci assert.strictEqual(typeof entry.detail.res.statusMessage, 'string'); 741cb0ef41Sopenharmony_ci assert.strictEqual(typeof entry.detail.res.headers, 'object'); 751cb0ef41Sopenharmony_ci } 761cb0ef41Sopenharmony_ci assert.strictEqual(numberOfHttpClients, 2); 771cb0ef41Sopenharmony_ci assert.strictEqual(numberOfHttpRequests, 2); 781cb0ef41Sopenharmony_ci}); 79