11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common'); 41cb0ef41Sopenharmony_ciconst { AsyncLocalStorage } = require('async_hooks'); 51cb0ef41Sopenharmony_ciconst dc = require('diagnostics_channel'); 61cb0ef41Sopenharmony_ciconst assert = require('assert'); 71cb0ef41Sopenharmony_ciconst http = require('http'); 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciconst als = new AsyncLocalStorage(); 101cb0ef41Sopenharmony_cilet context; 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci// Bind requests to an AsyncLocalStorage context 131cb0ef41Sopenharmony_cidc.subscribe('http.server.request.start', common.mustCall((message) => { 141cb0ef41Sopenharmony_ci als.enterWith(message); 151cb0ef41Sopenharmony_ci context = message; 161cb0ef41Sopenharmony_ci})); 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci// When the request ends, verify the context has been maintained 191cb0ef41Sopenharmony_ci// and that the messages contain the expected data 201cb0ef41Sopenharmony_cidc.subscribe('http.server.response.finish', common.mustCall((message) => { 211cb0ef41Sopenharmony_ci const data = { 221cb0ef41Sopenharmony_ci request, 231cb0ef41Sopenharmony_ci response, 241cb0ef41Sopenharmony_ci server, 251cb0ef41Sopenharmony_ci socket: request.socket 261cb0ef41Sopenharmony_ci }; 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ci // Context is maintained 291cb0ef41Sopenharmony_ci compare(als.getStore(), context); 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ci compare(context, data); 321cb0ef41Sopenharmony_ci compare(message, data); 331cb0ef41Sopenharmony_ci})); 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_cilet request; 361cb0ef41Sopenharmony_cilet response; 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ciconst server = http.createServer(common.mustCall((req, res) => { 391cb0ef41Sopenharmony_ci request = req; 401cb0ef41Sopenharmony_ci response = res; 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ci setTimeout(() => { 431cb0ef41Sopenharmony_ci res.end('done'); 441cb0ef41Sopenharmony_ci }, 1); 451cb0ef41Sopenharmony_ci})); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ciserver.listen(() => { 481cb0ef41Sopenharmony_ci const { port } = server.address(); 491cb0ef41Sopenharmony_ci http.get(`http://localhost:${port}`, (res) => { 501cb0ef41Sopenharmony_ci res.resume(); 511cb0ef41Sopenharmony_ci res.on('end', () => { 521cb0ef41Sopenharmony_ci server.close(); 531cb0ef41Sopenharmony_ci }); 541cb0ef41Sopenharmony_ci }); 551cb0ef41Sopenharmony_ci}); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_cifunction compare(a, b) { 581cb0ef41Sopenharmony_ci assert.strictEqual(a.request, b.request); 591cb0ef41Sopenharmony_ci assert.strictEqual(a.response, b.response); 601cb0ef41Sopenharmony_ci assert.strictEqual(a.socket, b.socket); 611cb0ef41Sopenharmony_ci assert.strictEqual(a.server, b.server); 621cb0ef41Sopenharmony_ci} 63