1'use strict'; 2 3const common = require('../common'); 4if (!common.hasIntl) 5 common.skip('missing Intl'); 6 7const assert = require('assert'); 8const url = require('url'); 9const URL = url.URL; 10 11const myURL = new URL('http://xn--lck1c3crb1723bpq4a.com/a?a=b#c'); 12 13assert.strictEqual( 14 url.format(myURL), 15 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 16); 17 18assert.strictEqual( 19 url.format(myURL, {}), 20 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 21); 22 23{ 24 [true, 1, 'test', Infinity].forEach((value) => { 25 assert.throws( 26 () => url.format(myURL, value), 27 { 28 code: 'ERR_INVALID_ARG_TYPE', 29 name: 'TypeError', 30 message: 'The "options" argument must be of type object.' + 31 common.invalidArgTypeHelper(value) 32 } 33 ); 34 }); 35} 36 37// Any falsy value other than undefined will be treated as false. 38// Any truthy value will be treated as true. 39 40assert.strictEqual( 41 url.format(myURL, { fragment: false }), 42 'http://xn--lck1c3crb1723bpq4a.com/a?a=b' 43); 44 45assert.strictEqual( 46 url.format(myURL, { fragment: '' }), 47 'http://xn--lck1c3crb1723bpq4a.com/a?a=b' 48); 49 50assert.strictEqual( 51 url.format(myURL, { fragment: 0 }), 52 'http://xn--lck1c3crb1723bpq4a.com/a?a=b' 53); 54 55assert.strictEqual( 56 url.format(myURL, { fragment: 1 }), 57 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 58); 59 60assert.strictEqual( 61 url.format(myURL, { fragment: {} }), 62 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 63); 64 65assert.strictEqual( 66 url.format(myURL, { search: false }), 67 'http://xn--lck1c3crb1723bpq4a.com/a#c' 68); 69 70assert.strictEqual( 71 url.format(myURL, { search: '' }), 72 'http://xn--lck1c3crb1723bpq4a.com/a#c' 73); 74 75assert.strictEqual( 76 url.format(myURL, { search: 0 }), 77 'http://xn--lck1c3crb1723bpq4a.com/a#c' 78); 79 80assert.strictEqual( 81 url.format(myURL, { search: 1 }), 82 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 83); 84 85assert.strictEqual( 86 url.format(myURL, { search: {} }), 87 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 88); 89 90assert.strictEqual( 91 url.format(myURL, { unicode: true }), 92 'http://理容ナカムラ.com/a?a=b#c' 93); 94 95assert.strictEqual( 96 url.format(myURL, { unicode: 1 }), 97 'http://理容ナカムラ.com/a?a=b#c' 98); 99 100assert.strictEqual( 101 url.format(myURL, { unicode: {} }), 102 'http://理容ナカムラ.com/a?a=b#c' 103); 104 105assert.strictEqual( 106 url.format(myURL, { unicode: false }), 107 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 108); 109 110assert.strictEqual( 111 url.format(myURL, { unicode: 0 }), 112 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 113); 114 115assert.strictEqual( 116 url.format(new URL('http://xn--0zwm56d.com:8080/path'), { unicode: true }), 117 'http://测试.com:8080/path' 118); 119