Skip to content

Commit cdee945

Browse files
chiaki-yokooaddaleax
authored andcommitted
test: improve https coverage to check create connection
PR-URL: #11435 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yuta Hiroto <[email protected]>
1 parent e468cd3 commit cdee945

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
if(!common.hasCrypto){
5+
common.skip('missing crypto');
6+
return;
7+
}
8+
9+
constassert=require('assert');
10+
consthttps=require('https');
11+
12+
constagent=newhttps.Agent();
13+
14+
constfs=require('fs');
15+
16+
constoptions={
17+
key: fs.readFileSync(common.fixturesDir+'/keys/agent1-key.pem'),
18+
cert: fs.readFileSync(common.fixturesDir+'/keys/agent1-cert.pem'),
19+
};
20+
21+
constserver=https.createServer(options,(req,res)=>{
22+
res.end('hello world\n');
23+
});
24+
25+
constexpectedHeader=/^HTTP\/1.1200OK/;
26+
constexpectedBody=/helloworld\n/;
27+
constexpectCertError=/^Error:unabletoverifythefirstcertificate$/;
28+
29+
constcheckRequest=(socket,server)=>{
30+
letresult='';
31+
socket.on('connect',common.mustCall((data)=>{
32+
socket.write('GET / HTTP/1.1\r\n\r\n');
33+
socket.end();
34+
}));
35+
socket.on('data',common.mustCall((chunk)=>{
36+
result+=chunk;
37+
}));
38+
socket.on('end',common.mustCall(()=>{
39+
assert(expectedHeader.test(result));
40+
assert(expectedBody.test(result));
41+
server.close();
42+
}));
43+
};
44+
45+
// use option connect
46+
server.listen(0,common.mustCall(()=>{
47+
constport=server.address().port;
48+
consthost='localhost';
49+
constoptions={
50+
port: port,
51+
host: host,
52+
rejectUnauthorized: false,
53+
_agentKey: agent.getName({
54+
port: port,
55+
host: host,
56+
}),
57+
};
58+
59+
constsocket=agent.createConnection(options);
60+
checkRequest(socket,server);
61+
}));
62+
63+
// use port and option connect
64+
server.listen(0,common.mustCall(()=>{
65+
constport=server.address().port;
66+
consthost='localhost';
67+
constoptions={
68+
rejectUnauthorized: false,
69+
_agentKey: agent.getName({
70+
port: port,
71+
host: host,
72+
}),
73+
};
74+
constsocket=agent.createConnection(port,options);
75+
checkRequest(socket,server);
76+
}));
77+
78+
// use port and host and option connect
79+
server.listen(0,common.mustCall(()=>{
80+
constport=server.address().port;
81+
consthost='localhost';
82+
constoptions={
83+
rejectUnauthorized: false,
84+
_agentKey: agent.getName({
85+
port: port,
86+
host: host,
87+
}),
88+
};
89+
constsocket=agent.createConnection(port,host,options);
90+
checkRequest(socket,server);
91+
}));
92+
93+
// use port and host and option does not have agentKey
94+
server.listen(0,common.mustCall(()=>{
95+
constport=server.address().port;
96+
consthost='localhost';
97+
constoptions={
98+
rejectUnauthorized: false,
99+
};
100+
constsocket=agent.createConnection(port,host,options);
101+
checkRequest(socket,server);
102+
}));
103+
104+
// options is null
105+
server.listen(0,common.mustCall(()=>{
106+
constport=server.address().port;
107+
consthost='localhost';
108+
constoptions=null;
109+
constsocket=agent.createConnection(port,host,options);
110+
socket.on('error',common.mustCall((e)=>{
111+
assert(expectCertError.test(e.toString()));
112+
}));
113+
}));
114+
115+
// options is undefined
116+
server.listen(0,common.mustCall(()=>{
117+
constport=server.address().port;
118+
consthost='localhost';
119+
constoptions=undefined;
120+
constsocket=agent.createConnection(port,host,options);
121+
socket.on('error',common.mustCall((e)=>{
122+
assert(expectCertError.test(e.toString()));
123+
}));
124+
}));

0 commit comments

Comments
(0)