summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Http/HttpUtils.php16
-rw-r--r--Tests/Http/HttpUtilsTest.php16
2 files changed, 27 insertions, 5 deletions
diff --git a/Http/HttpUtils.php b/Http/HttpUtils.php
index 81893ff..761aa5a 100644
--- a/Http/HttpUtils.php
+++ b/Http/HttpUtils.php
@@ -136,15 +136,25 @@ class HttpUtils
return $request->getUriForPath($path);
}
- return $this->generateUrl($path, true);
+ return $this->generateUrl($path, $request->attributes->all(), true);
}
- private function generateUrl($route, $absolute = false)
+ private function generateUrl($route, array $attributes = array(), $absolute = false)
{
if (null === $this->urlGenerator) {
throw new \LogicException('You must provide a UrlGeneratorInterface instance to be able to use routes.');
}
- return $this->urlGenerator->generate($route, array(), $absolute);
+ $url = $this->urlGenerator->generate($route, $attributes, $absolute);
+
+ // unnecessary query string parameters must be removed from url
+ // (ie. query parameters that are presents in $attributes)
+ // fortunately, they all are, so we have to remove entire query string
+ $position = strpos($url, '?');
+ if (false !== $position) {
+ $url = substr($url, 0, $position);
+ }
+
+ return $url;
}
}
diff --git a/Tests/Http/HttpUtilsTest.php b/Tests/Http/HttpUtilsTest.php
index fc1b754..8a2d2f0 100644
--- a/Tests/Http/HttpUtilsTest.php
+++ b/Tests/Http/HttpUtilsTest.php
@@ -137,13 +137,25 @@ class HttpUtilsTest extends \PHPUnit_Framework_TestCase
$utils->checkRequestPath($this->getRequest(), 'foobar');
}
- private function getUrlGenerator()
+ public function testGenerateUrlRemovesQueryString()
+ {
+ $method = new \ReflectionMethod('Symfony\Component\Security\Http\HttpUtils', 'generateUrl');
+ $method->setAccessible(true);
+
+ $utils = new HttpUtils($this->getUrlGenerator());
+ $this->assertEquals('/foo/bar', $method->invoke($utils, 'route_name'));
+
+ $utils = new HttpUtils($this->getUrlGenerator('/foo/bar?param=value'));
+ $this->assertEquals('/foo/bar', $method->invoke($utils, 'route_name'));
+ }
+
+ private function getUrlGenerator($generatedUrl = '/foo/bar')
{
$urlGenerator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
$urlGenerator
->expects($this->any())
->method('generate')
- ->will($this->returnValue('/foo/bar'))
+ ->will($this->returnValue($generatedUrl))
;
return $urlGenerator;