11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst http = require('http'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ciconst server = http.createServer(common.mustNotCall()); 71cb0ef41Sopenharmony_ciserver.on('connect', common.mustCall(function(req, socket, firstBodyChunk) { 81cb0ef41Sopenharmony_ci assert.strictEqual(req.method, 'CONNECT'); 91cb0ef41Sopenharmony_ci assert.strictEqual(req.url, 'example.com:443'); 101cb0ef41Sopenharmony_ci console.error('Server got CONNECT request'); 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ci // It is legal for the server to send some data intended for the client 131cb0ef41Sopenharmony_ci // along with the CONNECT response 141cb0ef41Sopenharmony_ci socket.write( 151cb0ef41Sopenharmony_ci 'HTTP/1.1 200 Connection established\r\n' + 161cb0ef41Sopenharmony_ci 'Date: Tue, 15 Nov 1994 08:12:31 GMT\r\n' + 171cb0ef41Sopenharmony_ci '\r\n' + 181cb0ef41Sopenharmony_ci 'Head' 191cb0ef41Sopenharmony_ci ); 201cb0ef41Sopenharmony_ci 211cb0ef41Sopenharmony_ci let data = firstBodyChunk.toString(); 221cb0ef41Sopenharmony_ci socket.on('data', function(buf) { 231cb0ef41Sopenharmony_ci data += buf.toString(); 241cb0ef41Sopenharmony_ci }); 251cb0ef41Sopenharmony_ci socket.on('end', function() { 261cb0ef41Sopenharmony_ci socket.end(data); 271cb0ef41Sopenharmony_ci }); 281cb0ef41Sopenharmony_ci})); 291cb0ef41Sopenharmony_ciserver.listen(0, common.mustCall(function() { 301cb0ef41Sopenharmony_ci const req = http.request({ 311cb0ef41Sopenharmony_ci port: this.address().port, 321cb0ef41Sopenharmony_ci method: 'CONNECT', 331cb0ef41Sopenharmony_ci path: 'example.com:443' 341cb0ef41Sopenharmony_ci }, common.mustNotCall()); 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci assert.strictEqual(req.destroyed, false); 371cb0ef41Sopenharmony_ci req.on('close', common.mustCall(() => { 381cb0ef41Sopenharmony_ci assert.strictEqual(req.destroyed, true); 391cb0ef41Sopenharmony_ci })); 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ci req.on('connect', common.mustCall(function(res, socket, firstBodyChunk) { 421cb0ef41Sopenharmony_ci console.error('Client got CONNECT request'); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ci // Make sure this request got removed from the pool. 451cb0ef41Sopenharmony_ci const name = `localhost:${server.address().port}`; 461cb0ef41Sopenharmony_ci assert(!(name in http.globalAgent.sockets)); 471cb0ef41Sopenharmony_ci assert(!(name in http.globalAgent.requests)); 481cb0ef41Sopenharmony_ci 491cb0ef41Sopenharmony_ci // Make sure this socket has detached. 501cb0ef41Sopenharmony_ci assert(!socket.ondata); 511cb0ef41Sopenharmony_ci assert(!socket.onend); 521cb0ef41Sopenharmony_ci assert.strictEqual(socket.listeners('connect').length, 0); 531cb0ef41Sopenharmony_ci assert.strictEqual(socket.listeners('data').length, 0); 541cb0ef41Sopenharmony_ci 551cb0ef41Sopenharmony_ci let data = firstBodyChunk.toString(); 561cb0ef41Sopenharmony_ci 571cb0ef41Sopenharmony_ci // Test that the firstBodyChunk was not parsed as HTTP 581cb0ef41Sopenharmony_ci assert.strictEqual(data, 'Head'); 591cb0ef41Sopenharmony_ci 601cb0ef41Sopenharmony_ci socket.on('data', function(buf) { 611cb0ef41Sopenharmony_ci data += buf.toString(); 621cb0ef41Sopenharmony_ci }); 631cb0ef41Sopenharmony_ci socket.on('end', function() { 641cb0ef41Sopenharmony_ci assert.strictEqual(data, 'HeadRequestEnd'); 651cb0ef41Sopenharmony_ci server.close(); 661cb0ef41Sopenharmony_ci }); 671cb0ef41Sopenharmony_ci socket.end('End'); 681cb0ef41Sopenharmony_ci })); 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci req.end('Request'); 711cb0ef41Sopenharmony_ci})); 72