Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve https coverage to check create connection #11435

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 124 additions & 0 deletions test/parallel/test-https-agent-create-connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
'use strict';

const common = require('../common');
if (!common.hasCrypto) {
common.skip('missing crypto');
return;
}

const assert = require('assert');
const https = require('https');

const agent = new https.Agent();

const fs = require('fs');

const options = {
key: fs.readFileSync(common.fixturesDir + '/keys/agent1-key.pem'),
cert: fs.readFileSync(common.fixturesDir + '/keys/agent1-cert.pem'),
};

const server = https.createServer(options, (req, res) => {
res.end('hello world\n');
});

const expectedHeader = /^HTTP\/1.1 200 OK/;
const expectedBody = /hello world\n/;
const expectCertError = /^Error: unable to verify the first certificate$/;

const checkRequest = (socket, server) => {
let result = '';
socket.on('connect', common.mustCall((data) => {
socket.write('GET / HTTP/1.1\r\n\r\n');
socket.end();
}));
socket.on('data', common.mustCall((chunk) => {
result += chunk;
}));
socket.on('end', common.mustCall(() => {
assert(expectedHeader.test(result));
assert(expectedBody.test(result));
server.close();
}));
};

// use option connect
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const host = 'localhost';
const options = {
port: port,
host: host,
rejectUnauthorized: false,
_agentKey: agent.getName({
port: port,
host: host,
}),
};

const socket = agent.createConnection(options);
checkRequest(socket, server);
}));

// use port and option connect
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const host = 'localhost';
const options = {
rejectUnauthorized: false,
_agentKey: agent.getName({
port: port,
host: host,
}),
};
const socket = agent.createConnection(port, options);
checkRequest(socket, server);
}));

// use port and host and option connect
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const host = 'localhost';
const options = {
rejectUnauthorized: false,
_agentKey: agent.getName({
port: port,
host: host,
}),
};
const socket = agent.createConnection(port, host, options);
checkRequest(socket, server);
}));

// use port and host and option does not have agentKey
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const host = 'localhost';
const options = {
rejectUnauthorized: false,
};
const socket = agent.createConnection(port, host, options);
checkRequest(socket, server);
}));

// options is null
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const host = 'localhost';
const options = null;
const socket = agent.createConnection(port, host, options);
socket.on('error', common.mustCall((e) => {
assert(expectCertError.test(e.toString()));
}));
}));

// options is undefined
server.listen(0, common.mustCall(() => {
const port = server.address().port;
const host = 'localhost';
const options = undefined;
const socket = agent.createConnection(port, host, options);
socket.on('error', common.mustCall((e) => {
assert(expectCertError.test(e.toString()));
}));
}));