From fcd08b8a1e4ff2e564a8b02c8b533fb495db55ff Mon Sep 17 00:00:00 2001 From: abouthiroppy Date: Mon, 23 Jan 2017 00:01:36 +0900 Subject: [PATCH] test: add tests for searchParams PR-URL: https://github.com/nodejs/node/pull/10952 Reviewed-By: Colin Ihrig Reviewed-By: Timothy Gu Reviewed-By: James M Snell Reviewed-By: Joyee Cheung --- .../test-whatwg-url-searchparams-append.js | 7 ++++ .../test-whatwg-url-searchparams-delete.js | 7 ++++ .../test-whatwg-url-searchparams-entries.js | 33 ++++++++++++++++++ .../test-whatwg-url-searchparams-foreach.js | 6 +++- .../test-whatwg-url-searchparams-get.js | 7 ++++ .../test-whatwg-url-searchparams-getall.js | 7 ++++ .../test-whatwg-url-searchparams-has.js | 7 ++++ .../test-whatwg-url-searchparams-keys.js | 34 +++++++++++++++++++ .../test-whatwg-url-searchparams-set.js | 7 ++++ ...est-whatwg-url-searchparams-stringifier.js | 3 ++ .../test-whatwg-url-searchparams-values.js | 34 +++++++++++++++++++ 11 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-whatwg-url-searchparams-entries.js create mode 100644 test/parallel/test-whatwg-url-searchparams-keys.js create mode 100644 test/parallel/test-whatwg-url-searchparams-values.js diff --git a/test/parallel/test-whatwg-url-searchparams-append.js b/test/parallel/test-whatwg-url-searchparams-append.js index 5ef92252be5d9e..44f49d20b09526 100644 --- a/test/parallel/test-whatwg-url-searchparams-append.js +++ b/test/parallel/test-whatwg-url-searchparams-append.js @@ -46,3 +46,10 @@ assert.strictEqual(params.get('third'), '', params.append('first', 10); assert.strictEqual(params.get('first'), '1', 'Search params object has name "first" with value "1"'); + +assert.throws(() => { + params.append.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); +assert.throws(() => { + params.set('a'); +}, /^TypeError: "name" and "value" arguments must be specified$/); diff --git a/test/parallel/test-whatwg-url-searchparams-delete.js b/test/parallel/test-whatwg-url-searchparams-delete.js index 71bb7487a75087..f39917e8e37712 100644 --- a/test/parallel/test-whatwg-url-searchparams-delete.js +++ b/test/parallel/test-whatwg-url-searchparams-delete.js @@ -41,6 +41,13 @@ params.delete('first'); assert.strictEqual(false, params.has('first'), 'Search params object has no "first" name'); +assert.throws(() => { + params.delete.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); +assert.throws(() => { + params.delete(); +}, /^TypeError: "name" argument must be specified$/); + // https://github.com/nodejs/node/issues/10480 // Emptying searchParams should correctly update url's query { diff --git a/test/parallel/test-whatwg-url-searchparams-entries.js b/test/parallel/test-whatwg-url-searchparams-entries.js new file mode 100644 index 00000000000000..97f60ceb17fa29 --- /dev/null +++ b/test/parallel/test-whatwg-url-searchparams-entries.js @@ -0,0 +1,33 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const URLSearchParams = require('url').URLSearchParams; + +const params = new URLSearchParams('a=b&c=d'); +const entries = params.entries(); +assert.strictEqual(typeof entries[Symbol.iterator], 'function'); +assert.strictEqual(entries[Symbol.iterator](), entries); +assert.deepStrictEqual(entries.next(), { + value: ['a', 'b'], + done: false +}); +assert.deepStrictEqual(entries.next(), { + value: ['c', 'd'], + done: false +}); +assert.deepStrictEqual(entries.next(), { + value: undefined, + done: true +}); +assert.deepStrictEqual(entries.next(), { + value: undefined, + done: true +}); + +assert.throws(() => { + entries.next.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/); +assert.throws(() => { + params.entries.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); diff --git a/test/parallel/test-whatwg-url-searchparams-foreach.js b/test/parallel/test-whatwg-url-searchparams-foreach.js index 7e6b6222a8f101..6adbb5789b0c11 100644 --- a/test/parallel/test-whatwg-url-searchparams-foreach.js +++ b/test/parallel/test-whatwg-url-searchparams-foreach.js @@ -12,7 +12,7 @@ let a, b, i; const params = new URLSearchParams('a=1&b=2&c=3'); const keys = []; const values = []; -params.forEach(function(value, key) { +params.forEach((value, key) => { keys.push(key); values.push(value); }); @@ -37,3 +37,7 @@ b = a.searchParams; for (i of b) { common.fail('should not be reached'); } + +assert.throws(() => { + params.forEach.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); diff --git a/test/parallel/test-whatwg-url-searchparams-get.js b/test/parallel/test-whatwg-url-searchparams-get.js index 7ddfb67e8b8a7b..95c3f47df5eabb 100644 --- a/test/parallel/test-whatwg-url-searchparams-get.js +++ b/test/parallel/test-whatwg-url-searchparams-get.js @@ -29,3 +29,10 @@ assert.strictEqual(params.get('third'), '', 'Search params object has name "third" with empty value.'); assert.strictEqual(params.get('fourth'), null, 'Search params object has no "fourth" name and value.'); + +assert.throws(() => { + params.get.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); +assert.throws(() => { + params.get(); +}, /^TypeError: "name" argument must be specified$/); diff --git a/test/parallel/test-whatwg-url-searchparams-getall.js b/test/parallel/test-whatwg-url-searchparams-getall.js index 34805164ab1b38..52f581c0158a90 100644 --- a/test/parallel/test-whatwg-url-searchparams-getall.js +++ b/test/parallel/test-whatwg-url-searchparams-getall.js @@ -36,3 +36,10 @@ assert(matches && matches.length == 1, 'Search params object has values for name "a"'); assert.deepStrictEqual(matches, ['one'], 'Search params object has expected name "a" values'); + +assert.throws(() => { + params.getAll.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); +assert.throws(() => { + params.getAll(); +}, /^TypeError: "name" argument must be specified$/); diff --git a/test/parallel/test-whatwg-url-searchparams-has.js b/test/parallel/test-whatwg-url-searchparams-has.js index cd8901da2398b7..bab34515d99ac2 100644 --- a/test/parallel/test-whatwg-url-searchparams-has.js +++ b/test/parallel/test-whatwg-url-searchparams-has.js @@ -33,3 +33,10 @@ assert.strictEqual(false, params.has('d'), params.delete('first'); assert.strictEqual(false, params.has('first'), 'Search params object has no name "first"'); + +assert.throws(() => { + params.has.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); +assert.throws(() => { + params.has(); +}, /^TypeError: "name" argument must be specified$/); diff --git a/test/parallel/test-whatwg-url-searchparams-keys.js b/test/parallel/test-whatwg-url-searchparams-keys.js new file mode 100644 index 00000000000000..d538422654065c --- /dev/null +++ b/test/parallel/test-whatwg-url-searchparams-keys.js @@ -0,0 +1,34 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const URLSearchParams = require('url').URLSearchParams; + +const params = new URLSearchParams('a=b&c=d'); +const keys = params.keys(); + +assert.strictEqual(typeof keys[Symbol.iterator], 'function'); +assert.strictEqual(keys[Symbol.iterator](), keys); +assert.deepStrictEqual(keys.next(), { + value: 'a', + done: false +}); +assert.deepStrictEqual(keys.next(), { + value: 'c', + done: false +}); +assert.deepStrictEqual(keys.next(), { + value: undefined, + done: true +}); +assert.deepStrictEqual(keys.next(), { + value: undefined, + done: true +}); + +assert.throws(() => { + keys.next.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/); +assert.throws(() => { + params.keys.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); diff --git a/test/parallel/test-whatwg-url-searchparams-set.js b/test/parallel/test-whatwg-url-searchparams-set.js index 5ab2bbe8d8ac39..c818cc9f71af48 100644 --- a/test/parallel/test-whatwg-url-searchparams-set.js +++ b/test/parallel/test-whatwg-url-searchparams-set.js @@ -32,3 +32,10 @@ assert.strictEqual(true, params.has('a'), 'Search params object has name "a"'); assert.strictEqual(params.get('a'), '4', 'Search params object has name "a" with value "4"'); + +assert.throws(() => { + params.set.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); +assert.throws(() => { + params.set('a'); +}, /^TypeError: "name" and "value" arguments must be specified$/); diff --git a/test/parallel/test-whatwg-url-searchparams-stringifier.js b/test/parallel/test-whatwg-url-searchparams-stringifier.js index c591425461a9d1..ba3f42683b46e6 100644 --- a/test/parallel/test-whatwg-url-searchparams-stringifier.js +++ b/test/parallel/test-whatwg-url-searchparams-stringifier.js @@ -110,3 +110,6 @@ assert.strictEqual(params + '', 'a%F0%9F%92%A9b=c'); // The lone '=' _does_ survive the roundtrip. params = new URLSearchParams('a=&a=b'); assert.strictEqual(params.toString(), 'a=&a=b'); +assert.throws(() => { + params.toString.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/); diff --git a/test/parallel/test-whatwg-url-searchparams-values.js b/test/parallel/test-whatwg-url-searchparams-values.js new file mode 100644 index 00000000000000..36aa1da9f057bb --- /dev/null +++ b/test/parallel/test-whatwg-url-searchparams-values.js @@ -0,0 +1,34 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); +const URLSearchParams = require('url').URLSearchParams; + +const params = new URLSearchParams('a=b&c=d'); +const values = params.values(); + +assert.strictEqual(typeof values[Symbol.iterator], 'function'); +assert.strictEqual(values[Symbol.iterator](), values); +assert.deepStrictEqual(values.next(), { + value: 'b', + done: false +}); +assert.deepStrictEqual(values.next(), { + value: 'd', + done: false +}); +assert.deepStrictEqual(values.next(), { + value: undefined, + done: true +}); +assert.deepStrictEqual(values.next(), { + value: undefined, + done: true +}); + +assert.throws(() => { + values.next.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParamsIterator$/); +assert.throws(() => { + params.values.call(undefined); +}, /^TypeError: Value of `this` is not a URLSearchParams$/);