Skip to content

Commit 678e225

Browse files
chiaki-yokooMylesBorins
authored andcommitted
test: improve https coverage to check create connection
PR-URL: #11435 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
1 parent f0288f3 commit 678e225

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+
const common = require('../common');
4+
if (!common.hasCrypto) {
5+
common.skip('missing crypto');
6+
return;
7+
}
8+
9+
const assert = require('assert');
10+
const https = require('https');
11+
12+
const agent = new https.Agent();
13+
14+
const fs = require('fs');
15+
16+
const options = {
17+
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
18+
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem'),
19+
};
20+
21+
const server = https.createServer(options, (req, res) => {
22+
res.end('hello world\n');
23+
});
24+
25+
const expectedHeader = /^HTTP\/1.1 200 OK/;
26+
const expectedBody = /hello world\n/;
27+
const expectCertError = /^Error: unable to verify the first certificate$/;
28+
29+
const checkRequest = (socket, server) => {
30+
let result = '';
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+
const port = server.address().port;
48+
const host = 'localhost';
49+
const options = {
50+
port: port,
51+
host: host,
52+
rejectUnauthorized: false,
53+
_agentKey: agent.getName({
54+
port: port,
55+
host: host,
56+
}),
57+
};
58+
59+
const socket = agent.createConnection(options);
60+
checkRequest(socket, server);
61+
}));
62+
63+
// use port and option connect
64+
server.listen(0, common.mustCall(() => {
65+
const port = server.address().port;
66+
const host = 'localhost';
67+
const options = {
68+
rejectUnauthorized: false,
69+
_agentKey: agent.getName({
70+
port: port,
71+
host: host,
72+
}),
73+
};
74+
const socket = 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+
const port = server.address().port;
81+
const host = 'localhost';
82+
const options = {
83+
rejectUnauthorized: false,
84+
_agentKey: agent.getName({
85+
port: port,
86+
host: host,
87+
}),
88+
};
89+
const socket = 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+
const port = server.address().port;
96+
const host = 'localhost';
97+
const options = {
98+
rejectUnauthorized: false,
99+
};
100+
const socket = agent.createConnection(port, host, options);
101+
checkRequest(socket, server);
102+
}));
103+
104+
// options is null
105+
server.listen(0, common.mustCall(() => {
106+
const port = server.address().port;
107+
const host = 'localhost';
108+
const options = null;
109+
const socket = 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+
const port = server.address().port;
118+
const host = 'localhost';
119+
const options = undefined;
120+
const socket = 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)