Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorchiaki-yokoo <chiaki_yokoo@r.recruit.co.jp>2017-02-17 06:55:27 +0300
committerAnna Henningsen <anna@addaleax.net>2017-03-06 01:23:29 +0300
commitcdee945307aae3b0a7366dd06afb7c157c67e0a2 (patch)
treeb2250bff6abe2cc73bca6e48e7b4ad9831320671 /test
parente468cd3ee79e51e7306d86731106e4ac80ab2d22 (diff)
test: improve https coverage to check create connection
PR-URL: https://github.com/nodejs/node/pull/11435 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com>
Diffstat (limited to 'test')
-rw-r--r--test/parallel/test-https-agent-create-connection.js124
1 files changed, 124 insertions, 0 deletions
diff --git a/test/parallel/test-https-agent-create-connection.js b/test/parallel/test-https-agent-create-connection.js
new file mode 100644
index 00000000000..f593700081a
--- /dev/null
+++ b/test/parallel/test-https-agent-create-connection.js
@@ -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()));
+ }));
+}));