Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: increase coverage for URL.searchParams #10952

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-append.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
33 changes: 33 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-entries.js
Original file line number Diff line number Diff line change
@@ -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
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may also test calling entries.next again (to simulate overread).

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$/);
6 changes: 5 additions & 1 deletion test/parallel/test-whatwg-url-searchparams-foreach.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-get.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-getall.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-has.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
34 changes: 34 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-keys.js
Original file line number Diff line number Diff line change
@@ -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$/);
7 changes: 7 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
3 changes: 3 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-stringifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -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$/);
34 changes: 34 additions & 0 deletions test/parallel/test-whatwg-url-searchparams-values.js
Original file line number Diff line number Diff line change
@@ -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$/);