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'); 9 10const myURL = new URL('http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c'); 11 12assert.strictEqual( 13 url.format(myURL), 14 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 15); 16 17assert.strictEqual( 18 url.format(myURL, {}), 19 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 20); 21 22{ 23 [true, 1, 'test', Infinity].forEach((value) => { 24 assert.throws( 25 () => url.format(myURL, value), 26 { 27 code: 'ERR_INVALID_ARG_TYPE', 28 name: 'TypeError', 29 message: 'The "options" argument must be of type object.' + 30 common.invalidArgTypeHelper(value) 31 } 32 ); 33 }); 34} 35 36// Any falsy value other than undefined will be treated as false. 37// Any truthy value will be treated as true. 38 39assert.strictEqual( 40 url.format(myURL, { auth: false }), 41 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 42); 43 44assert.strictEqual( 45 url.format(myURL, { auth: '' }), 46 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 47); 48 49assert.strictEqual( 50 url.format(myURL, { auth: 0 }), 51 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' 52); 53 54assert.strictEqual( 55 url.format(myURL, { auth: 1 }), 56 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 57); 58 59assert.strictEqual( 60 url.format(myURL, { auth: {} }), 61 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 62); 63 64assert.strictEqual( 65 url.format(myURL, { fragment: false }), 66 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b' 67); 68 69assert.strictEqual( 70 url.format(myURL, { fragment: '' }), 71 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b' 72); 73 74assert.strictEqual( 75 url.format(myURL, { fragment: 0 }), 76 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b' 77); 78 79assert.strictEqual( 80 url.format(myURL, { fragment: 1 }), 81 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 82); 83 84assert.strictEqual( 85 url.format(myURL, { fragment: {} }), 86 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 87); 88 89assert.strictEqual( 90 url.format(myURL, { search: false }), 91 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c' 92); 93 94assert.strictEqual( 95 url.format(myURL, { search: '' }), 96 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c' 97); 98 99assert.strictEqual( 100 url.format(myURL, { search: 0 }), 101 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a#c' 102); 103 104assert.strictEqual( 105 url.format(myURL, { search: 1 }), 106 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 107); 108 109assert.strictEqual( 110 url.format(myURL, { search: {} }), 111 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 112); 113 114assert.strictEqual( 115 url.format(myURL, { unicode: true }), 116 'http://user:pass@理容ナカムラ.com/a?a=b#c' 117); 118 119assert.strictEqual( 120 url.format(myURL, { unicode: 1 }), 121 'http://user:pass@理容ナカムラ.com/a?a=b#c' 122); 123 124assert.strictEqual( 125 url.format(myURL, { unicode: {} }), 126 'http://user:pass@理容ナカムラ.com/a?a=b#c' 127); 128 129assert.strictEqual( 130 url.format(myURL, { unicode: false }), 131 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 132); 133 134assert.strictEqual( 135 url.format(myURL, { unicode: 0 }), 136 'http://user:pass@xn--lck1c3crb1723bpq4a.com/a?a=b#c' 137); 138 139assert.strictEqual( 140 url.format(new URL('http://user:pass@xn--0zwm56d.com:8080/path'), { unicode: true }), 141 'http://user:pass@测试.com:8080/path' 142); 143 144assert.strictEqual( 145 url.format(new URL('tel:123')), 146 url.format(new URL('tel:123'), { unicode: true }) 147); 148