node/test/parallel/test-whatwg-url-searchparams-get.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

36 lines
1.3 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;
// Get basics
params = new URLSearchParams('a=b&c=d');
assert.strictEqual(params.get('a'), 'b');
assert.strictEqual(params.get('c'), 'd');
assert.strictEqual(params.get('e'), null);
params = new URLSearchParams('a=b&c=d&a=e');
assert.strictEqual(params.get('a'), 'b');
params = new URLSearchParams('=b&c=d');
assert.strictEqual(params.get(''), 'b');
params = new URLSearchParams('a=&c=d&a=e');
assert.strictEqual(params.get('a'), '');
// More get() basics
params = new URLSearchParams('first=second&third&&');
assert.notEqual(params, null, 'constructor returned non-null value.');
assert.strictEqual(true, params.has('first'),
'Search params object has name "first"');
assert.strictEqual(params.get('first'), 'second',
'Search params object has name "first" with value "second"');
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.');