0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/parallel/test-whatwg-url-searchparams-getall.js
Timothy Gu 61d6293033 url: improve URLSearchParams spec compliance
- Make URLSearchParams constructor spec-compliant
- Strip leading `?` in URL#search's setter
- Spec-compliant iterable interface
- More precise handling of update steps as mandated by the spec
- Add class strings to URLSearchParams objects and their prototype
- Make sure `this instanceof URLSearchParams` in methods

Also included are relevant tests from W3C's Web Platform Tests
(https://github.com/w3c/web-platform-tests/tree/master/url).

Fixes: https://github.com/nodejs/node/issues/9302
PR-URL: https://github.com/nodejs/node/pull/9484
Reviewed-By: James M Snell <jasnell@gmail.com>
2016-12-07 12:19:23 -05:00

44 lines
1.5 KiB
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const URL = require('url').URL;
const m = new URL('http://example.org');
let params = m.searchParams;
// Until we export URLSearchParams
const URLSearchParams = params.constructor;
let matches;
// getAll() basics
params = new URLSearchParams('a=b&c=d');
assert.deepStrictEqual(params.getAll('a'), ['b']);
assert.deepStrictEqual(params.getAll('c'), ['d']);
assert.deepStrictEqual(params.getAll('e'), []);
params = new URLSearchParams('a=b&c=d&a=e');
assert.deepStrictEqual(params.getAll('a'), ['b', 'e']);
params = new URLSearchParams('=b&c=d');
assert.deepStrictEqual(params.getAll(''), ['b']);
params = new URLSearchParams('a=&c=d&a=e');
assert.deepStrictEqual(params.getAll('a'), ['', 'e']);
// getAll() multiples
params = new URLSearchParams('a=1&a=2&a=3&a');
assert.strictEqual(true, params.has('a'),
'Search params object has name "a"');
matches = params.getAll('a');
assert(matches && matches.length == 4,
'Search params object has values for name "a"');
assert.deepStrictEqual(matches, ['1', '2', '3', ''],
'Search params object has expected name "a" values');
params.set('a', 'one');
assert.strictEqual(params.get('a'), 'one',
'Search params object has name "a" with value "one"');
matches = params.getAll('a');
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');