From 557dfaa2c1623e2160d75473a8a736a1d9522f6b Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Sat, 23 Nov 2013 00:07:07 +0100 Subject: [PATCH] Remove usage of deprecated _scheme in Routing Component Instead correctly use the array of schemes from the Route. Also adjusted the dumpers to dump the correct data. I extended the tests to not only test the deprecated behavior, but also the new schemes-requirement. --- .../Routing/RedirectableUrlMatcherTest.php | 22 +++++++- .../Generator/Dumper/PhpGeneratorDumper.php | 5 +- .../Routing/Generator/UrlGenerator.php | 23 ++++++-- .../Matcher/Dumper/PhpMatcherDumper.php | 18 ++++--- .../Matcher/RedirectableUrlMatcher.php | 7 +-- .../Routing/Matcher/TraceableUrlMatcher.php | 8 +-- .../Component/Routing/Matcher/UrlMatcher.php | 4 +- src/Symfony/Component/Routing/Route.php | 19 +++++++ .../Tests/Fixtures/dumper/url_matcher2.php | 10 ++-- .../Dumper/PhpGeneratorDumperTest.php | 33 ++++++++++++ .../Tests/Generator/UrlGeneratorTest.php | 54 ++++++++++++++++--- .../Matcher/RedirectableUrlMatcherTest.php | 30 ++++++++++- .../Routing/Tests/Matcher/UrlMatcherTest.php | 12 ++++- .../Component/Routing/Tests/RouteTest.php | 8 +++ 14 files changed, 219 insertions(+), 34 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php index fe8fc709a6312..282f860262425 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php @@ -38,7 +38,7 @@ public function testRedirectWhenNoSlash() ); } - public function testSchemeRedirect() + public function testSchemeRedirectBC() { $coll = new RouteCollection(); $coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https'))); @@ -57,4 +57,24 @@ public function testSchemeRedirect() $matcher->match('/foo') ); } + + public function testSchemeRedirect() + { + $coll = new RouteCollection(); + $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https'))); + + $matcher = new RedirectableUrlMatcher($coll, $context = new RequestContext()); + + $this->assertEquals(array( + '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', + 'path' => '/foo', + 'permanent' => true, + 'scheme' => 'https', + 'httpPort' => $context->getHttpPort(), + 'httpsPort' => $context->getHttpsPort(), + '_route' => 'foo', + ), + $matcher->match('/foo') + ); + } } diff --git a/src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php b/src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php index 42cd9108939da..4d19d2a2ec8db 100644 --- a/src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php +++ b/src/Symfony/Component/Routing/Generator/Dumper/PhpGeneratorDumper.php @@ -92,6 +92,7 @@ private function generateDeclaredRoutes() $properties[] = $route->getRequirements(); $properties[] = $compiledRoute->getTokens(); $properties[] = $compiledRoute->getHostTokens(); + $properties[] = $route->getSchemes(); $routes .= sprintf(" '%s' => %s,\n", $name, str_replace("\n", '', var_export($properties, true))); } @@ -114,9 +115,9 @@ public function generate(\$name, \$parameters = array(), \$referenceType = self: throw new RouteNotFoundException(sprintf('Unable to generate a URL for the named route "%s" as such route does not exist.', \$name)); } - list(\$variables, \$defaults, \$requirements, \$tokens, \$hostTokens) = self::\$declaredRoutes[\$name]; + list(\$variables, \$defaults, \$requirements, \$tokens, \$hostTokens, \$requiredSchemes) = self::\$declaredRoutes[\$name]; - return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$referenceType, \$hostTokens); + return \$this->doGenerate(\$variables, \$defaults, \$requirements, \$tokens, \$parameters, \$name, \$referenceType, \$hostTokens, \$requiredSchemes); } EOF; } diff --git a/src/Symfony/Component/Routing/Generator/UrlGenerator.php b/src/Symfony/Component/Routing/Generator/UrlGenerator.php index f224cb3f6d9f6..59ce9e25a5226 100644 --- a/src/Symfony/Component/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Component/Routing/Generator/UrlGenerator.php @@ -137,7 +137,7 @@ public function generate($name, $parameters = array(), $referenceType = self::AB // the Route has a cache of its own and is not recompiled as long as it does not get modified $compiledRoute = $route->compile(); - return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $referenceType, $compiledRoute->getHostTokens()); + return $this->doGenerate($compiledRoute->getVariables(), $route->getDefaults(), $route->getRequirements(), $compiledRoute->getTokens(), $parameters, $name, $referenceType, $compiledRoute->getHostTokens(), $route->getSchemes()); } /** @@ -145,7 +145,7 @@ public function generate($name, $parameters = array(), $referenceType = self::AB * @throws InvalidParameterException When a parameter value for a placeholder is not correct because * it does not match the requirement */ - protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens) + protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens, array $requiredSchemes = array()) { $variables = array_flip($variables); $mergedParams = array_replace($defaults, $this->context->getParameters(), $parameters); @@ -204,7 +204,24 @@ protected function doGenerate($variables, $defaults, $requirements, $tokens, $pa $schemeAuthority = ''; if ($host = $this->context->getHost()) { $scheme = $this->context->getScheme(); - if (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) { + + if ($requiredSchemes) { + $schemeMatched = false; + foreach ($requiredSchemes as $requiredScheme) { + if ($scheme === $requiredScheme) { + $schemeMatched = true; + + break; + } + } + + if (!$schemeMatched) { + $referenceType = self::ABSOLUTE_URL; + $scheme = current($requiredSchemes); + } + + } elseif (isset($requirements['_scheme']) && ($req = strtolower($requirements['_scheme'])) && $scheme !== $req) { + // We do this for BC; to be removed if _scheme is not supported anymore $referenceType = self::ABSOLUTE_URL; $scheme = $req; } diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index dc17ffbe984a3..2b71b9ef5f1c2 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -280,14 +280,15 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren EOF; } - if ($scheme = $route->getRequirement('_scheme')) { + if ($schemes = $route->getSchemes()) { if (!$supportsRedirections) { - throw new \LogicException('The "_scheme" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.'); + throw new \LogicException('The "schemes" requirement is only supported for URL matchers that implement RedirectableUrlMatcherInterface.'); } - + $schemes = str_replace("\n", '', var_export(array_flip($schemes), true)); $code .= <<context->getScheme() !== '$scheme') { - return \$this->redirect(\$pathinfo, '$name', '$scheme'); + \$requiredSchemes = $schemes; + if (!isset(\$requiredSchemes[\$this->context->getScheme()])) { + return \$this->redirect(\$pathinfo, '$name', key(\$requiredSchemes)); } @@ -305,8 +306,11 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren } $vars[] = "array('_route' => '$name')"; - $code .= sprintf(" return \$this->mergeDefaults(array_replace(%s), %s);\n" - , implode(', ', $vars), str_replace("\n", '', var_export($route->getDefaults(), true))); + $code .= sprintf( + " return \$this->mergeDefaults(array_replace(%s), %s);\n", + implode(', ', $vars), + str_replace("\n", '', var_export($route->getDefaults(), true)) + ); } elseif ($route->getDefaults()) { $code .= sprintf(" return %s;\n", str_replace("\n", '', var_export(array_replace($route->getDefaults(), array('_route' => $name)), true))); diff --git a/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php b/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php index 51e80057cd53b..e24a15c0156ac 100644 --- a/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/RedirectableUrlMatcher.php @@ -51,9 +51,10 @@ public function match($pathinfo) protected function handleRouteRequirements($pathinfo, $name, Route $route) { // check HTTP scheme requirement - $scheme = $route->getRequirement('_scheme'); - if ($scheme && $this->context->getScheme() !== $scheme) { - return array(self::ROUTE_MATCH, $this->redirect($pathinfo, $name, $scheme)); + $scheme = $this->context->getScheme(); + $schemes = $route->getSchemes(); + if ($schemes && !$route->hasScheme($scheme)) { + return array(self::ROUTE_MATCH, $this->redirect($pathinfo, $name, current($schemes))); } return array(self::REQUIREMENT_MATCH, null); diff --git a/src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php b/src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php index c09f83e86a227..9062e9914ec6b 100644 --- a/src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php @@ -95,9 +95,11 @@ protected function matchCollection($pathinfo, RouteCollection $routes) } // check HTTP scheme requirement - if ($scheme = $route->getRequirement('_scheme')) { - if ($this->context->getScheme() !== $scheme) { - $this->addTrace(sprintf('Scheme "%s" does not match the requirement ("%s"); the user will be redirected', $this->context->getScheme(), $scheme), self::ROUTE_ALMOST_MATCHES, $name, $route); + if ($requiredSchemes = $route->getSchemes()) { + $scheme = $this->context->getScheme(); + + if (!$route->hasScheme($scheme)) { + $this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes ("%s"); the user will be redirected to first required scheme', $scheme, implode(', ', $requiredSchemes)), self::ROUTE_ALMOST_MATCHES, $name, $route); return true; } diff --git a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php index db18ec4e7b259..21d81106223bb 100644 --- a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php @@ -181,8 +181,8 @@ protected function getAttributes(Route $route, $name, array $attributes) protected function handleRouteRequirements($pathinfo, $name, Route $route) { // check HTTP scheme requirement - $scheme = $route->getRequirement('_scheme'); - $status = $scheme && $scheme !== $this->context->getScheme() ? self::REQUIREMENT_MISMATCH : self::REQUIREMENT_MATCH; + $scheme = $this->context->getScheme(); + $status = $route->getSchemes() && !$route->hasScheme($scheme) ? self::REQUIREMENT_MISMATCH : self::REQUIREMENT_MATCH; return array($status, null); } diff --git a/src/Symfony/Component/Routing/Route.php b/src/Symfony/Component/Routing/Route.php index 5bc535c683ed2..45034027f10f5 100644 --- a/src/Symfony/Component/Routing/Route.php +++ b/src/Symfony/Component/Routing/Route.php @@ -241,6 +241,25 @@ public function setSchemes($schemes) return $this; } + /** + * Checks if a scheme requirement has been set. + * + * @param string $scheme + * + * @return Boolean true if the scheme requirement exists, otherwise false + */ + public function hasScheme($scheme) + { + $scheme = strtolower($scheme); + foreach ($this->schemes as $requiredScheme) { + if ($scheme === $requiredScheme) { + return true; + } + } + + return false; + } + /** * Returns the uppercased HTTP methods this route is restricted to. * So an empty array means that any method is allowed. diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php index ad157909b8c0e..a27be98d39173 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/dumper/url_matcher2.php @@ -319,8 +319,9 @@ public function match($pathinfo) // secure if ($pathinfo === '/secure') { - if ($this->context->getScheme() !== 'https') { - return $this->redirect($pathinfo, 'secure', 'https'); + $requiredSchemes = array ( 'https' => 0,); + if (!isset($requiredSchemes[$this->context->getScheme()])) { + return $this->redirect($pathinfo, 'secure', key($requiredSchemes)); } return array('_route' => 'secure'); @@ -328,8 +329,9 @@ public function match($pathinfo) // nonsecure if ($pathinfo === '/nonsecure') { - if ($this->context->getScheme() !== 'http') { - return $this->redirect($pathinfo, 'nonsecure', 'http'); + $requiredSchemes = array ( 'http' => 0,); + if (!isset($requiredSchemes[$this->context->getScheme()])) { + return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes)); } return array('_route' => 'nonsecure'); diff --git a/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php b/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php index ab5f4cdae0aab..78e3907fd56bf 100644 --- a/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php +++ b/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php @@ -114,4 +114,37 @@ public function testDumpForRouteWithDefaults() $this->assertEquals($url, '/testing'); } + + public function testDumpWithSchemeRequirement() + { + $this->routeCollection->add('Test1', new Route('/testing', array(), array(), array(), '', array('ftp', 'https'))); + $this->routeCollection->add('Test2', new Route('/testing_bc', array(), array('_scheme' => 'https'))); // BC + + file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'SchemeUrlGenerator'))); + include ($this->testTmpFilepath); + + $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php')); + + $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), true); + $absoluteUrlBC = $projectUrlGenerator->generate('Test2', array(), true); + $relativeUrl = $projectUrlGenerator->generate('Test1', array(), false); + $relativeUrlBC = $projectUrlGenerator->generate('Test2', array(), false); + + $this->assertEquals($absoluteUrl, 'ftp://localhost/app.php/testing'); + $this->assertEquals($absoluteUrlBC, 'https://localhost/app.php/testing_bc'); + $this->assertEquals($relativeUrl, 'ftp://localhost/app.php/testing'); + $this->assertEquals($relativeUrlBC, 'https://localhost/app.php/testing_bc'); + + $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php', 'GET', 'localhost', 'https')); + + $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), true); + $absoluteUrlBC = $projectUrlGenerator->generate('Test2', array(), true); + $relativeUrl = $projectUrlGenerator->generate('Test1', array(), false); + $relativeUrlBC = $projectUrlGenerator->generate('Test2', array(), false); + + $this->assertEquals($absoluteUrl, 'https://localhost/app.php/testing'); + $this->assertEquals($absoluteUrlBC, 'https://localhost/app.php/testing_bc'); + $this->assertEquals($relativeUrl, '/app.php/testing'); + $this->assertEquals($relativeUrlBC, '/app.php/testing_bc'); + } } diff --git a/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php b/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php index 5f8ef491276c2..dea0e7f4a9e74 100644 --- a/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php +++ b/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php @@ -248,22 +248,40 @@ public function testRequiredParamAndEmptyPassed() public function testSchemeRequirementDoesNothingIfSameCurrentScheme() { - $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http'))); + $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http'))); // BC $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test')); - $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https'))); + $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https'))); // BC + $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); + + $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('http'))); + $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test')); + + $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('https'))); $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); } public function testSchemeRequirementForcesAbsoluteUrl() { - $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https'))); + $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https'))); // BC + $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test')); + + $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http'))); // BC + $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); + + $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('https'))); $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test')); - $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http'))); + $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('http'))); $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); } + public function testSchemeRequirementCreatesUrlForFirstRequiredScheme() + { + $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('Ftp', 'https'))); + $this->assertEquals('ftp://localhost/app.php/', $this->getGenerator($routes)->generate('test')); + } + public function testPathWithTwoStartingSlashes() { $routes = $this->getRoutes('test', new Route('//path-and-not-domain')); @@ -447,7 +465,7 @@ public function testUrlWithInvalidParameterInHostInNonStrictMode() $this->assertNull($generator->generate('test', array('foo' => 'baz'), false)); } - public function testGenerateNetworkPath() + public function testGenerateNetworkPathBC() { $routes = $this->getRoutes('test', new Route('/{name}', array(), array('_scheme' => 'http'), array(), '{locale}.example.com')); @@ -465,13 +483,32 @@ public function testGenerateNetworkPath() ); } + public function testGenerateNetworkPath() + { + $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com', array('http'))); + + $this->assertSame('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', + array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'network path with different host' + ); + $this->assertSame('//fr.example.com/app.php/Fabien?query=string', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', + array('name' =>'Fabien', 'locale' => 'fr', 'query' => 'string'), UrlGeneratorInterface::NETWORK_PATH), 'network path although host same as context' + ); + $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test', + array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'absolute URL because scheme requirement does not match context' + ); + $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', + array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::ABSOLUTE_URL), 'absolute URL with same scheme because it is requested' + ); + } + public function testGenerateRelativePath() { $routes = new RouteCollection(); $routes->add('article', new Route('/{author}/{article}/')); $routes->add('comments', new Route('/{author}/{article}/comments')); $routes->add('host', new Route('/{article}', array(), array(), array(), '{author}.example.com')); - $routes->add('scheme', new Route('/{author}', array(), array('_scheme' => 'https'))); + $routes->add('schemeBC', new Route('/{author}', array(), array('_scheme' => 'https'))); // BC + $routes->add('scheme', new Route('/{author}/blog', array(), array(), array(), '', array('https'))); $routes->add('unrelated', new Route('/about')); $generator = $this->getGenerator($routes, array('host' => 'example.com', 'pathInfo' => '/fabien/symfony-is-great/')); @@ -491,9 +528,12 @@ public function testGenerateRelativePath() $this->assertSame('//bernhard.example.com/app.php/forms-are-great', $generator->generate('host', array('author' =>'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH) ); - $this->assertSame('https://example.com/app.php/bernhard', $generator->generate('scheme', + $this->assertSame('https://example.com/app.php/bernhard', $generator->generate('schemeBC', array('author' =>'bernhard'), UrlGeneratorInterface::RELATIVE_PATH) ); + $this->assertSame('https://example.com/app.php/bernhard/blog', $generator->generate('scheme', + array('author' =>'bernhard'), UrlGeneratorInterface::RELATIVE_PATH) + ); $this->assertSame('../../about', $generator->generate('unrelated', array(), UrlGeneratorInterface::RELATIVE_PATH) ); diff --git a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php index 2ad4fc8725a82..5cbb605479117 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php @@ -41,7 +41,7 @@ public function testRedirectWhenNoSlashForNonSafeMethod() $matcher->match('/foo'); } - public function testSchemeRedirect() + public function testSchemeRedirectBC() { $coll = new RouteCollection(); $coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https'))); @@ -55,4 +55,32 @@ public function testSchemeRedirect() ; $matcher->match('/foo'); } + + public function testSchemeRedirectRedirectsToFirstScheme() + { + $coll = new RouteCollection(); + $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('FTP', 'HTTPS'))); + + $matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); + $matcher + ->expects($this->once()) + ->method('redirect') + ->with('/foo', 'foo', 'ftp') + ->will($this->returnValue(array('_route' => 'foo'))) + ; + $matcher->match('/foo'); + } + + public function testNoSchemaRedirectIfOnOfMultipleSchemesMatches() + { + $coll = new RouteCollection(); + $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https', 'http'))); + + $matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); + $matcher + ->expects($this->never()) + ->method('redirect') + ; + $matcher->match('/foo'); + } } diff --git a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php index 8a1428f170859..ce0f0ea513864 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php @@ -313,13 +313,23 @@ public function testDefaultRequirementOfVariableDisallowsNextSeparator() /** * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException */ - public function testSchemeRequirement() + public function testSchemeRequirementBC() { $coll = new RouteCollection(); $coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https'))); $matcher = new UrlMatcher($coll, new RequestContext()); $matcher->match('/foo'); } + /** + * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException + */ + public function testSchemeRequirement() + { + $coll = new RouteCollection(); + $coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https'))); + $matcher = new UrlMatcher($coll, new RequestContext()); + $matcher->match('/foo'); + } public function testDecodeOnce() { diff --git a/src/Symfony/Component/Routing/Tests/RouteTest.php b/src/Symfony/Component/Routing/Tests/RouteTest.php index 31f1066feda1e..e4046fafd5a52 100644 --- a/src/Symfony/Component/Routing/Tests/RouteTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteTest.php @@ -141,10 +141,15 @@ public function testScheme() { $route = new Route('/'); $this->assertEquals(array(), $route->getSchemes(), 'schemes is initialized with array()'); + $this->assertFalse($route->hasScheme('http')); $route->setSchemes('hTTp'); $this->assertEquals(array('http'), $route->getSchemes(), '->setSchemes() accepts a single scheme string and lowercases it'); + $this->assertTrue($route->hasScheme('htTp')); + $this->assertFalse($route->hasScheme('httpS')); $route->setSchemes(array('HttpS', 'hTTp')); $this->assertEquals(array('https', 'http'), $route->getSchemes(), '->setSchemes() accepts an array of schemes and lowercases them'); + $this->assertTrue($route->hasScheme('htTp')); + $this->assertTrue($route->hasScheme('httpS')); } public function testSchemeIsBC() @@ -153,6 +158,9 @@ public function testSchemeIsBC() $route->setRequirement('_scheme', 'http|https'); $this->assertEquals('http|https', $route->getRequirement('_scheme')); $this->assertEquals(array('http', 'https'), $route->getSchemes()); + $this->assertTrue($route->hasScheme('https')); + $this->assertTrue($route->hasScheme('http')); + $this->assertFalse($route->hasScheme('ftp')); $route->setSchemes(array('hTTp')); $this->assertEquals('http', $route->getRequirement('_scheme')); $route->setSchemes(array()); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy