1'use strict';
2const common = require('../common');
3const assert = require('assert');
4const url = require('url');
5
6// https://github.com/joyent/node/issues/568
7[
8  [undefined, 'undefined'],
9  [null, 'object'],
10  [true, 'boolean'],
11  [false, 'boolean'],
12  [0.0, 'number'],
13  [0, 'number'],
14  [[], 'object'],
15  [{}, 'object'],
16  [() => {}, 'function'],
17  [Symbol('foo'), 'symbol'],
18].forEach(([val, type]) => {
19  assert.throws(() => {
20    url.parse(val);
21  }, {
22    code: 'ERR_INVALID_ARG_TYPE',
23    name: 'TypeError',
24    message: 'The "url" argument must be of type string.' +
25             common.invalidArgTypeHelper(val)
26  });
27});
28
29assert.throws(() => { url.parse('http://%E0%A4%A@fail'); },
30              (e) => {
31                // The error should be a URIError.
32                if (!(e instanceof URIError))
33                  return false;
34
35                // The error should be from the JS engine and not from Node.js.
36                // JS engine errors do not have the `code` property.
37                return e.code === undefined;
38              });
39
40assert.throws(() => { url.parse('http://[127.0.0.1\x00c8763]:8000/'); },
41              { code: 'ERR_INVALID_URL', input: 'http://[127.0.0.1\x00c8763]:8000/' }
42);
43
44if (common.hasIntl) {
45  // An array of Unicode code points whose Unicode NFKD contains a "bad
46  // character".
47  const badIDNA = (() => {
48    const BAD_CHARS = '#%/:?@[\\]^|';
49    const out = [];
50    for (let i = 0x80; i < 0x110000; i++) {
51      const cp = String.fromCodePoint(i);
52      for (const badChar of BAD_CHARS) {
53        if (cp.normalize('NFKD').includes(badChar)) {
54          out.push(cp);
55        }
56      }
57    }
58    return out;
59  })();
60
61  // The generation logic above should at a minimum produce these two
62  // characters.
63  assert(badIDNA.includes('℀'));
64  assert(badIDNA.includes('@'));
65
66  for (const badCodePoint of badIDNA) {
67    const badURL = `http://fail${badCodePoint}fail.com/`;
68    assert.throws(() => { url.parse(badURL); },
69                  (e) => e.code === 'ERR_INVALID_URL',
70                  `parsing ${badURL}`);
71  }
72
73  assert.throws(() => { url.parse('http://\u00AD/bad.com/'); },
74                (e) => e.code === 'ERR_INVALID_URL',
75                'parsing http://\u00AD/bad.com/');
76}
77