Skip to content

Commit 4e4efb7

Browse files
slushieMyles Borins
authored andcommitted
test: add test for responses to HTTP CONNECT req
See: #6198 PR-URL: #6279 Reviewed-By: Brian White <mscdex@mscdex.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 16b23b2 commit 4e4efb7

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
'use strict';
2+
constcommon=require('../common');
3+
constassert=require('assert');
4+
consthttp=require('http');
5+
6+
constserver=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+
vardata=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+
constreq=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+
constname='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+
vardata=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

Comments
 (0)