diff options
author | Timothy Gu <timothygu99@gmail.com> | 2017-04-21 11:54:58 -0700 |
---|---|---|
committer | Timothy Gu <timothygu99@gmail.com> | 2017-04-25 16:33:08 -0700 |
commit | d457a986a01b84c4cee3a952082caac3f118e1db (patch) | |
tree | 7af9a6c6de2554c28c47ffc8823ed7beeb13e98c /test/parallel | |
parent | b7a341d7e5868cd1279c33e9f62c388a1e886110 (diff) | |
download | android-node-v8-d457a986a01b84c4cee3a952082caac3f118e1db.tar.gz android-node-v8-d457a986a01b84c4cee3a952082caac3f118e1db.tar.bz2 android-node-v8-d457a986a01b84c4cee3a952082caac3f118e1db.zip |
url: port WHATWG URL API to internal/errors
Also slightly revises grammar.
PR-URL: https://github.com/nodejs/node/pull/12574
Refs: https://github.com/nodejs/node/issues/11273
Refs: https://github.com/nodejs/node/issues/11299
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
Diffstat (limited to 'test/parallel')
18 files changed, 200 insertions, 63 deletions
diff --git a/test/parallel/test-fs-whatwg-url.js b/test/parallel/test-fs-whatwg-url.js index 7a94fd68d7..ff2ee641e1 100644 --- a/test/parallel/test-fs-whatwg-url.js +++ b/test/parallel/test-fs-whatwg-url.js @@ -30,9 +30,11 @@ fs.readFile(url, common.mustCall((err, data) => { // Check that using a non file:// URL reports an error const httpUrl = new URL('http://example.org'); fs.readFile(httpUrl, common.mustCall((err) => { - assert(err); - assert.strictEqual(err.message, - 'Only `file:` URLs are supported'); + common.expectsError({ + code: 'ERR_INVALID_URL_SCHEME', + type: TypeError, + message: 'The URL must be of scheme file' + })(err); })); // pct-encoded characters in the path will be decoded and checked @@ -46,25 +48,30 @@ if (common.isWindows) { // encoded back and forward slashes are not permitted on windows ['%2f', '%2F', '%5c', '%5C'].forEach((i) => { fs.readFile(new URL(`file:///c:/tmp/${i}`), common.mustCall((err) => { - assert(err); - assert.strictEqual(err.message, - 'Path must not include encoded \\ or / characters'); + common.expectsError({ + code: 'ERR_INVALID_FILE_URL_PATH', + type: TypeError, + message: 'File URL path must not include encoded \\ or / characters' + })(err); })); }); } else { // encoded forward slashes are not permitted on other platforms ['%2f', '%2F'].forEach((i) => { fs.readFile(new URL(`file:///c:/tmp/${i}`), common.mustCall((err) => { - assert(err); - assert.strictEqual(err.message, - 'Path must not include encoded / characters'); + common.expectsError({ + code: 'ERR_INVALID_FILE_URL_PATH', + type: TypeError, + message: 'File URL path must not include encoded / characters' + })(err); })); }); fs.readFile(new URL('file://hostname/a/b/c'), common.mustCall((err) => { - assert(err); - assert.strictEqual(err.message, - `File URLs on ${os.platform()} must use ` + - 'hostname \'localhost\' or not specify any hostname'); + common.expectsError({ + code: 'ERR_INVALID_FILE_URL_HOST', + type: TypeError, + message: `File URL host must be "localhost" or empty on ${os.platform()}` + })(err); })); } diff --git a/test/parallel/test-internal-errors.js b/test/parallel/test-internal-errors.js index fbd627b030..dd4f02c1f7 100644 --- a/test/parallel/test-internal-errors.js +++ b/test/parallel/test-internal-errors.js @@ -137,7 +137,7 @@ assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b']), assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', ['b']]), 'The "a" argument must be of type b'); assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', ['b', 'c']]), - 'The "a" argument must be one of type b, or c'); + 'The "a" argument must be one of type b or c'); assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', ['b', 'c', 'd']]), 'The "a" argument must be one of type b, c, or d'); @@ -150,3 +150,29 @@ assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', assert.strictEqual(errors.message('ERR_INVALID_ARG_TYPE', ['a', 'b', null]), 'The "a" argument must be of type b. Received type null'); + +// Test ERR_INVALID_URL_SCHEME +assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', ['file']), + 'The URL must be of scheme file'); +assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['file']]), + 'The URL must be of scheme file'); +assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['http', 'ftp']]), + 'The URL must be one of scheme http or ftp'); +assert.strictEqual(errors.message('ERR_INVALID_URL_SCHEME', [['a', 'b', 'c']]), + 'The URL must be one of scheme a, b, or c'); +assert.throws( + () => errors.message('ERR_INVALID_URL_SCHEME', [[]]), + /^AssertionError: At least one expected value needs to be specified$/ +); + +// Test ERR_MISSING_ARGS +assert.strictEqual(errors.message('ERR_MISSING_ARGS', ['name']), + 'The "name" argument must be specified'); +assert.strictEqual(errors.message('ERR_MISSING_ARGS', ['name', 'value']), + 'The "name" and "value" arguments must be specified'); +assert.strictEqual(errors.message('ERR_MISSING_ARGS', ['a', 'b', 'c']), + 'The "a", "b", and "c" arguments must be specified'); +assert.throws( + () => errors.message('ERR_MISSING_ARGS'), + /^AssertionError: At least one arg needs to be specified$/ +); diff --git a/test/parallel/test-url-format-whatwg.js b/test/parallel/test-url-format-whatwg.js index 507d3f8419..f9e5691ae5 100644 --- a/test/parallel/test-url-format-whatwg.js +++ b/test/parallel/test-url-format-whatwg.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const url = require('url'); const URL = url.URL; @@ -17,11 +17,17 @@ assert.strictEqual( 'http://xn--lck1c3crb1723bpq4a.com/a?a=b#c' ); -const errreg = /^TypeError: options must be an object$/; -assert.throws(() => url.format(myURL, true), errreg); -assert.throws(() => url.format(myURL, 1), errreg); -assert.throws(() => url.format(myURL, 'test'), errreg); -assert.throws(() => url.format(myURL, Infinity), errreg); +{ + const expectedErr = common.expectsError({ + code: 'ERR_INVALID_ARG_TYPE', + type: TypeError, + message: 'The "options" argument must be of type object' + }); + assert.throws(() => url.format(myURL, true), expectedErr); + assert.throws(() => url.format(myURL, 1), expectedErr); + assert.throws(() => url.format(myURL, 'test'), expectedErr); + assert.throws(() => url.format(myURL, Infinity), expectedErr); +} // Any falsy value other than undefined will be treated as false. // Any truthy value will be treated as true. diff --git a/test/parallel/test-whatwg-url-domainto.js b/test/parallel/test-whatwg-url-domainto.js index 70b32c8dce..13ff996870 100644 --- a/test/parallel/test-whatwg-url-domainto.js +++ b/test/parallel/test-whatwg-url-domainto.js @@ -13,10 +13,10 @@ const { domainToASCII, domainToUnicode } = require('url'); const tests = require('../fixtures/url-idna.js'); { - assert.throws(() => domainToASCII(), - /^TypeError: "domain" argument must be specified$/); - assert.throws(() => domainToUnicode(), - /^TypeError: "domain" argument must be specified$/); + const expectedError = common.expectsError( + { code: 'ERR_MISSING_ARGS', type: TypeError }); + assert.throws(() => domainToASCII(), expectedError); + assert.throws(() => domainToUnicode(), expectedError); assert.strictEqual(domainToASCII(undefined), 'undefined'); assert.strictEqual(domainToUnicode(undefined), 'undefined'); } diff --git a/test/parallel/test-whatwg-url-parsing.js b/test/parallel/test-whatwg-url-parsing.js index c82461c25e..0e8f3b6d77 100644 --- a/test/parallel/test-whatwg-url-parsing.js +++ b/test/parallel/test-whatwg-url-parsing.js @@ -26,12 +26,18 @@ const failureTests = tests.filter((test) => test.failure).concat([ { input: common.noop } ]); +const expectedError = common.expectsError( + { code: 'ERR_INVALID_URL', type: TypeError }); + for (const test of failureTests) { assert.throws( () => new URL(test.input, test.base), (error) => { + if (!expectedError(error)) + return false; + // The input could be processed, so we don't do strict matching here - const match = (error + '').match(/^TypeError: Invalid URL: (.*)$/); + const match = (error + '').match(/Invalid URL: (.*)$/); if (!match) { return false; } diff --git a/test/parallel/test-whatwg-url-searchparams-append.js b/test/parallel/test-whatwg-url-searchparams-append.js index ff4a568c30..9d1eaceba1 100644 --- a/test/parallel/test-whatwg-url-searchparams-append.js +++ b/test/parallel/test-whatwg-url-searchparams-append.js @@ -53,10 +53,18 @@ test(function() { const params = new URLSearchParams(); assert.throws(() => { params.append.call(undefined); - }, /^TypeError: Value of `this` is not a URLSearchParams$/); + }, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' + })); assert.throws(() => { - params.set('a'); - }, /^TypeError: "name" and "value" arguments must be specified$/); + params.append('a'); + }, common.expectsError({ + code: 'ERR_MISSING_ARGS', + type: TypeError, + message: 'The "name" and "value" arguments must be specified' + })); const obj = { toString() { throw new Error('toString'); }, diff --git a/test/parallel/test-whatwg-url-searchparams-constructor.js b/test/parallel/test-whatwg-url-searchparams-constructor.js index da459fe99c..e22ceec570 100644 --- a/test/parallel/test-whatwg-url-searchparams-constructor.js +++ b/test/parallel/test-whatwg-url-searchparams-constructor.js @@ -188,24 +188,31 @@ test(() => { } { + const iterableError = common.expectsError({ + code: 'ERR_ARG_NOT_ITERABLE', + type: TypeError, + message: 'Query pairs must be iterable' + }); + const tupleError = common.expectsError({ + code: 'ERR_INVALID_TUPLE', + type: TypeError, + message: 'Each query pair must be an iterable [name, value] tuple' + }); + let params; // URLSearchParams constructor, undefined and null as argument params = new URLSearchParams(undefined); assert.strictEqual(params.toString(), ''); params = new URLSearchParams(null); assert.strictEqual(params.toString(), ''); - assert.throws(() => new URLSearchParams([[1]]), - /^TypeError: Each query pair must be a name\/value tuple$/); - assert.throws(() => new URLSearchParams([[1, 2, 3]]), - /^TypeError: Each query pair must be a name\/value tuple$/); + assert.throws(() => new URLSearchParams([[1]]), tupleError); + assert.throws(() => new URLSearchParams([[1, 2, 3]]), tupleError); assert.throws(() => new URLSearchParams({ [Symbol.iterator]: 42 }), - /^TypeError: Query pairs must be iterable$/); - assert.throws(() => new URLSearchParams([{}]), - /^TypeError: Each query pair must be iterable$/); - assert.throws(() => new URLSearchParams(['a']), - /^TypeError: Each query pair must be iterable$/); + iterableError); + assert.throws(() => new URLSearchParams([{}]), tupleError); + assert.throws(() => new URLSearchParams(['a']), tupleError); assert.throws(() => new URLSearchParams([{ [Symbol.iterator]: 42 }]), - /^TypeError: Each query pair must be iterable$/); + tupleError); } { diff --git a/test/parallel/test-whatwg-url-searchparams-delete.js b/test/parallel/test-whatwg-url-searchparams-delete.js index 589fbc2f86..bb3fcc9549 100644 --- a/test/parallel/test-whatwg-url-searchparams-delete.js +++ b/test/parallel/test-whatwg-url-searchparams-delete.js @@ -47,10 +47,18 @@ test(function() { const params = new URLSearchParams(); assert.throws(() => { params.delete.call(undefined); - }, /^TypeError: Value of `this` is not a URLSearchParams$/); + }, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' + })); assert.throws(() => { params.delete(); - }, /^TypeError: "name" argument must be specified$/); + }, common.expectsError({ + code: 'ERR_MISSING_ARGS', + type: TypeError, + message: 'The "name" argument must be specified' + })); const obj = { toString() { throw new Error('toString'); }, diff --git a/test/parallel/test-whatwg-url-searchparams-entries.js b/test/parallel/test-whatwg-url-searchparams-entries.js index c8b8d129b0..1dfcdb0338 100644 --- a/test/parallel/test-whatwg-url-searchparams-entries.js +++ b/test/parallel/test-whatwg-url-searchparams-entries.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const URLSearchParams = require('url').URLSearchParams; @@ -28,7 +28,15 @@ assert.deepStrictEqual(entries.next(), { assert.throws(() => { entries.next.call(undefined); -}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/); +}, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParamsIterator' +})); assert.throws(() => { params.entries.call(undefined); -}, /^TypeError: Value of `this` is not a URLSearchParams$/); +}, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' +})); diff --git a/test/parallel/test-whatwg-url-searchparams-foreach.js b/test/parallel/test-whatwg-url-searchparams-foreach.js index 9181371ae5..ea85da21a3 100644 --- a/test/parallel/test-whatwg-url-searchparams-foreach.js +++ b/test/parallel/test-whatwg-url-searchparams-foreach.js @@ -50,5 +50,9 @@ test(function() { const params = new URLSearchParams(); assert.throws(() => { params.forEach.call(undefined); - }, /^TypeError: Value of `this` is not a URLSearchParams$/); + }, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' + })); } diff --git a/test/parallel/test-whatwg-url-searchparams-get.js b/test/parallel/test-whatwg-url-searchparams-get.js index 5e81be4f32..cc34373528 100644 --- a/test/parallel/test-whatwg-url-searchparams-get.js +++ b/test/parallel/test-whatwg-url-searchparams-get.js @@ -38,10 +38,18 @@ test(function() { const params = new URLSearchParams(); assert.throws(() => { params.get.call(undefined); - }, /^TypeError: Value of `this` is not a URLSearchParams$/); + }, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' + })); assert.throws(() => { params.get(); - }, /^TypeError: "name" argument must be specified$/); + }, common.expectsError({ + code: 'ERR_MISSING_ARGS', + type: TypeError, + message: 'The "name" argument must be specified' + })); const obj = { toString() { throw new Error('toString'); }, diff --git a/test/parallel/test-whatwg-url-searchparams-getall.js b/test/parallel/test-whatwg-url-searchparams-getall.js index f80f45d542..9c060ecb6d 100644 --- a/test/parallel/test-whatwg-url-searchparams-getall.js +++ b/test/parallel/test-whatwg-url-searchparams-getall.js @@ -42,10 +42,18 @@ test(function() { const params = new URLSearchParams(); assert.throws(() => { params.getAll.call(undefined); - }, /^TypeError: Value of `this` is not a URLSearchParams$/); + }, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' + })); assert.throws(() => { params.getAll(); - }, /^TypeError: "name" argument must be specified$/); + }, common.expectsError({ + code: 'ERR_MISSING_ARGS', + type: TypeError, + message: 'The "name" argument must be specified' + })); const obj = { toString() { throw new Error('toString'); }, diff --git a/test/parallel/test-whatwg-url-searchparams-has.js b/test/parallel/test-whatwg-url-searchparams-has.js index f2696063b9..fac4364081 100644 --- a/test/parallel/test-whatwg-url-searchparams-has.js +++ b/test/parallel/test-whatwg-url-searchparams-has.js @@ -41,10 +41,18 @@ test(function() { const params = new URLSearchParams(); assert.throws(() => { params.has.call(undefined); - }, /^TypeError: Value of `this` is not a URLSearchParams$/); + }, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' + })); assert.throws(() => { params.has(); - }, /^TypeError: "name" argument must be specified$/); + }, common.expectsError({ + code: 'ERR_MISSING_ARGS', + type: TypeError, + message: 'The "name" argument must be specified' + })); const obj = { toString() { throw new Error('toString'); }, diff --git a/test/parallel/test-whatwg-url-searchparams-keys.js b/test/parallel/test-whatwg-url-searchparams-keys.js index 6942b152cd..cbf0766bc2 100644 --- a/test/parallel/test-whatwg-url-searchparams-keys.js +++ b/test/parallel/test-whatwg-url-searchparams-keys.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const URLSearchParams = require('url').URLSearchParams; @@ -29,7 +29,15 @@ assert.deepStrictEqual(keys.next(), { assert.throws(() => { keys.next.call(undefined); -}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/); +}, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParamsIterator' +})); assert.throws(() => { params.keys.call(undefined); -}, /^TypeError: Value of `this` is not a URLSearchParams$/); +}, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' +})); diff --git a/test/parallel/test-whatwg-url-searchparams-set.js b/test/parallel/test-whatwg-url-searchparams-set.js index acd62955d2..a630355b6e 100644 --- a/test/parallel/test-whatwg-url-searchparams-set.js +++ b/test/parallel/test-whatwg-url-searchparams-set.js @@ -39,10 +39,18 @@ test(function() { const params = new URLSearchParams(); assert.throws(() => { params.set.call(undefined); - }, /^TypeError: Value of `this` is not a URLSearchParams$/); + }, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' + })); assert.throws(() => { params.set('a'); - }, /^TypeError: "name" and "value" arguments must be specified$/); + }, common.expectsError({ + code: 'ERR_MISSING_ARGS', + type: TypeError, + message: 'The "name" and "value" arguments must be specified' + })); const obj = { toString() { throw new Error('toString'); }, diff --git a/test/parallel/test-whatwg-url-searchparams-stringifier.js b/test/parallel/test-whatwg-url-searchparams-stringifier.js index ac09979e02..327611bc68 100644 --- a/test/parallel/test-whatwg-url-searchparams-stringifier.js +++ b/test/parallel/test-whatwg-url-searchparams-stringifier.js @@ -127,5 +127,9 @@ test(function() { const params = new URLSearchParams(); assert.throws(() => { params.toString.call(undefined); - }, /^TypeError: Value of `this` is not a URLSearchParams$/); + }, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' + })); } diff --git a/test/parallel/test-whatwg-url-searchparams-values.js b/test/parallel/test-whatwg-url-searchparams-values.js index eb6fa05daf..96f1a0fddb 100644 --- a/test/parallel/test-whatwg-url-searchparams-values.js +++ b/test/parallel/test-whatwg-url-searchparams-values.js @@ -1,6 +1,6 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const URLSearchParams = require('url').URLSearchParams; @@ -29,7 +29,15 @@ assert.deepStrictEqual(values.next(), { assert.throws(() => { values.next.call(undefined); -}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/); +}, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParamsIterator' +})); assert.throws(() => { params.values.call(undefined); -}, /^TypeError: Value of `this` is not a URLSearchParams$/); +}, common.expectsError({ + code: 'ERR_INVALID_THIS', + type: TypeError, + message: 'Value of "this" must be of type URLSearchParams' +})); diff --git a/test/parallel/test-whatwg-url-searchparams.js b/test/parallel/test-whatwg-url-searchparams.js index c7acb7d909..5bb9cf407d 100644 --- a/test/parallel/test-whatwg-url-searchparams.js +++ b/test/parallel/test-whatwg-url-searchparams.js @@ -71,10 +71,15 @@ sp.forEach(function(val, key, obj) { sp.forEach(function() { assert.strictEqual(this, m); }, m); -assert.throws(() => sp.forEach(), - /^TypeError: "callback" argument must be a function$/); -assert.throws(() => sp.forEach(1), - /^TypeError: "callback" argument must be a function$/); + +{ + const callbackErr = common.expectsError({ + code: 'ERR_INVALID_CALLBACK', + type: TypeError + }); + assert.throws(() => sp.forEach(), callbackErr); + assert.throws(() => sp.forEach(1), callbackErr); +} m.search = '?a=a&b=b'; assert.strictEqual(sp.toString(), 'a=a&b=b'); |