From 2da71f24def62aa2858c005548e378f042de0e20 Mon Sep 17 00:00:00 2001 From: Jay Brownlee Date: Sun, 11 Dec 2016 18:20:49 -0600 Subject: [PATCH] url, test: fix typo in inspect output, add test In the string returned from URL.inspect there was an extra semicolon at the end when showHidden === true. The semicolon has been removed and a test for the inspect function has been added. The test parses the returned string, validates all of the contained keys/values and tests logic related to the showHidden option. PR-URL: https://github.com/nodejs/node/pull/10231 Reviewed-By: Anna Henningsen Reviewed-By: Joyee Cheung Reviewed-By: Evan Lucas --- lib/internal/url.js | 2 +- test/parallel/test-whatwg-url-parsing.js | 68 ++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index acec7b8f6a324d..e97fb3931607f2 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -440,7 +440,7 @@ class URL { ret += ` hash: ${this.hash}\n`; if (opts.showHidden) { ret += ` cannot-be-base: ${this[cannotBeBase]}\n`; - ret += ` special: ${this[special]}\n;`; + ret += ` special: ${this[special]}\n`; } ret += '}'; return ret; diff --git a/test/parallel/test-whatwg-url-parsing.js b/test/parallel/test-whatwg-url-parsing.js index 40ada010c1dd36..99bc3ce1c63c94 100644 --- a/test/parallel/test-whatwg-url-parsing.js +++ b/test/parallel/test-whatwg-url-parsing.js @@ -127,3 +127,71 @@ additional_tests.forEach((test) => { if (test.search) assert.strictEqual(test.search, u.search); if (test.hash) assert.strictEqual(test.hash, u.hash); }); + +// test inspect +const allTests = additional_tests.slice(); +for (const test of tests) { + if (test.failure || typeof test === 'string') continue; + allTests.push(test); +} + +for (const test of allTests) { + const url = test.url + ? new URL(test.url) + : new URL(test.input, test.base); + + for (const showHidden of [true, false]) { + const res = url.inspect(null, { + showHidden: showHidden + }); + + const lines = res.split('\n'); + + const firstLine = lines[0]; + assert.strictEqual(firstLine, 'URL {'); + + const lastLine = lines[lines.length - 1]; + assert.strictEqual(lastLine, '}'); + + const innerLines = lines.slice(1, lines.length - 1); + const keys = new Set(); + for (const line of innerLines) { + const i = line.indexOf(': '); + const k = line.slice(0, i).trim(); + const v = line.slice(i + 2); + assert.strictEqual(keys.has(k), false, 'duplicate key found: ' + k); + keys.add(k); + + const hidden = new Set([ + 'password', + 'cannot-be-base', + 'special' + ]); + if (showHidden) { + if (!hidden.has(k)) { + assert.strictEqual(v, url[k], k); + continue; + } + + if (k === 'password') { + assert.strictEqual(v, url[k], k); + } + if (k === 'cannot-be-base') { + assert.ok(v.match(/^true$|^false$/), k + ' is Boolean'); + } + if (k === 'special') { + assert.ok(v.match(/^true$|^false$/), k + ' is Boolean'); + } + continue; + } + + // showHidden is false + if (k === 'password') { + assert.strictEqual(v, '--------', k); + continue; + } + assert.strictEqual(hidden.has(k), false, 'no hidden keys: ' + k); + assert.strictEqual(v, url[k], k); + } + } +}