[HttpFoundation] updated getQueryString() to work in more scenarios
This commit is contained in:
parent
b16303e8b2
commit
1443d4a0ba
@ -388,13 +388,21 @@ class Request
|
|||||||
}
|
}
|
||||||
|
|
||||||
$parts = array();
|
$parts = array();
|
||||||
foreach (explode('&', $qs) as $segment) {
|
$order = array();
|
||||||
$tmp = explode('=', urldecode($segment), 2);
|
|
||||||
$parts[urlencode($tmp[0])] = urlencode($tmp[1]);
|
|
||||||
}
|
|
||||||
ksort($parts);
|
|
||||||
|
|
||||||
return http_build_query($parts);
|
foreach (explode('&', $qs) as $segment) {
|
||||||
|
if (false === strpos($segment, '=')) {
|
||||||
|
$parts[] = $segment;
|
||||||
|
$order[] = $segment;
|
||||||
|
} else {
|
||||||
|
$tmp = explode('=', urldecode($segment), 2);
|
||||||
|
$parts[] = urlencode($tmp[0]).'='.urlencode($tmp[1]);
|
||||||
|
$order[] = $tmp[0];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
array_multisort($order, SORT_ASC, $parts);
|
||||||
|
|
||||||
|
return implode('&', $parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isSecure()
|
public function isSecure()
|
||||||
|
@ -78,4 +78,33 @@ class RequestTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('js', $request->getFormat('application/x-javascript'), '->getFormat() returns correct format when format have multiple mime-type');
|
$this->assertEquals('js', $request->getFormat('application/x-javascript'), '->getFormat() returns correct format when format have multiple mime-type');
|
||||||
$this->assertEquals('js', $request->getFormat('text/javascript'), '->getFormat() returns correct format when format have multiple mime-type (last)');
|
$this->assertEquals('js', $request->getFormat('text/javascript'), '->getFormat() returns correct format when format have multiple mime-type (last)');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Symfony\Component\HttpFoundation\Request::getQueryString
|
||||||
|
*/
|
||||||
|
public function testGetQueryString()
|
||||||
|
{
|
||||||
|
$request = new Request();
|
||||||
|
|
||||||
|
$request->server->set('QUERY_STRING', 'foo');
|
||||||
|
$this->assertEquals('foo', $request->getQueryString(), '->getQueryString() works with valueless parameters');
|
||||||
|
|
||||||
|
$request->server->set('QUERY_STRING', 'foo=');
|
||||||
|
$this->assertEquals('foo=', $request->getQueryString(), '->getQueryString() includes a dangling equal sign');
|
||||||
|
|
||||||
|
$request->server->set('QUERY_STRING', 'bar=&foo=bar');
|
||||||
|
$this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() works when empty parameters');
|
||||||
|
|
||||||
|
$request->server->set('QUERY_STRING', 'foo=bar&bar=');
|
||||||
|
$this->assertEquals('bar=&foo=bar', $request->getQueryString(), '->getQueryString() sorts keys alphabetically');
|
||||||
|
|
||||||
|
$request->server->set('QUERY_STRING', 'him=John%20Doe&her=Jane+Doe');
|
||||||
|
$this->assertEquals('her=Jane+Doe&him=John+Doe', $request->getQueryString(), '->getQueryString() normalizes encoding');
|
||||||
|
|
||||||
|
$request->server->set('QUERY_STRING', 'foo[]=1&foo[]=2');
|
||||||
|
$this->assertEquals('foo%5B%5D=1&foo%5B%5D=2', $request->getQueryString(), '->getQueryString() allows array notation');
|
||||||
|
|
||||||
|
$request->server->set('QUERY_STRING', 'foo=1&foo=2');
|
||||||
|
$this->assertEquals('foo=1&foo=2', $request->getQueryString(), '->getQueryString() allows repeated parameters');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user