Skip to content

Commit b0ba23e

Browse files
committed
Fixed request targetting API returning false positives for APIs not operating under a prefix or domain.
Signed-off-by: Jason Lewis <[email protected]>
1 parent aad963b commit b0ba23e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/Routing/Router.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,23 @@ public function requestTargettingApi($request = null)
400400
return true;
401401
}
402402

403-
return $this->getApiRouteCollectionFromRequest($request) instanceof ApiRouteCollection;
403+
if ($collection = $this->getApiRouteCollectionFromRequest($request))
404+
{
405+
try
406+
{
407+
$collection->match($request);
408+
409+
return true;
410+
}
411+
catch (NotFoundHttpException $exception)
412+
{
413+
// If we don't find a matching route then we'll let this
414+
// fall through so that false is returned as the
415+
// request is not targetting the API.
416+
}
417+
}
418+
419+
return false;
404420
}
405421

406422
/**

tests/RoutingRouterTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,17 @@ public function testRouterIgnoresRouteGroupsWithAnApiPrefix()
342342
}
343343

344344

345+
public function testRequestTargettingAnApiWithNoPrefixOrDomain()
346+
{
347+
$this->router->get('/', function() { return 'foo'; });
348+
349+
$this->router->api(['version' => 'v1'], function()
350+
{
351+
$this->router->get('foo', function() { return 'bar'; });
352+
});
353+
354+
$this->assertFalse($this->router->requestTargettingApi(Request::create('/', 'GET')));
355+
}
356+
357+
345358
}

0 commit comments

Comments
 (0)