File
Blob: src/workerd/api/node/tests/querystring-nodejs-test.js
| 1 | // Copyright (c) 2017-2022 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | // Copyright Joyent and Node contributors. All rights reserved. MIT license. |
| 5 | import qs from 'node:querystring'; |
| 6 | import assert from 'node:assert'; |
| 7 | import { inspect } from 'node:util'; |
| 8 | |
| 9 | // Ref: https://github.com/nodejs/node/blob/4d6d7d644be4f10f90e5c9c66563736112fffbff/test/parallel/test-querystring.js |
| 10 | export const testQuerystring = { |
| 11 | async test() { |
| 12 | function createWithNoPrototype(properties) { |
| 13 | const noProto = { __proto__: null }; |
| 14 | properties.forEach((property) => { |
| 15 | noProto[property.key] = property.value; |
| 16 | }); |
| 17 | return noProto; |
| 18 | } |
| 19 | |
| 20 | // Folding block, commented to pass gjslint |
| 21 | // {{{ |
| 22 | // [ wonkyQS, canonicalQS, obj ] |
| 23 | const qsTestCases = [ |
| 24 | [ |
| 25 | '__proto__=1', |
| 26 | '__proto__=1', |
| 27 | createWithNoPrototype([{ key: '__proto__', value: '1' }]), |
| 28 | ], |
| 29 | [ |
| 30 | '__defineGetter__=asdf', |
| 31 | '__defineGetter__=asdf', |
| 32 | JSON.parse('{"__defineGetter__":"asdf"}'), |
| 33 | ], |
| 34 | [ |
| 35 | 'foo=918854443121279438895193', |
| 36 | 'foo=918854443121279438895193', |
| 37 | { foo: '918854443121279438895193' }, |
| 38 | ], |
| 39 | ['foo=bar', 'foo=bar', { foo: 'bar' }], |
| 40 | ['foo=bar&foo=quux', 'foo=bar&foo=quux', { foo: ['bar', 'quux'] }], |
| 41 | ['foo=1&bar=2', 'foo=1&bar=2', { foo: '1', bar: '2' }], |
| 42 | [ |
| 43 | 'my+weird+field=q1%212%22%27w%245%267%2Fz8%29%3F', |
| 44 | "my%20weird%20field=q1!2%22'w%245%267%2Fz8)%3F", |
| 45 | { 'my weird field': 'q1!2"\'w$5&7/z8)?' }, |
| 46 | ], |
| 47 | ['foo%3Dbaz=bar', 'foo%3Dbaz=bar', { 'foo=baz': 'bar' }], |
| 48 | ['foo=baz=bar', 'foo=baz%3Dbar', { foo: 'baz=bar' }], |
| 49 | [ |
| 50 | 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=', |
| 51 | 'str=foo&arr=1&arr=2&arr=3&somenull=&undef=', |
| 52 | { str: 'foo', arr: ['1', '2', '3'], somenull: '', undef: '' }, |
| 53 | ], |
| 54 | [' foo = bar ', '%20foo%20=%20bar%20', { ' foo ': ' bar ' }], |
| 55 | ['foo=%zx', 'foo=%25zx', { foo: '%zx' }], |
| 56 | ['foo=%EF%BF%BD', 'foo=%EF%BF%BD', { foo: '\ufffd' }], |
| 57 | // See: https://github.com/joyent/node/issues/1707 |
| 58 | [ |
| 59 | 'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz', |
| 60 | 'hasOwnProperty=x&toString=foo&valueOf=bar&__defineGetter__=baz', |
| 61 | { |
| 62 | hasOwnProperty: 'x', |
| 63 | toString: 'foo', |
| 64 | valueOf: 'bar', |
| 65 | __defineGetter__: 'baz', |
| 66 | }, |
| 67 | ], |
| 68 | // See: https://github.com/joyent/node/issues/3058 |
| 69 | ['foo&bar=baz', 'foo=&bar=baz', { foo: '', bar: 'baz' }], |
| 70 | ['a=b&c&d=e', 'a=b&c=&d=e', { a: 'b', c: '', d: 'e' }], |
| 71 | ['a=b&c=&d=e', 'a=b&c=&d=e', { a: 'b', c: '', d: 'e' }], |
| 72 | ['a=b&=c&d=e', 'a=b&=c&d=e', { a: 'b', '': 'c', d: 'e' }], |
| 73 | ['a=b&=&c=d', 'a=b&=&c=d', { a: 'b', '': '', c: 'd' }], |
| 74 | ['&&foo=bar&&', 'foo=bar', { foo: 'bar' }], |
| 75 | ['&', '', {}], |
| 76 | ['&&&&', '', {}], |
| 77 | ['&=&', '=', { '': '' }], |
| 78 | ['&=&=', '=&=', { '': ['', ''] }], |
| 79 | ['=', '=', { '': '' }], |
| 80 | ['+', '%20=', { ' ': '' }], |
| 81 | ['+=', '%20=', { ' ': '' }], |
| 82 | ['+&', '%20=', { ' ': '' }], |
| 83 | ['=+', '=%20', { '': ' ' }], |
| 84 | ['+=&', '%20=', { ' ': '' }], |
| 85 | ['a&&b', 'a=&b=', { a: '', b: '' }], |
| 86 | ['a=a&&b=b', 'a=a&b=b', { a: 'a', b: 'b' }], |
| 87 | ['&a', 'a=', { a: '' }], |
| 88 | ['&=', '=', { '': '' }], |
| 89 | ['a&a&', 'a=&a=', { a: ['', ''] }], |
| 90 | ['a&a&a&', 'a=&a=&a=', { a: ['', '', ''] }], |
| 91 | ['a&a&a&a&', 'a=&a=&a=&a=', { a: ['', '', '', ''] }], |
| 92 | ['a=&a=value&a=', 'a=&a=value&a=', { a: ['', 'value', ''] }], |
| 93 | ['foo+bar=baz+quux', 'foo%20bar=baz%20quux', { 'foo bar': 'baz quux' }], |
| 94 | ['+foo=+bar', '%20foo=%20bar', { ' foo': ' bar' }], |
| 95 | ['a+', 'a%20=', { 'a ': '' }], |
| 96 | ['=a+', '=a%20', { '': 'a ' }], |
| 97 | ['a+&', 'a%20=', { 'a ': '' }], |
| 98 | ['=a+&', '=a%20', { '': 'a ' }], |
| 99 | ['%20+', '%20%20=', { ' ': '' }], |
| 100 | ['=%20+', '=%20%20', { '': ' ' }], |
| 101 | ['%20+&', '%20%20=', { ' ': '' }], |
| 102 | ['=%20+&', '=%20%20', { '': ' ' }], |
| 103 | [null, '', {}], |
| 104 | [undefined, '', {}], |
| 105 | ]; |
| 106 | |
| 107 | // [ wonkyQS, canonicalQS, obj ] |
| 108 | const qsColonTestCases = [ |
| 109 | ['foo:bar', 'foo:bar', { foo: 'bar' }], |
| 110 | ['foo:bar;foo:quux', 'foo:bar;foo:quux', { foo: ['bar', 'quux'] }], |
| 111 | [ |
| 112 | 'foo:1&bar:2;baz:quux', |
| 113 | 'foo:1%26bar%3A2;baz:quux', |
| 114 | { foo: '1&bar:2', baz: 'quux' }, |
| 115 | ], |
| 116 | ['foo%3Abaz:bar', 'foo%3Abaz:bar', { 'foo:baz': 'bar' }], |
| 117 | ['foo:baz:bar', 'foo:baz%3Abar', { foo: 'baz:bar' }], |
| 118 | ]; |
| 119 | |
| 120 | // [wonkyObj, qs, canonicalObj] |
| 121 | function extendedFunction() {} |
| 122 | extendedFunction.prototype = { a: 'b' }; |
| 123 | const qsWeirdObjects = [ |
| 124 | [{ regexp: /./g }, 'regexp=', { regexp: '' }], |
| 125 | [{ regexp: new RegExp('.', 'g') }, 'regexp=', { regexp: '' }], |
| 126 | [{ fn: () => {} }, 'fn=', { fn: '' }], |
| 127 | [{ math: Math }, 'math=', { math: '' }], |
| 128 | [{ e: extendedFunction }, 'e=', { e: '' }], |
| 129 | [{ d: new Date() }, 'd=', { d: '' }], |
| 130 | [{ d: Date }, 'd=', { d: '' }], |
| 131 | [ |
| 132 | { f: new Boolean(false), t: new Boolean(true) }, |
| 133 | 'f=&t=', |
| 134 | { f: '', t: '' }, |
| 135 | ], |
| 136 | [{ f: false, t: true }, 'f=false&t=true', { f: 'false', t: 'true' }], |
| 137 | [{ n: null }, 'n=', { n: '' }], |
| 138 | [{ nan: NaN }, 'nan=', { nan: '' }], |
| 139 | [{ inf: Infinity }, 'inf=', { inf: '' }], |
| 140 | [{ a: [], b: [] }, '', {}], |
| 141 | [{ a: 1, b: [] }, 'a=1', { a: '1' }], |
| 142 | ]; |
| 143 | // }}} |
| 144 | |
| 145 | const qsNoMungeTestCases = [ |
| 146 | ['', {}], |
| 147 | ['foo=bar&foo=baz', { foo: ['bar', 'baz'] }], |
| 148 | ['blah=burp', { blah: 'burp' }], |
| 149 | ["a=!-._~'()*", { a: "!-._~'()*" }], |
| 150 | ['a=abcdefghijklmnopqrstuvwxyz', { a: 'abcdefghijklmnopqrstuvwxyz' }], |
| 151 | ['a=ABCDEFGHIJKLMNOPQRSTUVWXYZ', { a: 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' }], |
| 152 | ['a=0123456789', { a: '0123456789' }], |
| 153 | ['gragh=1&gragh=3&goo=2', { gragh: ['1', '3'], goo: '2' }], |
| 154 | [ |
| 155 | 'frappucino=muffin&goat%5B%5D=scone&pond=moose', |
| 156 | { frappucino: 'muffin', 'goat[]': 'scone', pond: 'moose' }, |
| 157 | ], |
| 158 | ['trololol=yes&lololo=no', { trololol: 'yes', lololo: 'no' }], |
| 159 | ]; |
| 160 | |
| 161 | const qsUnescapeTestCases = [ |
| 162 | [ |
| 163 | 'there is nothing to unescape here', |
| 164 | 'there is nothing to unescape here', |
| 165 | ], |
| 166 | [ |
| 167 | 'there%20are%20several%20spaces%20that%20need%20to%20be%20unescaped', |
| 168 | 'there are several spaces that need to be unescaped', |
| 169 | ], |
| 170 | [ |
| 171 | 'there%2Qare%0-fake%escaped values in%%%%this%9Hstring', |
| 172 | 'there%2Qare%0-fake%escaped values in%%%%this%9Hstring', |
| 173 | ], |
| 174 | [ |
| 175 | '%20%21%22%23%24%25%26%27%28%29%2A%2B%2C%2D%2E%2F%30%31%32%33%34%35%36%37', |
| 176 | ' !"#$%&\'()*+,-./01234567', |
| 177 | ], |
| 178 | ['%%2a', '%*'], |
| 179 | ['%2sf%2a', '%2sf*'], |
| 180 | ['%2%2af%2a', '%2*f*'], |
| 181 | ]; |
| 182 | |
| 183 | assert.strictEqual( |
| 184 | qs.parse('id=918854443121279438895193').id, |
| 185 | '918854443121279438895193' |
| 186 | ); |
| 187 | |
| 188 | function check(actual, expected, input) { |
| 189 | assert(!(actual instanceof Object)); |
| 190 | const actualKeys = Object.keys(actual).sort(); |
| 191 | const expectedKeys = Object.keys(expected).sort(); |
| 192 | let msg; |
| 193 | if (typeof input === 'string') { |
| 194 | msg = |
| 195 | `Input: ${inspect(input)}\n` + |
| 196 | `Actual keys: ${inspect(actualKeys)}\n` + |
| 197 | `Expected keys: ${inspect(expectedKeys)}`; |
| 198 | } |
| 199 | assert.deepStrictEqual(actualKeys, expectedKeys, msg); |
| 200 | expectedKeys.forEach((key) => { |
| 201 | if (typeof input === 'string') { |
| 202 | msg = |
| 203 | `Input: ${inspect(input)}\n` + |
| 204 | `Key: ${inspect(key)}\n` + |
| 205 | `Actual value: ${inspect(actual[key])}\n` + |
| 206 | `Expected value: ${inspect(expected[key])}`; |
| 207 | } else { |
| 208 | msg = undefined; |
| 209 | } |
| 210 | assert.deepStrictEqual(actual[key], expected[key], msg); |
| 211 | }); |
| 212 | } |
| 213 | |
| 214 | // Test that the canonical qs is parsed properly. |
| 215 | qsTestCases.forEach((testCase) => { |
| 216 | check(qs.parse(testCase[0]), testCase[2], testCase[0]); |
| 217 | }); |
| 218 | |
| 219 | // Test that the colon test cases can do the same |
| 220 | qsColonTestCases.forEach((testCase) => { |
| 221 | check(qs.parse(testCase[0], ';', ':'), testCase[2], testCase[0]); |
| 222 | }); |
| 223 | |
| 224 | // Test the weird objects, that they get parsed properly |
| 225 | qsWeirdObjects.forEach((testCase) => { |
| 226 | check(qs.parse(testCase[1]), testCase[2], testCase[1]); |
| 227 | }); |
| 228 | |
| 229 | qsNoMungeTestCases.forEach((testCase) => { |
| 230 | assert.deepStrictEqual(qs.stringify(testCase[1], '&', '='), testCase[0]); |
| 231 | }); |
| 232 | |
| 233 | // Test the nested qs-in-qs case |
| 234 | { |
| 235 | const f = qs.parse('a=b&q=x%3Dy%26y%3Dz'); |
| 236 | check( |
| 237 | f, |
| 238 | createWithNoPrototype([ |
| 239 | { key: 'a', value: 'b' }, |
| 240 | { key: 'q', value: 'x=y&y=z' }, |
| 241 | ]) |
| 242 | ); |
| 243 | |
| 244 | f.q = qs.parse(f.q); |
| 245 | const expectedInternal = createWithNoPrototype([ |
| 246 | { key: 'x', value: 'y' }, |
| 247 | { key: 'y', value: 'z' }, |
| 248 | ]); |
| 249 | check(f.q, expectedInternal); |
| 250 | } |
| 251 | |
| 252 | // nested in colon |
| 253 | { |
| 254 | const f = qs.parse('a:b;q:x%3Ay%3By%3Az', ';', ':'); |
| 255 | check( |
| 256 | f, |
| 257 | createWithNoPrototype([ |
| 258 | { key: 'a', value: 'b' }, |
| 259 | { key: 'q', value: 'x:y;y:z' }, |
| 260 | ]) |
| 261 | ); |
| 262 | f.q = qs.parse(f.q, ';', ':'); |
| 263 | const expectedInternal = createWithNoPrototype([ |
| 264 | { key: 'x', value: 'y' }, |
| 265 | { key: 'y', value: 'z' }, |
| 266 | ]); |
| 267 | check(f.q, expectedInternal); |
| 268 | } |
| 269 | |
| 270 | // Now test stringifying |
| 271 | |
| 272 | // basic |
| 273 | qsTestCases.forEach((testCase) => { |
| 274 | assert.strictEqual(qs.stringify(testCase[2]), testCase[1]); |
| 275 | }); |
| 276 | |
| 277 | qsColonTestCases.forEach((testCase) => { |
| 278 | assert.strictEqual(qs.stringify(testCase[2], ';', ':'), testCase[1]); |
| 279 | }); |
| 280 | |
| 281 | qsWeirdObjects.forEach((testCase) => { |
| 282 | assert.strictEqual(qs.stringify(testCase[0]), testCase[1]); |
| 283 | }); |
| 284 | |
| 285 | // BigInt values |
| 286 | |
| 287 | assert.strictEqual( |
| 288 | qs.stringify({ foo: 2n ** 1023n }), |
| 289 | 'foo=' + 2n ** 1023n |
| 290 | ); |
| 291 | assert.strictEqual(qs.stringify([0n, 1n, 2n]), '0=0&1=1&2=2'); |
| 292 | |
| 293 | assert.strictEqual( |
| 294 | qs.stringify({ foo: 2n ** 1023n }, null, null, { |
| 295 | encodeURIComponent: (c) => c, |
| 296 | }), |
| 297 | 'foo=' + 2n ** 1023n |
| 298 | ); |
| 299 | assert.strictEqual( |
| 300 | qs.stringify([0n, 1n, 2n], null, null, { encodeURIComponent: (c) => c }), |
| 301 | '0=0&1=1&2=2' |
| 302 | ); |
| 303 | |
| 304 | // Invalid surrogate pair throws URIError |
| 305 | assert.throws(() => qs.stringify({ foo: '\udc00' }), { |
| 306 | code: 'ERR_INVALID_URI', |
| 307 | name: 'URIError', |
| 308 | message: 'URI malformed', |
| 309 | }); |
| 310 | |
| 311 | // Coerce numbers to string |
| 312 | assert.strictEqual(qs.stringify({ foo: 0 }), 'foo=0'); |
| 313 | assert.strictEqual(qs.stringify({ foo: -0 }), 'foo=0'); |
| 314 | assert.strictEqual(qs.stringify({ foo: 3 }), 'foo=3'); |
| 315 | assert.strictEqual(qs.stringify({ foo: -72.42 }), 'foo=-72.42'); |
| 316 | assert.strictEqual(qs.stringify({ foo: NaN }), 'foo='); |
| 317 | assert.strictEqual(qs.stringify({ foo: 1e21 }), 'foo=1e%2B21'); |
| 318 | assert.strictEqual(qs.stringify({ foo: Infinity }), 'foo='); |
| 319 | |
| 320 | // nested |
| 321 | { |
| 322 | const f = qs.stringify({ |
| 323 | a: 'b', |
| 324 | q: qs.stringify({ |
| 325 | x: 'y', |
| 326 | y: 'z', |
| 327 | }), |
| 328 | }); |
| 329 | assert.strictEqual(f, 'a=b&q=x%3Dy%26y%3Dz'); |
| 330 | } |
| 331 | |
| 332 | qs.parse(undefined); // Should not throw. |
| 333 | |
| 334 | // nested in colon |
| 335 | { |
| 336 | const f = qs.stringify( |
| 337 | { |
| 338 | a: 'b', |
| 339 | q: qs.stringify( |
| 340 | { |
| 341 | x: 'y', |
| 342 | y: 'z', |
| 343 | }, |
| 344 | ';', |
| 345 | ':' |
| 346 | ), |
| 347 | }, |
| 348 | ';', |
| 349 | ':' |
| 350 | ); |
| 351 | assert.strictEqual(f, 'a:b;q:x%3Ay%3By%3Az'); |
| 352 | } |
| 353 | |
| 354 | // empty string |
| 355 | assert.strictEqual(qs.stringify(), ''); |
| 356 | assert.strictEqual(qs.stringify(0), ''); |
| 357 | assert.strictEqual(qs.stringify([]), ''); |
| 358 | assert.strictEqual(qs.stringify(null), ''); |
| 359 | assert.strictEqual(qs.stringify(true), ''); |
| 360 | |
| 361 | check(qs.parse(), {}); |
| 362 | |
| 363 | // empty sep |
| 364 | check(qs.parse('a', []), { a: '' }); |
| 365 | |
| 366 | // empty eq |
| 367 | check(qs.parse('a', null, []), { '': 'a' }); |
| 368 | |
| 369 | // Test limiting |
| 370 | assert.strictEqual( |
| 371 | Object.keys(qs.parse('a=1&b=1&c=1', null, null, { maxKeys: 1 })).length, |
| 372 | 1 |
| 373 | ); |
| 374 | |
| 375 | // Test limiting with a case that starts from `&` |
| 376 | assert.strictEqual( |
| 377 | Object.keys(qs.parse('&a', null, null, { maxKeys: 1 })).length, |
| 378 | 0 |
| 379 | ); |
| 380 | |
| 381 | // Test removing limit |
| 382 | { |
| 383 | function testUnlimitedKeys() { |
| 384 | const query = {}; |
| 385 | |
| 386 | for (let i = 0; i < 2000; i++) query[i] = i; |
| 387 | |
| 388 | const url = qs.stringify(query); |
| 389 | |
| 390 | assert.strictEqual( |
| 391 | Object.keys(qs.parse(url, null, null, { maxKeys: 0 })).length, |
| 392 | 2000 |
| 393 | ); |
| 394 | } |
| 395 | |
| 396 | testUnlimitedKeys(); |
| 397 | } |
| 398 | |
| 399 | { |
| 400 | const b = qs.unescapeBuffer( |
| 401 | '%d3%f2Ug%1f6v%24%5e%98%cb' + '%0d%ac%a2%2f%9d%eb%d8%a2%e6' |
| 402 | ); |
| 403 | // <Buffer d3 f2 55 67 1f 36 76 24 5e 98 cb 0d ac a2 2f 9d eb d8 a2 e6> |
| 404 | assert.strictEqual(b[0], 0xd3); |
| 405 | assert.strictEqual(b[1], 0xf2); |
| 406 | assert.strictEqual(b[2], 0x55); |
| 407 | assert.strictEqual(b[3], 0x67); |
| 408 | assert.strictEqual(b[4], 0x1f); |
| 409 | assert.strictEqual(b[5], 0x36); |
| 410 | assert.strictEqual(b[6], 0x76); |
| 411 | assert.strictEqual(b[7], 0x24); |
| 412 | assert.strictEqual(b[8], 0x5e); |
| 413 | assert.strictEqual(b[9], 0x98); |
| 414 | assert.strictEqual(b[10], 0xcb); |
| 415 | assert.strictEqual(b[11], 0x0d); |
| 416 | assert.strictEqual(b[12], 0xac); |
| 417 | assert.strictEqual(b[13], 0xa2); |
| 418 | assert.strictEqual(b[14], 0x2f); |
| 419 | assert.strictEqual(b[15], 0x9d); |
| 420 | assert.strictEqual(b[16], 0xeb); |
| 421 | assert.strictEqual(b[17], 0xd8); |
| 422 | assert.strictEqual(b[18], 0xa2); |
| 423 | assert.strictEqual(b[19], 0xe6); |
| 424 | } |
| 425 | |
| 426 | assert.strictEqual(qs.unescapeBuffer('a+b', true).toString(), 'a b'); |
| 427 | assert.strictEqual(qs.unescapeBuffer('a+b').toString(), 'a+b'); |
| 428 | assert.strictEqual(qs.unescapeBuffer('a%').toString(), 'a%'); |
| 429 | assert.strictEqual(qs.unescapeBuffer('a%2').toString(), 'a%2'); |
| 430 | assert.strictEqual(qs.unescapeBuffer('a%20').toString(), 'a '); |
| 431 | assert.strictEqual(qs.unescapeBuffer('a%2g').toString(), 'a%2g'); |
| 432 | assert.strictEqual(qs.unescapeBuffer('a%%').toString(), 'a%%'); |
| 433 | |
| 434 | // Test invalid encoded string |
| 435 | check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' }); |
| 436 | |
| 437 | // Test custom decode |
| 438 | { |
| 439 | function demoDecode(str) { |
| 440 | return str + str; |
| 441 | } |
| 442 | |
| 443 | check( |
| 444 | qs.parse('a=a&b=b&c=c', null, null, { decodeURIComponent: demoDecode }), |
| 445 | { aa: 'aa', bb: 'bb', cc: 'cc' } |
| 446 | ); |
| 447 | check( |
| 448 | qs.parse('a=a&b=b&c=c', null, '==', { |
| 449 | decodeURIComponent: (str) => str, |
| 450 | }), |
| 451 | { 'a=a': '', 'b=b': '', 'c=c': '' } |
| 452 | ); |
| 453 | } |
| 454 | |
| 455 | // Test QueryString.unescape |
| 456 | { |
| 457 | function errDecode(str) { |
| 458 | throw new Error('To jump to the catch scope'); |
| 459 | } |
| 460 | |
| 461 | check(qs.parse('a=a', null, null, { decodeURIComponent: errDecode }), { |
| 462 | a: 'a', |
| 463 | }); |
| 464 | } |
| 465 | |
| 466 | // Test custom encode |
| 467 | { |
| 468 | function demoEncode(str) { |
| 469 | return str[0]; |
| 470 | } |
| 471 | |
| 472 | const obj = { aa: 'aa', bb: 'bb', cc: 'cc' }; |
| 473 | assert.strictEqual( |
| 474 | qs.stringify(obj, null, null, { encodeURIComponent: demoEncode }), |
| 475 | 'a=a&b=b&c=c' |
| 476 | ); |
| 477 | } |
| 478 | |
| 479 | // Test custom encode for different types |
| 480 | { |
| 481 | const obj = { |
| 482 | number: 1, |
| 483 | bigint: 2n, |
| 484 | true: true, |
| 485 | false: false, |
| 486 | object: {}, |
| 487 | }; |
| 488 | assert.strictEqual( |
| 489 | qs.stringify(obj, null, null, { encodeURIComponent: (v) => v }), |
| 490 | 'number=1&bigint=2&true=true&false=false&object=' |
| 491 | ); |
| 492 | } |
| 493 | |
| 494 | // Test QueryString.unescapeBuffer |
| 495 | qsUnescapeTestCases.forEach((testCase) => { |
| 496 | assert.strictEqual(qs.unescape(testCase[0]), testCase[1]); |
| 497 | assert.strictEqual( |
| 498 | qs.unescapeBuffer(testCase[0]).toString(), |
| 499 | testCase[1] |
| 500 | ); |
| 501 | }); |
| 502 | |
| 503 | // Test separator and "equals" parsing order |
| 504 | check(qs.parse('foo&bar', '&', '&'), { foo: '', bar: '' }); |
| 505 | }, |
| 506 | }; |
| 507 | |
| 508 | // Ref: https://github.com/nodejs/node/blob/4d6d7d644be4f10f90e5c9c66563736112fffbff/test/parallel/test-querystring-escape.js |
| 509 | export const testQueryStringEscape = { |
| 510 | async test() { |
| 511 | assert.strictEqual(qs.escape(5), '5'); |
| 512 | assert.strictEqual(qs.escape('test'), 'test'); |
| 513 | assert.strictEqual(qs.escape({}), '%5Bobject%20Object%5D'); |
| 514 | assert.strictEqual(qs.escape([5, 10]), '5%2C10'); |
| 515 | assert.strictEqual(qs.escape('Ŋōđĕ'), '%C5%8A%C5%8D%C4%91%C4%95'); |
| 516 | assert.strictEqual(qs.escape('testŊōđĕ'), 'test%C5%8A%C5%8D%C4%91%C4%95'); |
| 517 | assert.strictEqual( |
| 518 | qs.escape(`${String.fromCharCode(0xd800 + 1)}test`), |
| 519 | '%F0%90%91%B4est' |
| 520 | ); |
| 521 | |
| 522 | assert.throws(() => qs.escape(String.fromCharCode(0xd800 + 1)), { |
| 523 | code: 'ERR_INVALID_URI', |
| 524 | name: 'URIError', |
| 525 | message: 'URI malformed', |
| 526 | }); |
| 527 | |
| 528 | // Using toString for objects |
| 529 | assert.strictEqual( |
| 530 | qs.escape({ test: 5, toString: () => 'test', valueOf: () => 10 }), |
| 531 | 'test' |
| 532 | ); |
| 533 | |
| 534 | // `toString` is not callable, must throw an error. |
| 535 | // Error message will vary between different JavaScript engines, so only check |
| 536 | // that it is a `TypeError`. |
| 537 | assert.throws(() => qs.escape({ toString: 5 }), TypeError); |
| 538 | |
| 539 | // Should use valueOf instead of non-callable toString. |
| 540 | assert.strictEqual( |
| 541 | qs.escape({ toString: 5, valueOf: () => 'test' }), |
| 542 | 'test' |
| 543 | ); |
| 544 | |
| 545 | // Error message will vary between different JavaScript engines, so only check |
| 546 | // that it is a `TypeError`. |
| 547 | assert.throws(() => qs.escape(Symbol('test')), TypeError); |
| 548 | }, |
| 549 | }; |
| 550 | |
| 551 | // Ref: https://github.com/nodejs/node/blob/4d6d7d644be4f10f90e5c9c66563736112fffbff/test/parallel/test-querystring-maxKeys-non-finite.js |
| 552 | export const maxKeysNonFinite = { |
| 553 | async test() { |
| 554 | // Taken from express-js/body-parser |
| 555 | // https://github.com/expressjs/body-parser/blob/ed25264fb494cf0c8bc992b8257092cd4f694d5e/test/urlencoded.js#L636-L651 |
| 556 | function createManyParams(count) { |
| 557 | let str = ''; |
| 558 | |
| 559 | if (count === 0) { |
| 560 | return str; |
| 561 | } |
| 562 | |
| 563 | str += '0=0'; |
| 564 | |
| 565 | for (let i = 1; i < count; i++) { |
| 566 | const n = i.toString(36); |
| 567 | str += `&${n}=${n}`; |
| 568 | } |
| 569 | |
| 570 | return str; |
| 571 | } |
| 572 | |
| 573 | const count = 10000; |
| 574 | const originalMaxLength = 1000; |
| 575 | const params = createManyParams(count); |
| 576 | |
| 577 | // thealphanerd |
| 578 | // 27def4f introduced a change to parse that would cause Infinity |
| 579 | // to be passed to String.prototype.split as an argument for limit |
| 580 | // In this instance split will always return an empty array |
| 581 | // this test confirms that the output of parse is the expected length |
| 582 | // when passed Infinity as the argument for maxKeys |
| 583 | const resultInfinity = qs.parse(params, undefined, undefined, { |
| 584 | maxKeys: Infinity, |
| 585 | }); |
| 586 | const resultNaN = qs.parse(params, undefined, undefined, { |
| 587 | maxKeys: NaN, |
| 588 | }); |
| 589 | const resultInfinityString = qs.parse(params, undefined, undefined, { |
| 590 | maxKeys: 'Infinity', |
| 591 | }); |
| 592 | const resultNaNString = qs.parse(params, undefined, undefined, { |
| 593 | maxKeys: 'NaN', |
| 594 | }); |
| 595 | |
| 596 | // Non Finite maxKeys should return the length of input |
| 597 | assert.strictEqual(Object.keys(resultInfinity).length, count); |
| 598 | assert.strictEqual(Object.keys(resultNaN).length, count); |
| 599 | // Strings maxKeys should return the maxLength |
| 600 | // defined by parses internals |
| 601 | assert.strictEqual( |
| 602 | Object.keys(resultInfinityString).length, |
| 603 | originalMaxLength |
| 604 | ); |
| 605 | assert.strictEqual(Object.keys(resultNaNString).length, originalMaxLength); |
| 606 | }, |
| 607 | }; |
| 608 | |
| 609 | // Ref: https://github.com/nodejs/node/blob/4d6d7d644be4f10f90e5c9c66563736112fffbff/test/parallel/test-querystring-multichar-separator.js |
| 610 | export const multiCharSeparator = { |
| 611 | async test() { |
| 612 | function check(actual, expected) { |
| 613 | assert(!(actual instanceof Object)); |
| 614 | assert.deepStrictEqual( |
| 615 | Object.keys(actual).sort(), |
| 616 | Object.keys(expected).sort() |
| 617 | ); |
| 618 | Object.keys(expected).forEach(function (key) { |
| 619 | assert.deepStrictEqual(actual[key], expected[key]); |
| 620 | }); |
| 621 | } |
| 622 | |
| 623 | check(qs.parse('foo=>bar&&bar=>baz', '&&', '=>'), { |
| 624 | foo: 'bar', |
| 625 | bar: 'baz', |
| 626 | }); |
| 627 | |
| 628 | check( |
| 629 | qs.stringify({ foo: 'bar', bar: 'baz' }, '&&', '=>'), |
| 630 | 'foo=>bar&&bar=>baz' |
| 631 | ); |
| 632 | |
| 633 | check(qs.parse('foo==>bar, bar==>baz', ', ', '==>'), { |
| 634 | foo: 'bar', |
| 635 | bar: 'baz', |
| 636 | }); |
| 637 | |
| 638 | check( |
| 639 | qs.stringify({ foo: 'bar', bar: 'baz' }, ', ', '==>'), |
| 640 | 'foo==>bar, bar==>baz' |
| 641 | ); |
| 642 | }, |
| 643 | }; |