| 
 | 1 | +'use strict';  | 
 | 2 | +const common = require('../common');  | 
 | 3 | +const assert = require('assert');  | 
 | 4 | +const http = require('http');  | 
 | 5 | + | 
 | 6 | +const server = http.createServer(function(req, res) {  | 
 | 7 | +  assert(false);  | 
 | 8 | +});  | 
 | 9 | +server.on('connect', common.mustCall(function(req, socket, firstBodyChunk) {  | 
 | 10 | +  assert.equal(req.method, 'CONNECT');  | 
 | 11 | +  assert.equal(req.url, 'example.com:443');  | 
 | 12 | +  console.error('Server got CONNECT request');  | 
 | 13 | + | 
 | 14 | +  // It is legal for the server to send some data intended for the client  | 
 | 15 | +  // along with the CONNECT response  | 
 | 16 | +  socket.write(  | 
 | 17 | +    'HTTP/1.1 200 Connection established\r\n' +  | 
 | 18 | +    'Date: Tue, 15 Nov 1994 08:12:31 GMT\r\n' +  | 
 | 19 | +    '\r\n' +  | 
 | 20 | +    'Head'  | 
 | 21 | +  );  | 
 | 22 | + | 
 | 23 | +  var data = firstBodyChunk.toString();  | 
 | 24 | +  socket.on('data', function(buf) {  | 
 | 25 | +    data += buf.toString();  | 
 | 26 | +  });  | 
 | 27 | +  socket.on('end', function() {  | 
 | 28 | +    socket.end(data);  | 
 | 29 | +  });  | 
 | 30 | +}));  | 
 | 31 | +server.listen(common.PORT, common.mustCall(function() {  | 
 | 32 | +  const req = http.request({  | 
 | 33 | +    port: common.PORT,  | 
 | 34 | +    method: 'CONNECT',  | 
 | 35 | +    path: 'example.com:443'  | 
 | 36 | +  }, function(res) {  | 
 | 37 | +    assert(false);  | 
 | 38 | +  });  | 
 | 39 | + | 
 | 40 | +  req.on('close', common.mustCall(function() { }));  | 
 | 41 | + | 
 | 42 | +  req.on('connect', common.mustCall(function(res, socket, firstBodyChunk) {  | 
 | 43 | +    console.error('Client got CONNECT request');  | 
 | 44 | + | 
 | 45 | +    // Make sure this request got removed from the pool.  | 
 | 46 | +    const name = 'localhost:' + common.PORT;  | 
 | 47 | +    assert(!http.globalAgent.sockets.hasOwnProperty(name));  | 
 | 48 | +    assert(!http.globalAgent.requests.hasOwnProperty(name));  | 
 | 49 | + | 
 | 50 | +    // Make sure this socket has detached.  | 
 | 51 | +    assert(!socket.ondata);  | 
 | 52 | +    assert(!socket.onend);  | 
 | 53 | +    assert.equal(socket.listeners('connect').length, 0);  | 
 | 54 | +    assert.equal(socket.listeners('data').length, 0);  | 
 | 55 | + | 
 | 56 | +    var data = firstBodyChunk.toString();  | 
 | 57 | + | 
 | 58 | +    // test that the firstBodyChunk was not parsed as HTTP  | 
 | 59 | +    assert.equal(data, 'Head');  | 
 | 60 | + | 
 | 61 | +    socket.on('data', function(buf) {  | 
 | 62 | +      data += buf.toString();  | 
 | 63 | +    });  | 
 | 64 | +    socket.on('end', function() {  | 
 | 65 | +      assert.equal(data, 'HeadRequestEnd');  | 
 | 66 | +      server.close();  | 
 | 67 | +    });  | 
 | 68 | +    socket.end('End');  | 
 | 69 | +  }));  | 
 | 70 | + | 
 | 71 | +  req.end('Request');  | 
 | 72 | +}));  | 
0 commit comments