|
| 1 | +'use strict'; |
| 2 | +const common = require('../common'); |
| 3 | +const assert = require('assert'); |
| 4 | +const http = require('http'); |
| 5 | + |
| 6 | +const baseOptions = { |
| 7 | + method: 'GET', |
| 8 | + port: undefined, |
| 9 | + host: common.localhostIPv4, |
| 10 | +}; |
| 11 | + |
| 12 | +const failingAgentOptions = [ |
| 13 | + true, |
| 14 | + 'agent', |
| 15 | + {}, |
| 16 | + 1, |
| 17 | + () => null, |
| 18 | + Symbol(), |
| 19 | +]; |
| 20 | + |
| 21 | +const acceptableAgentOptions = [ |
| 22 | + false, |
| 23 | + undefined, |
| 24 | + null, |
| 25 | + new http.Agent(), |
| 26 | +]; |
| 27 | + |
| 28 | +const server = http.createServer((req, res) => { |
| 29 | + res.end('hello'); |
| 30 | +}); |
| 31 | + |
| 32 | +let numberOfResponses = 0; |
| 33 | + |
| 34 | +function createRequest(agent) { |
| 35 | + const options = Object.assign(baseOptions, {agent}); |
| 36 | + const request = http.request(options); |
| 37 | + request.end(); |
| 38 | + request.on('response', common.mustCall(() => { |
| 39 | + numberOfResponses++; |
| 40 | + if (numberOfResponses === acceptableAgentOptions.length) { |
| 41 | + server.close(); |
| 42 | + } |
| 43 | + })); |
| 44 | +} |
| 45 | + |
| 46 | +server.listen(0, baseOptions.host, common.mustCall(function() { |
| 47 | + baseOptions.port = this.address().port; |
| 48 | + |
| 49 | + failingAgentOptions.forEach((agent) => { |
| 50 | + assert.throws( |
| 51 | + () => createRequest(agent), |
| 52 | + /^TypeError: Agent option must be an instance of http.Agent/, |
| 53 | + `Expected typeof agent: ${typeof agent} to be rejected` |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + acceptableAgentOptions.forEach((agent) => { |
| 58 | + assert.doesNotThrow(() => createRequest(agent)); |
| 59 | + }); |
| 60 | +})); |
| 61 | + |
| 62 | +process.on('exit', () => { |
| 63 | + assert.strictEqual(numberOfResponses, acceptableAgentOptions.length); |
| 64 | +}); |
0 commit comments