1// Copyright Joyent, Inc. and other Node contributors. 2// 3// Permission is hereby granted, free of charge, to any person obtaining a 4// copy of this software and associated documentation files (the 5// "Software"), to deal in the Software without restriction, including 6// without limitation the rights to use, copy, modify, merge, publish, 7// distribute, sublicense, and/or sell copies of the Software, and to permit 8// persons to whom the Software is furnished to do so, subject to the 9// following conditions: 10// 11// The above copyright notice and this permission notice shall be included 12// in all copies or substantial portions of the Software. 13// 14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 20// USE OR OTHER DEALINGS IN THE SOFTWARE. 21 22'use strict'; 23const common = require('../common'); 24const assert = require('assert'); 25const http = require('http'); 26const Countdown = require('../common/countdown'); 27 28const server = http.createServer(common.mustCall((req, res) => { 29 req.resume(); 30 res.writeHead(200); 31 res.write(''); 32 setTimeout(() => res.end(req.url), 50); 33}, 2)); 34 35const countdown = new Countdown(2, () => server.close()); 36 37server.on('connect', common.mustCall((req, socket) => { 38 socket.write('HTTP/1.1 200 Connection established\r\n\r\n'); 39 socket.resume(); 40 socket.on('end', () => socket.end()); 41})); 42 43server.listen(0, common.mustCall(() => { 44 const req = http.request({ 45 port: server.address().port, 46 method: 'CONNECT', 47 path: 'google.com:80' 48 }); 49 req.on('connect', common.mustCall((res, socket) => { 50 socket.end(); 51 socket.on('end', common.mustCall(() => { 52 doRequest(0); 53 doRequest(1); 54 })); 55 socket.resume(); 56 })); 57 req.end(); 58})); 59 60function doRequest(i) { 61 http.get({ 62 port: server.address().port, 63 path: `/request${i}` 64 }, common.mustCall((res) => { 65 let data = ''; 66 res.setEncoding('utf8'); 67 res.on('data', (chunk) => data += chunk); 68 res.on('end', common.mustCall(() => { 69 assert.strictEqual(data, `/request${i}`); 70 countdown.dec(); 71 })); 72 })); 73} 74