1'use strict';
2
3require('../common');
4const assert = require('assert');
5const http = require('http');
6const path = require('path');
7
8const tmpdir = require('../common/tmpdir');
9
10const agent = new http.Agent();
11
12// Default to localhost
13assert.strictEqual(
14  agent.getName({
15    port: 80,
16    localAddress: '192.168.1.1'
17  }),
18  'localhost:80:192.168.1.1'
19);
20
21// empty argument
22assert.strictEqual(
23  agent.getName(),
24  'localhost::'
25);
26
27// empty options
28assert.strictEqual(
29  agent.getName({}),
30  'localhost::'
31);
32
33// pass all arguments
34assert.strictEqual(
35  agent.getName({
36    host: '0.0.0.0',
37    port: 80,
38    localAddress: '192.168.1.1'
39  }),
40  '0.0.0.0:80:192.168.1.1'
41);
42
43// unix socket
44const socketPath = path.join(tmpdir.path, 'foo', 'bar');
45assert.strictEqual(
46  agent.getName({
47    socketPath
48  }),
49  `localhost:::${socketPath}`
50);
51
52for (const family of [0, null, undefined, 'bogus'])
53  assert.strictEqual(agent.getName({ family }), 'localhost::');
54
55for (const family of [4, 6])
56  assert.strictEqual(agent.getName({ family }), `localhost:::${family}`);
57