Skip to content

Commit 0880174

Browse files
committed
[HttpFoundation] added failing tests for query string normalization
1 parent 3ee1b38 commit 0880174

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/Symfony/Component/HttpFoundation/Tests/RequestTest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ public function testGetQueryString()
510510
{
511511
$request = new Request();
512512

513+
$this->assertNull($request->getQueryString(), '->getQueryString() returns null for non-existent query string');
514+
515+
$request->server->set('QUERY_STRING', '');
516+
$this->assertNull($request->getQueryString(), '->getQueryString() returns null for empty query string');
517+
513518
$request->server->set('QUERY_STRING', 'foo');
514519
$this->assertEquals('foo', $request->getQueryString(), '->getQueryString() works with valueless parameters');
515520

@@ -523,13 +528,38 @@ public function testGetQueryString()
523528
$this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() sorts keys alphabetically');
524529

525530
$request->server->set('QUERY_STRING', 'him=John%20Doe&her=Jane+Doe');
526-
$this->assertEquals('her=Jane%2BDoe&him=John%20Doe', $request->getQueryString(), '->getQueryString() normalizes encoding');
531+
// GET parameters, that are submitted from a HTML form, encode spaces as "+" by default (as defined in enctype application/x-www-form-urlencoded).
532+
// PHP also converts "+" to spaces when filling the global _GET or when using the function parse_str.
533+
$this->assertSame('her=Jane%20Doe&him=John%20Doe', $request->getQueryString(), '->getQueryString() normalizes spaces in both encodings "%20" and "+"');
527534

528535
$request->server->set('QUERY_STRING', 'foo[]=1&foo[]=2');
529536
$this->assertEquals('foo%5B%5D=1&foo%5B%5D=2', $request->getQueryString(), '->getQueryString() allows array notation');
530537

531538
$request->server->set('QUERY_STRING', 'foo=1&foo=2');
532539
$this->assertEquals('foo=1&foo=2', $request->getQueryString(), '->getQueryString() allows repeated parameters');
540+
541+
$request->server->set('QUERY_STRING', 'pa%3Dram=foo%26bar%3Dbaz&test=test');
542+
$this->assertSame('pa%3Dram=foo%26bar%3Dbaz&test=test', $request->getQueryString(), '->getQueryString() works with encoded delimiters');
543+
544+
$request->server->set('QUERY_STRING', '0');
545+
$this->assertSame('0', $request->getQueryString(), '->getQueryString() allows "0"');
546+
547+
$request->server->set('QUERY_STRING', 'Jane Doe&John%20Doe');
548+
$this->assertSame('Jane%20Doe&John%20Doe', $request->getQueryString(), '->getQueryString() normalizes encoding in keys');
549+
550+
$request->server->set('QUERY_STRING', 'her=Jane Doe&him=John%20Doe');
551+
$this->assertSame('her=Jane%20Doe&him=John%20Doe', $request->getQueryString(), '->getQueryString() normalizes encoding in values');
552+
553+
$request->server->set('QUERY_STRING', 'foo=bar&&&test&&');
554+
$this->assertSame('foo=bar&test', $request->getQueryString(), '->getQueryString() removes unneeded delimiters');
555+
556+
$request->server->set('QUERY_STRING', 'formula=e=m*c^2');
557+
$this->assertSame('formula=e%3Dm%2Ac%5E2', $request->getQueryString(), '->getQueryString() correctly treats only the first "=" as delimiter and the next as value');
558+
559+
$request->server->set('QUERY_STRING', 'foo=bar&=a=b&=x=y');
560+
// Ignore pairs with empty key, even if there was a value, e.g. "=value", as such nameless values cannot be retrieved anyway.
561+
// PHP also does not include them when building _GET.
562+
$this->assertSame('foo=bar', $request->getQueryString(), '->getQueryString() removes params with empty key');
533563
}
534564

535565
/**

0 commit comments

Comments
 (0)