1'use strict'; 2 3const common = require('../common'); 4const fixtures = require('../common/fixtures'); 5if (!common.hasCrypto) 6 common.skip('missing crypto'); 7 8const assert = require('assert'); 9const https = require('https'); 10 11const agent = new https.Agent(); 12 13const options = { 14 key: fixtures.readKey('agent1-key.pem'), 15 cert: fixtures.readKey('agent1-cert.pem'), 16}; 17 18const expectedHeader = /^HTTP\/1\.1 200 OK/; 19const expectedBody = /hello world\n/; 20const expectCertError = /^Error: unable to verify the first certificate$/; 21 22const checkRequest = (socket, server) => { 23 let result = ''; 24 socket.on('connect', common.mustCall((data) => { 25 socket.write('GET / HTTP/1.1\r\n\r\n'); 26 socket.end(); 27 })); 28 socket.on('data', common.mustCall((chunk) => { 29 result += chunk; 30 })); 31 socket.on('end', common.mustCall(() => { 32 assert(expectedHeader.test(result)); 33 assert(expectedBody.test(result)); 34 server.close(); 35 })); 36}; 37 38function createServer() { 39 return https.createServer(options, (req, res) => { 40 res.end('hello world\n'); 41 }); 42} 43 44// use option connect 45{ 46 const server = createServer(); 47 server.listen(0, common.mustCall(() => { 48 const port = server.address().port; 49 const host = 'localhost'; 50 const options = { 51 port: port, 52 host: host, 53 rejectUnauthorized: false, 54 _agentKey: agent.getName({ port, host }) 55 }; 56 57 const socket = agent.createConnection(options); 58 checkRequest(socket, server); 59 })); 60} 61 62// Use port and option connect 63{ 64 const server = createServer(); 65 server.listen(0, common.mustCall(() => { 66 const port = server.address().port; 67 const host = 'localhost'; 68 const options = { 69 rejectUnauthorized: false, 70 _agentKey: agent.getName({ port, host }) 71 }; 72 const socket = agent.createConnection(port, options); 73 checkRequest(socket, server); 74 })); 75} 76 77// Use port and host and option connect 78{ 79 const server = createServer(); 80 server.listen(0, common.mustCall(() => { 81 const port = server.address().port; 82 const host = 'localhost'; 83 const options = { 84 rejectUnauthorized: false, 85 _agentKey: agent.getName({ port, host }) 86 }; 87 const socket = agent.createConnection(port, host, options); 88 checkRequest(socket, server); 89 })); 90} 91 92// Use port and host and option does not have agentKey 93{ 94 const server = createServer(); 95 server.listen(0, common.mustCall(() => { 96 const port = server.address().port; 97 const host = 'localhost'; 98 const options = { 99 rejectUnauthorized: false, 100 }; 101 const socket = agent.createConnection(port, host, options); 102 checkRequest(socket, server); 103 })); 104} 105 106// `options` is null 107{ 108 const server = createServer(); 109 server.listen(0, common.mustCall(() => { 110 const port = server.address().port; 111 const host = 'localhost'; 112 const options = null; 113 const socket = agent.createConnection(port, host, options); 114 socket.on('error', common.mustCall((e) => { 115 assert(expectCertError.test(e.toString())); 116 server.close(); 117 })); 118 })); 119} 120 121// `options` is undefined 122{ 123 const server = createServer(); 124 server.listen(0, common.mustCall(() => { 125 const port = server.address().port; 126 const host = 'localhost'; 127 const options = undefined; 128 const socket = agent.createConnection(port, host, options); 129 socket.on('error', common.mustCall((e) => { 130 assert(expectCertError.test(e.toString())); 131 server.close(); 132 })); 133 })); 134} 135 136// `options` should not be modified 137{ 138 const server = createServer(); 139 server.listen(0, common.mustCall(() => { 140 const port = server.address().port; 141 const host = 'localhost'; 142 const options = { 143 port: 3000, 144 rejectUnauthorized: false 145 }; 146 147 const socket = agent.createConnection(port, host, options); 148 socket.on('connect', common.mustCall((data) => { 149 socket.end(); 150 })); 151 socket.on('end', common.mustCall(() => { 152 assert.deepStrictEqual(options, { 153 port: 3000, rejectUnauthorized: false 154 }); 155 server.close(); 156 })); 157 })); 158} 159