Skip to content

Commit

Permalink
Fix OPTIONS method bug when use same path but diff domain
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesChou committed Dec 25, 2020
1 parent 344710f commit 20dd16e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Illuminate/Routing/CompiledRouteCollection.php
Expand Up @@ -252,6 +252,10 @@ public function getRoutesByMethod()
})
->map(function (Collection $routes) {
return $routes->mapWithKeys(function (Route $route) {
if ($domain = $route->getDomain()) {
return [$domain . '/' . $route->uri => $route];
}

return [$route->uri => $route];
})->all();
})
Expand Down
53 changes: 53 additions & 0 deletions tests/Integration/Routing/CompiledRouteCollectionTest.php
Expand Up @@ -490,6 +490,59 @@ public function testTrailingSlashIsTrimmedWhenMatchingCachedRoutes()
$this->assertSame('foo', $this->collection()->match($request)->getName());
}

public function testRouteWithSamePathAndSameMethodButDiffDomainNameWithOptionsMethod()
{
$routes = [
'foo_domain' => $this->newRoute('GET', 'same/path', [
'uses' => 'FooController@index',
'as' => 'foo',
'domain' => 'foo.localhost',
]),
'bar_domain' => $this->newRoute('GET', 'same/path', [
'uses' => 'BarController@index',
'as' => 'bar',
'domain' => 'bar.localhost',
]),
'no_domain' => $this->newRoute('GET', 'same/path', [
'uses' => 'BarController@index',
'as' => 'no_domain',
]),
];

$this->routeCollection->add($routes['foo_domain']);
$this->routeCollection->add($routes['bar_domain']);
$this->routeCollection->add($routes['no_domain']);

$expectedMethods = [
'OPTIONS',
];

$this->assertSame($expectedMethods, $this->collection()->match(
Request::create('http://foo.localhost/same/path', 'OPTIONS')
)->methods);

$this->assertSame($expectedMethods, $this->collection()->match(
Request::create('http://bar.localhost/same/path', 'OPTIONS')
)->methods);

$this->assertSame($expectedMethods, $this->collection()->match(
Request::create('http://no.localhost/same/path', 'OPTIONS')
)->methods);

$this->assertEquals([
'HEAD' => [
'foo.localhost/same/path' => $routes['foo_domain'],
'bar.localhost/same/path' => $routes['bar_domain'],
'same/path' => $routes['no_domain'],
],
'GET' => [
'foo.localhost/same/path' => $routes['foo_domain'],
'bar.localhost/same/path' => $routes['bar_domain'],
'same/path' => $routes['no_domain'],
],
], $this->collection()->getRoutesByMethod());
}

/**
* Create a new Route object.
*
Expand Down

0 comments on commit 20dd16e

Please sign in to comment.