• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Tests below are not from WPT.
4
5require('../common');
6const assert = require('assert');
7const fixtures = require('../common/fixtures');
8
9const serialized = 'a=a&a=1&a=true&a=undefined&a=null&a=%EF%BF%BD' +
10                   '&a=%EF%BF%BD&a=%F0%9F%98%80&a=%EF%BF%BD%EF%BF%BD' +
11                   '&a=%5Bobject+Object%5D';
12const values = ['a', 1, true, undefined, null, '\uD83D', '\uDE00',
13                '\uD83D\uDE00', '\uDE00\uD83D', {}];
14const normalizedValues = ['a', '1', 'true', 'undefined', 'null', '\uFFFD',
15                          '\uFFFD', '\uD83D\uDE00', '\uFFFD\uFFFD',
16                          '[object Object]'];
17
18const m = new URL('http://example.org');
19const sp = m.searchParams;
20
21assert(sp);
22assert.strictEqual(sp.toString(), '');
23assert.strictEqual(m.search, '');
24
25assert(!sp.has('a'));
26values.forEach((i) => sp.set('a', i));
27assert(sp.has('a'));
28assert.strictEqual(sp.get('a'), '[object Object]');
29sp.delete('a');
30assert(!sp.has('a'));
31
32m.search = '';
33assert.strictEqual(sp.toString(), '');
34
35values.forEach((i) => sp.append('a', i));
36assert(sp.has('a'));
37assert.strictEqual(sp.getAll('a').length, values.length);
38assert.strictEqual(sp.get('a'), 'a');
39
40assert.strictEqual(sp.toString(), serialized);
41
42assert.strictEqual(m.search, `?${serialized}`);
43
44assert.strictEqual(sp[Symbol.iterator], sp.entries);
45
46let key, val;
47let n = 0;
48for ([key, val] of sp) {
49  assert.strictEqual(key, 'a', n);
50  assert.strictEqual(val, normalizedValues[n], n);
51  n++;
52}
53n = 0;
54for (key of sp.keys()) {
55  assert.strictEqual(key, 'a', n);
56  n++;
57}
58n = 0;
59for (val of sp.values()) {
60  assert.strictEqual(val, normalizedValues[n], n);
61  n++;
62}
63n = 0;
64sp.forEach(function(val, key, obj) {
65  assert.strictEqual(this, undefined, n);
66  assert.strictEqual(key, 'a', n);
67  assert.strictEqual(val, normalizedValues[n], n);
68  assert.strictEqual(obj, sp, n);
69  n++;
70});
71sp.forEach(function() {
72  assert.strictEqual(this, m);
73}, m);
74
75{
76  const callbackErr = {
77    code: 'ERR_INVALID_CALLBACK',
78    name: 'TypeError'
79  };
80  assert.throws(() => sp.forEach(), callbackErr);
81  assert.throws(() => sp.forEach(1), callbackErr);
82}
83
84m.search = '?a=a&b=b';
85assert.strictEqual(sp.toString(), 'a=a&b=b');
86
87const tests = require(fixtures.path('url-searchparams.js'));
88
89for (const [input, expected, parsed] of tests) {
90  if (input[0] !== '?') {
91    const sp = new URLSearchParams(input);
92    assert.strictEqual(String(sp), expected);
93    assert.deepStrictEqual(Array.from(sp), parsed);
94
95    m.search = input;
96    assert.strictEqual(String(m.searchParams), expected);
97    assert.deepStrictEqual(Array.from(m.searchParams), parsed);
98  }
99
100  {
101    const sp = new URLSearchParams(`?${input}`);
102    assert.strictEqual(String(sp), expected);
103    assert.deepStrictEqual(Array.from(sp), parsed);
104
105    m.search = `?${input}`;
106    assert.strictEqual(String(m.searchParams), expected);
107    assert.deepStrictEqual(Array.from(m.searchParams), parsed);
108  }
109}
110