Skip to content
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

Treat PSR-7 request conversions as route failures #13

Merged
merged 4 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion .laminas-ci.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"ignore_php_platform_requirements": {
"8.1": true
"8.0": false,
"8.1": false
}
}
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@
"forum": "https://discourse.laminas.dev"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"extra": {
"laminas": {
Expand Down
20 changes: 13 additions & 7 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<?xml version="1.0"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
convertDeprecationsToExceptions="true" >
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="Mezzio laminas-mvc Router Tests">
<directory>./test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
15 changes: 13 additions & 2 deletions src/LaminasRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

namespace Mezzio\Router;

use Laminas\Http\Exception\InvalidArgumentException;
use Laminas\Psr7Bridge\Psr7ServerRequest;
use Laminas\Router\Http\TreeRouteStack;
use Laminas\Router\RouteMatch;
use Laminas\Uri\Exception\InvalidUriPartException;
use Psr\Http\Message\ServerRequestInterface as PsrRequest;

use function array_key_exists;
Expand Down Expand Up @@ -82,8 +84,17 @@ public function match(PsrRequest $request): RouteResult
// Must inject routes prior to matching.
$this->injectRoutes();

$laminasRequest = Psr7ServerRequest::toLaminas($request, true);
$match = $this->laminasRouter->match($laminasRequest);
try {
$laminasRequest = Psr7ServerRequest::toLaminas($request, true);
} catch (InvalidArgumentException $e) {
$previous = $e->getPrevious();
if ($previous instanceof InvalidUriPartException) {
return RouteResult::fromRouteFailure(null);
}
throw $e;
}

$match = $this->laminasRouter->match($laminasRequest);

if (null === $match) {
// No route matched at all; to indicate that it's not due to the
Expand Down
18 changes: 18 additions & 0 deletions test/LaminasRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Fig\Http\Message\RequestMethodInterface as RequestMethod;
use Laminas\Diactoros\ServerRequest;
use Laminas\Diactoros\Uri;
use Laminas\Http\Request as LaminasRequest;
use Laminas\I18n\Translator\TranslatorInterface;
use Laminas\Psr7Bridge\Psr7ServerRequest;
Expand Down Expand Up @@ -560,4 +561,21 @@ public function testGenerateUriRaisesExceptionForNotFoundRoute(): void
$this->expectExceptionMessage('route not found');
$router->generateUri('foo');
}

public function testMatchReturnsRouteFailureOnFailureToConvertPsr7Request(): void
{
$route = new Route('/some/path', $this->getMiddleware(), [RequestMethod::METHOD_GET], 'test');

$router = new LaminasRouter();
$router->addRoute($route);

$serverRequest = (new ServerRequest())
->withUri(new Uri('https://${ip}/some/path'))
->withHeader('Host', '${ip}')
->withHeader('Accept', 'application/json');

$result = $router->match($serverRequest);

$this->assertTrue($result->isFailure());
}
}