• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2require('../common');
3const assert = require('assert');
4const { urlToHttpOptions } = require('url');
5
6// Test urlToHttpOptions
7const urlObj = new URL('http://user:pass@foo.bar.com:21/aaa/zzz?l=24#test');
8const opts = urlToHttpOptions(urlObj);
9assert.strictEqual(opts instanceof URL, false);
10assert.strictEqual(opts.protocol, 'http:');
11assert.strictEqual(opts.auth, 'user:pass');
12assert.strictEqual(opts.hostname, 'foo.bar.com');
13assert.strictEqual(opts.port, 21);
14assert.strictEqual(opts.path, '/aaa/zzz?l=24');
15assert.strictEqual(opts.pathname, '/aaa/zzz');
16assert.strictEqual(opts.search, '?l=24');
17assert.strictEqual(opts.hash, '#test');
18
19const { hostname } = urlToHttpOptions(new URL('http://[::1]:21'));
20assert.strictEqual(hostname, '::1');
21
22// If a WHATWG URL object is copied, it is possible that the resulting copy
23// contains the Symbols that Node uses for brand checking, but not the data
24// properties, which are getters. Verify that urlToHttpOptions() can handle
25// such a case.
26const copiedUrlObj = { ...urlObj };
27const copiedOpts = urlToHttpOptions(copiedUrlObj);
28assert.strictEqual(copiedOpts instanceof URL, false);
29assert.strictEqual(copiedOpts.protocol, undefined);
30assert.strictEqual(copiedOpts.auth, undefined);
31assert.strictEqual(copiedOpts.hostname, undefined);
32assert.strictEqual(copiedOpts.port, NaN);
33assert.strictEqual(copiedOpts.path, '');
34assert.strictEqual(copiedOpts.pathname, undefined);
35assert.strictEqual(copiedOpts.search, undefined);
36assert.strictEqual(copiedOpts.hash, undefined);
37assert.strictEqual(copiedOpts.href, undefined);
38