8000 [Routing] Add a '_host' requirement for routes by sebhoerl · Pull Request #3002 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Routing] Add a '_host' requirement for routes #3002

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Routing] Added tests for host requirement
  • Loading branch information
sebhoerl committed Dec 30, 2011
commit 12bef9ff84391e60d0e982db8e20d4e439f2487a
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren

if ($hostreq = $route->getRequirement('_host')) {
$code[] = <<<EOF
if(\$this->context->getHost() !== '$hostreq') {
goto $gotoname;
}
if(\$this->context->getHost() !== '$hostreq') {
goto $gotoname;
}
EOF;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ RewriteRule .* - [QSA,L]
RewriteCond %{REQUEST_URI} ^/foo/(baz|symfony)$
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:foo,E=_ROUTING_bar:%1,E=_ROUTING_def:test]

# hostbar
RewriteCond %{HTTP_HOST} example.org
RewriteCond %{REQUEST_URI} ^/hostbar$
RewriteRule .* app.php [QSA,L,E=_ROUTING__route:hostbar]

# bar
RewriteCond %{REQUEST_URI} ^/bar/([^/]+?)$
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [NC]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ public function match($pathinfo)
}
not_bar:

// barhost
if ($pathinfo === '/bar/') {
if($this->context->getHost() !== 'example.org') {
goto not_barhost;
}
return array('_route' => 'barhost');
}
not_barhost:

// barhead
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#xs', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public function match($pathinfo)
}
not_bar:

// barhost
if (rtrim($pathinfo, '/') === '/bar') {
if($this->context->getHost() !== 'example.org') {
goto not_barhost;
}
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'barhost');
}
return array('_route' => 'barhost');
}
not_barhost:

< 8000 /span>
// barhead
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]+?)$#xs', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@

class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testAbsoluteUrlWithHost()
{
$routes = $this->getRoutes('test', new Route( '/testing', array(), array('_host' => 'example.org')));
$url = $this->getGenerator($routes)->generate('test', array(), true);

$this->assertEquals('http://example.org/app.php/testing', $url);
}

public function testAbsoluteUrlWithPort80()
{
$routes = $this->getRoutes('test', new Route('/testing'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function testDump()
array('def' => 'test'),
array('bar' => 'baz|symfony')
));
// host requirement
$collection->add('hostbar', new Route(
'/hostbar',
array(),
array('_host' => 'example.org')
));
// method requirement
$collection->add('bar', new Route(
'/bar/{foo}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ protected function getRouteCollection()
array(),
array('_method' => 'GET|head')
));
// host requirement
$collection->add('barhost', new Route(
'/bar/',
array(),
array('_host' => 'example.org')
));
// GET method requirement automatically adds HEAD as valid
$collection->add('barhead', new Route(
'/barhead/{foo}',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@ public function testNoMethodSoAllowed()
$matcher->match('/foo');
}

public function testHostRequirement()
public function testHostRequirement()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array('_host' => 'example.org')));

$matcher = new UrlMatcher($coll, new RequestContext('', 'get', 'example.org'));
$this->assertEquals(array('_route' => 'foo'), $matcher->match( '/foo' ) );
$this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo'));

$matcher = new UrlMatcher($coll, new RequestContext('', 'get', 'other.host'));

$matcher = new UrlMatcher($coll, new RequestContext('', 'get', 'other.host'));
try {
$matcher->match( '/foo' );
$matcher->match('/foo');
$this->fail();
} catch (ResourceNotFoundException $e) {}

}

public function testMethodNotAllowed()
Expand Down
0