11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst net = require('net'); 41cb0ef41Sopenharmony_ciconst http = require('http'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciclass Agent extends http.Agent { 71cb0ef41Sopenharmony_ci createConnection() { 81cb0ef41Sopenharmony_ci const socket = new net.Socket(); 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ci socket.on('error', function() { 111cb0ef41Sopenharmony_ci socket.push('HTTP/1.1 200\r\n\r\n'); 121cb0ef41Sopenharmony_ci }); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci let onNewListener; 151cb0ef41Sopenharmony_ci socket.on('newListener', onNewListener = (name) => { 161cb0ef41Sopenharmony_ci if (name !== 'error') 171cb0ef41Sopenharmony_ci return; 181cb0ef41Sopenharmony_ci socket.removeListener('newListener', onNewListener); 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci // Let other listeners to be set up too 211cb0ef41Sopenharmony_ci process.nextTick(() => { 221cb0ef41Sopenharmony_ci this.breakSocket(socket); 231cb0ef41Sopenharmony_ci }); 241cb0ef41Sopenharmony_ci }); 251cb0ef41Sopenharmony_ci 261cb0ef41Sopenharmony_ci return socket; 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci breakSocket(socket) { 301cb0ef41Sopenharmony_ci socket.emit('error', new Error('Intentional error')); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci} 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciconst agent = new Agent(); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_cihttp.request({ 371cb0ef41Sopenharmony_ci agent 381cb0ef41Sopenharmony_ci}).once('error', function() { 391cb0ef41Sopenharmony_ci console.log('ignore'); 401cb0ef41Sopenharmony_ci this.on('data', common.mustNotCall()); 411cb0ef41Sopenharmony_ci}); 42