-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
QueryStringTest.php
520 lines (487 loc) · 17 KB
/
QueryStringTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
/**
* League.Uri (https://uri.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\Uri;
use ArrayIterator;
use League\Uri\Components\Fragment;
use League\Uri\Exceptions\SyntaxError;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Stringable;
use function date_create;
use const PHP_QUERY_RFC1738;
use const PHP_QUERY_RFC3986;
final class QueryStringTest extends TestCase
{
public function testEncodingThrowsExceptionWithQueryParser(): void
{
$this->expectException(SyntaxError::class);
QueryString::parse('foo=bar', '&', 42);
}
public function testSyntaxErrorThrowsExceptionWithQueryParser(): void
{
$this->expectException(SyntaxError::class);
QueryString::parse("foo=bar\0");
}
public function testSyntaxErrorThrowsExceptionWithQueryParserAndAnEmptySeparator(): void
{
$this->expectException(SyntaxError::class);
QueryString::parse('foo=bar', ''); /* @phpstan-ignore-line */
}
public function testEncodingThrowsExceptionWithQueryBuilder(): void
{
$this->expectException(SyntaxError::class);
QueryString::build([['foo', 'bar']], '&', 42);
}
public function testBuildThrowsExceptionWithQueryBuilder(): void
{
$this->expectException(SyntaxError::class);
QueryString::build([['foo', 'boo' => 'bar']]); /* @phpstan-ignore-line */
}
#[DataProvider('extractQueryProvider')]
public function testExtractQuery(Stringable|string|null|bool $query, array $expected): void
{
self::assertSame($expected, QueryString::extract($query));
}
public static function extractQueryProvider(): array
{
return [
[
'query' => null,
'expected' => [],
],
[
'query' => false,
'expected' => ['0' => ''],
],
[
'query' => '%25car=%25car',
'expected' => ['%car' => '%car'],
],
[
'query' => '&&',
'expected' => [],
],
[
'query' => true,
'expected' => ['1' => ''],
],
[
'query' => false,
'expected' => ['0' => ''],
],
[
'query' => 'arr[1=sid&arr[4][2=fred',
'expected' => [
'arr[1' => 'sid',
'arr' => ['4' => 'fred'],
],
],
[
'query' => 'arr1]=sid&arr[4]2]=fred',
'expected' => [
'arr1]' => 'sid',
'arr' => ['4' => 'fred'],
],
],
[
'query' => 'arr[one=sid&arr[4][two=fred',
'expected' => [
'arr[one' => 'sid',
'arr' => ['4' => 'fred'],
],
],
[
'query' => 'first=%41&second=%a&third=%b',
'expected' => [
'first' => 'A',
'second' => '%a',
'third' => '%b',
],
],
[
'query' => 'arr.test[1]=sid&arr test[4][two]=fred',
'expected' => [
'arr.test' => ['1' => 'sid'],
'arr test' => ['4' => ['two' => 'fred']],
],
],
[
'query' => 'foo&bar=&baz=bar&fo.o',
'expected' => [
'foo' => '',
'bar' => '',
'baz' => 'bar',
'fo.o' => '',
],
],
[
'query' => 'foo[]=bar&foo[]=baz',
'expected' => [
'foo' => ['bar', 'baz'],
],
],
];
}
/**
* @param non-empty-string $separator
*/
#[DataProvider('parserProvider')]
public function testParse(Stringable|string|null|bool $query, string $separator, array $expected, int $encoding): void
{
self::assertSame($expected, QueryString::parse($query, $separator, $encoding));
}
public static function parserProvider(): array
{
return [
'URI Component Object object' => [
Fragment::new('a=1&a=2'),
'&',
[['a', '1'], ['a', '2']],
PHP_QUERY_RFC3986,
],
'stringable object' => [
new class () {
public function __toString(): string
{
return 'a=1&a=2';
}
},
'&',
[['a', '1'], ['a', '2']],
PHP_QUERY_RFC3986,
],
'rfc1738 without hexaencoding' => [
'to+to=foo%2bbar',
'&',
[['to to', 'foo+bar']],
PHP_QUERY_RFC1738,
],
'null value' => [
null,
'&',
[],
PHP_QUERY_RFC3986,
],
'empty string' => [
'',
'&',
[['', null]],
PHP_QUERY_RFC3986,
],
'bool value' => [
false,
'&',
[['0', null]],
PHP_QUERY_RFC1738,
],
'identical keys' => [
'a=1&a=2',
'&',
[['a', '1'], ['a', '2']],
PHP_QUERY_RFC3986,
],
'no value' => [
'a&b',
'&',
[['a', null], ['b', null]],
PHP_QUERY_RFC3986,
],
'empty value' => [
'a=&b=',
'&',
[['a', ''], ['b', '']],
PHP_QUERY_RFC3986,
],
'php array' => [
'a[]=1&a[]=2',
'&',
[['a[]', '1'], ['a[]', '2']],
PHP_QUERY_RFC3986,
],
'preserve dot' => [
'a.b=3',
'&',
[['a.b', '3']],
PHP_QUERY_RFC3986,
],
'decode' => [
'a%20b=c%20d',
'&',
[['a b', 'c d']],
PHP_QUERY_RFC3986,
],
'no key stripping' => [
'a=&b',
'&',
[['a', ''], ['b', null]],
PHP_QUERY_RFC3986,
],
'no value stripping' => [
'a=b=',
'&',
[['a', 'b=']],
PHP_QUERY_RFC3986,
],
'key only' => [
'a',
'&',
[['a', null]],
PHP_QUERY_RFC3986,
],
'preserve falsey 1' => [
'0',
'&',
[['0', null]],
PHP_QUERY_RFC3986,
],
'preserve falsey 2' => [
'0=',
'&',
[['0', '']],
PHP_QUERY_RFC3986,
],
'preserve falsey 3' => [
'a=0',
'&',
[['a', '0']],
PHP_QUERY_RFC3986,
],
'different separator' => [
'a=0;b=0&c=4',
';',
[['a', '0'], ['b', '0&c=4']],
PHP_QUERY_RFC3986,
],
'numeric key only' => [
'42',
'&',
[['42', null]],
PHP_QUERY_RFC3986,
],
'numeric key' => [
'42=l33t',
'&',
[['42', 'l33t']],
PHP_QUERY_RFC3986,
],
'rfc1738' => [
'42=l3+3t',
'&',
[['42', 'l3 3t']],
PHP_QUERY_RFC1738,
],
];
}
#[DataProvider('buildProvider')]
public function testBuild(
iterable $pairs,
?string $expected_rfc1738,
?string $expected_rfc3986
): void {
self::assertSame($expected_rfc1738, QueryString::build($pairs, '&', PHP_QUERY_RFC1738));
self::assertSame($expected_rfc3986, QueryString::build($pairs, '&', PHP_QUERY_RFC3986));
}
public static function buildProvider(): array
{
return [
'empty string' => [
'pairs' => [],
'expected_rfc1738' => null,
'expected_rfc3986' => null,
],
'identical keys' => [
'pairs' => new ArrayIterator([['a', true] , [true, 'a']]),
'expected_rfc1738' => 'a=1&1=a',
'expected_rfc3986' => 'a=1&1=a',
],
'no value' => [
'pairs' => [['a', null], ['b', null]],
'expected_rfc1738' => 'a&b',
'expected_rfc3986' => 'a&b',
],
'empty value' => [
'pairs' => [['a', ''], ['b', 1.3]],
'expected_rfc1738' => 'a=&b=1.3',
'expected_rfc3986' => 'a=&b=1.3',
],
'php array (1)' => [
'pairs' => [['a[]', '1%a6'], ['a[]', '2']],
'expected_rfc1738' => 'a%5B%5D=1%25a6&a%5B%5D=2',
'expected_rfc3986' => 'a%5B%5D=1%25a6&a%5B%5D=2',
],
'php array (2)' => [
'pairs' => [['module', 'home'], ['action', 'show'], ['page', '😓']],
'expected_rfc1738' => 'module=home&action=show&page=%F0%9F%98%93',
'expected_rfc3986' => 'module=home&action=show&page=%F0%9F%98%93',
],
'php array (3)' => [
'pairs' => [['module', 'home'], ['action', 'v%61lue']],
'expected_rfc1738' => 'module=home&action=v%2561lue',
'expected_rfc3986' => 'module=home&action=v%2561lue',
],
'preserve dot' => [
'pairs' => [['a.b', '3']],
'expected_rfc1738' => 'a.b=3',
'expected_rfc3986' => 'a.b=3',
],
'no key stripping' => [
'pairs' => [['a', ''], ['b', null]],
'expected_rfc1738' => 'a=&b',
'expected_rfc3986' => 'a=&b',
],
'no value stripping' => [
'pairs' => [['a', 'b=']],
'expected_rfc1738' => 'a=b%3D',
'expected_rfc3986' => 'a=b%3D',
],
'key only' => [
'pairs' => [['a', null]],
'expected_rfc1738' => 'a',
'expected_rfc3986' => 'a',
],
'preserve falsey 1' => [
'pairs' => [['0', null]],
'expected_rfc1738' => '0',
'expected_rfc3986' => '0',
],
'preserve falsey 2' => [
'pairs' => [['0', '']],
'expected_rfc1738' => '0=',
'expected_rfc3986' => '0=',
],
'preserve falsey 3' => [
'pairs' => [['0', '0']],
'expected_rfc1738' => '0=0',
'expected_rfc3986' => '0=0',
],
'rcf1738' => [
'pairs' => [['toto', 'foo+bar toto']],
'expected_rfc1738' => 'toto=foo%2Bbar+toto',
'expected_rfc3986' => 'toto=foo%2Bbar%20toto',
],
'utf-8 characters' => [
'pairs' => [["\v\xED", "\v\xED"]],
'expected_rfc1738' => '%0B%ED=%0B%ED',
'expected_rfc3986' => '%0B%ED=%0B%ED',
],
'uri in value' => [
'pairs' => [['url', 'https://uri.thephpleague.com/components/2.0/?module=home#what-you-will-be-able-to-do with space']],
'expected_rfc1738' => 'url=https%3A%2F%2Furi.thephpleague.com%2Fcomponents%2F2.0%2F%3Fmodule%3Dhome%23what-you-will-be-able-to-do+with+space',
'expected_rfc3986' => 'url=https%3A%2F%2Furi.thephpleague.com%2Fcomponents%2F2.0%2F%3Fmodule%3Dhome%23what-you-will-be-able-to-do%20with%20space',
],
];
}
#[DataProvider('failedBuilderProvider')]
public function testBuildQueryThrowsException(iterable $pairs, string $separator, int $enc_type): void
{
$this->expectException(SyntaxError::class);
QueryString::build($pairs, $separator, $enc_type); /* @phpstan-ignore-line */
}
public static function failedBuilderProvider(): array
{
return [
'The collection cannot contain empty pair' => [
[[]],
'&',
PHP_QUERY_RFC1738,
],
'The pair key must be stringable' => [
[[date_create(), 'bar']],
'&',
PHP_QUERY_RFC1738,
],
'The pair value must be stringable or null - rfc3986/rfc1738' => [
[['foo', date_create()]],
'&',
PHP_QUERY_RFC3986,
],
'identical keys with associative array' => [
new ArrayIterator([['key' => 'a', 'value' => true] , ['key' => 'a', 'value' => '2']]),
'&',
PHP_QUERY_RFC3986,
],
'Object' => [
[['a[]', (object) '1']],
'&',
PHP_QUERY_RFC1738,
],
'the separator cannot be the empty string' => [
[['foo', 'bar']],
'',
PHP_QUERY_RFC3986,
],
];
}
#[DataProvider('queryProvider')]
public function testStringRepresentationComponent(string|array $input, string|null $expected): void
{
$query = is_array($input) ? QueryString::build($input) : QueryString::build(QueryString::parse($input));
self::assertSame($expected, $query);
}
public static function queryProvider(): array
{
$unreserved = 'a-zA-Z0-9.-_~!$&\'()*,;=:@';
return [
'bug fix issue 84' => ['fào=?%25bar&q=v%61lue', 'f%C3%A0o=%3F%25bar&q=value'],
'string' => ['kingkong=toto', 'kingkong=toto'],
'query object' => ['kingkong=toto', 'kingkong=toto'],
'empty string' => ['', ''],
'empty array' => [[], null],
'non empty array' => [[['', null]], ''],
'contains a reserved word #' => ['foo%23bar', 'foo%23bar'],
'contains a delimiter ?' => ['?foo%23bar', '%3Ffoo%23bar'],
'key-only' => ['k^ey', 'k%5Eey'],
'key-value' => ['k^ey=valu`', 'k%5Eey=valu%60'],
'array-key-only' => ['key[]', 'key%5B%5D'],
'array-key-value' => ['key[]=valu`', 'key%5B%5D=valu%60'],
'complex' => ['k^ey&key[]=valu`&f<>=`bar', 'k%5Eey&key%5B%5D=valu%60&f%3C%3E=%60bar'],
'Percent encode spaces' => ['q=va lue', 'q=va%20lue'],
'Percent encode multibyte' => ['€', '%E2%82%AC'],
"Don't encode something that's already encoded" => ['q=va%20lue', 'q=va%20lue'],
'Percent encode invalid percent encodings' => ['q=va%2-lue', 'q=va%252-lue'],
"Don't encode path segments" => ['q=va/lue', 'q=va%2Flue'],
"Don't encode unreserved chars or sub-delimiters" => [$unreserved, 'a-zA-Z0-9.-_~%21%24&%27%28%29%2A%2C%3B=%3A%40'],
'Encoded unreserved chars are not decoded' => ['q=v%61lue', 'q=value'],
];
}
#[Test]
#[DataProvider('queryWithInnerEmptyBracetsProvider')]
public function it_should_parse_empty_bracets_issue_146(string $query, string $expected): void
{
$data = QueryString::extract($query);
parse_str($query, $result);
self::assertSame($data, $result);
self::assertSame($expected, http_build_query($data, '', '&', PHP_QUERY_RFC3986));
}
public static function queryWithInnerEmptyBracetsProvider(): iterable
{
yield 'query with on level empty bracets' => [
'query' => 'foo[]=bar',
'expected' => 'foo%5B0%5D=bar',
];
yield 'query with two level bracets' => [
'query' => 'key[][][foo][9]=bar',
'expected' => 'key%5B0%5D%5B0%5D%5Bfoo%5D%5B9%5D=bar',
];
yield 'query with invalid remaining; close bracet without an opening bracet' => [
'query' => 'key[][]foo][9]=bar',
'expected' => 'key%5B0%5D%5B0%5D=bar',
];
yield 'query with invalid remaining; no opening bracet' => [
'query' => 'key[]9=bar',
'expected' => 'key%5B0%5D=bar',
];
yield 'query with invalid remaining; opening bracet no at the start of the remaining string' => [
'query' => 'key[]9[]=bar',
'expected' => 'key%5B0%5D=bar',
];
}
}