11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ciconst common = require('../common'); 31cb0ef41Sopenharmony_ciconst assert = require('assert'); 41cb0ef41Sopenharmony_ciconst url = require('url'); 51cb0ef41Sopenharmony_ci 61cb0ef41Sopenharmony_ci// https://github.com/joyent/node/issues/568 71cb0ef41Sopenharmony_ci[ 81cb0ef41Sopenharmony_ci [undefined, 'undefined'], 91cb0ef41Sopenharmony_ci [null, 'object'], 101cb0ef41Sopenharmony_ci [true, 'boolean'], 111cb0ef41Sopenharmony_ci [false, 'boolean'], 121cb0ef41Sopenharmony_ci [0.0, 'number'], 131cb0ef41Sopenharmony_ci [0, 'number'], 141cb0ef41Sopenharmony_ci [[], 'object'], 151cb0ef41Sopenharmony_ci [{}, 'object'], 161cb0ef41Sopenharmony_ci [() => {}, 'function'], 171cb0ef41Sopenharmony_ci [Symbol('foo'), 'symbol'], 181cb0ef41Sopenharmony_ci].forEach(([val, type]) => { 191cb0ef41Sopenharmony_ci assert.throws(() => { 201cb0ef41Sopenharmony_ci url.parse(val); 211cb0ef41Sopenharmony_ci }, { 221cb0ef41Sopenharmony_ci code: 'ERR_INVALID_ARG_TYPE', 231cb0ef41Sopenharmony_ci name: 'TypeError', 241cb0ef41Sopenharmony_ci message: 'The "url" argument must be of type string.' + 251cb0ef41Sopenharmony_ci common.invalidArgTypeHelper(val) 261cb0ef41Sopenharmony_ci }); 271cb0ef41Sopenharmony_ci}); 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ciassert.throws(() => { url.parse('http://%E0%A4%A@fail'); }, 301cb0ef41Sopenharmony_ci (e) => { 311cb0ef41Sopenharmony_ci // The error should be a URIError. 321cb0ef41Sopenharmony_ci if (!(e instanceof URIError)) 331cb0ef41Sopenharmony_ci return false; 341cb0ef41Sopenharmony_ci 351cb0ef41Sopenharmony_ci // The error should be from the JS engine and not from Node.js. 361cb0ef41Sopenharmony_ci // JS engine errors do not have the `code` property. 371cb0ef41Sopenharmony_ci return e.code === undefined; 381cb0ef41Sopenharmony_ci }); 391cb0ef41Sopenharmony_ci 401cb0ef41Sopenharmony_ciassert.throws(() => { url.parse('http://[127.0.0.1\x00c8763]:8000/'); }, 411cb0ef41Sopenharmony_ci { code: 'ERR_INVALID_URL', input: 'http://[127.0.0.1\x00c8763]:8000/' } 421cb0ef41Sopenharmony_ci); 431cb0ef41Sopenharmony_ci 441cb0ef41Sopenharmony_ciif (common.hasIntl) { 451cb0ef41Sopenharmony_ci // An array of Unicode code points whose Unicode NFKD contains a "bad 461cb0ef41Sopenharmony_ci // character". 471cb0ef41Sopenharmony_ci const badIDNA = (() => { 481cb0ef41Sopenharmony_ci const BAD_CHARS = '#%/:?@[\\]^|'; 491cb0ef41Sopenharmony_ci const out = []; 501cb0ef41Sopenharmony_ci for (let i = 0x80; i < 0x110000; i++) { 511cb0ef41Sopenharmony_ci const cp = String.fromCodePoint(i); 521cb0ef41Sopenharmony_ci for (const badChar of BAD_CHARS) { 531cb0ef41Sopenharmony_ci if (cp.normalize('NFKD').includes(badChar)) { 541cb0ef41Sopenharmony_ci out.push(cp); 551cb0ef41Sopenharmony_ci } 561cb0ef41Sopenharmony_ci } 571cb0ef41Sopenharmony_ci } 581cb0ef41Sopenharmony_ci return out; 591cb0ef41Sopenharmony_ci })(); 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ci // The generation logic above should at a minimum produce these two 621cb0ef41Sopenharmony_ci // characters. 631cb0ef41Sopenharmony_ci assert(badIDNA.includes('℀')); 641cb0ef41Sopenharmony_ci assert(badIDNA.includes('@')); 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci for (const badCodePoint of badIDNA) { 671cb0ef41Sopenharmony_ci const badURL = `http://fail${badCodePoint}fail.com/`; 681cb0ef41Sopenharmony_ci assert.throws(() => { url.parse(badURL); }, 691cb0ef41Sopenharmony_ci (e) => e.code === 'ERR_INVALID_URL', 701cb0ef41Sopenharmony_ci `parsing ${badURL}`); 711cb0ef41Sopenharmony_ci } 721cb0ef41Sopenharmony_ci 731cb0ef41Sopenharmony_ci assert.throws(() => { url.parse('http://\u00AD/bad.com/'); }, 741cb0ef41Sopenharmony_ci (e) => e.code === 'ERR_INVALID_URL', 751cb0ef41Sopenharmony_ci 'parsing http://\u00AD/bad.com/'); 761cb0ef41Sopenharmony_ci} 77