Skip to content

Commit de4d772

Browse files
authored
Merge pull request #15 from jonnitto/main
Fix: Allow error codes above 399 from Settings.yaml
2 parents fca5d1a + d13dd9d commit de4d772

3 files changed

Lines changed: 57 additions & 9 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flowpack\SeoRouting\Exceptions\Http;
6+
7+
use Throwable;
8+
9+
/**
10+
* An exception where the HTTP status code can be set.
11+
*/
12+
class Exception extends \Neos\Flow\Http\Exception
13+
{
14+
public function __construct(int $statusCode, string $message = "", int $code = 0, ?Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
18+
$this->statusCode = $statusCode;
19+
}
20+
}

Classes/RoutingMiddleware.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Flowpack\SeoRouting;
66

77
use Flowpack\SeoRouting\Enum\TrailingSlashModeEnum;
8+
use Flowpack\SeoRouting\Exceptions\Http\Exception as HttpException;
89
use Flowpack\SeoRouting\Helper\BlocklistHelper;
910
use Flowpack\SeoRouting\Helper\ConfigurationHelper;
1011
use Flowpack\SeoRouting\Helper\LowerCaseHelper;
@@ -33,6 +34,9 @@ class RoutingMiddleware implements MiddlewareInterface
3334
#[Flow\Inject]
3435
protected LowerCaseHelper $lowerCaseHelper;
3536

37+
/**
38+
* @throws HttpException
39+
*/
3640
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
3741
{
3842
$isTrailingSlashEnabled = $this->configurationHelper->isTrailingSlashEnabled();
@@ -61,17 +65,19 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
6165
$uri = $this->lowerCaseHelper->convertPathToLowerCase($uri);
6266
}
6367

64-
if ($uri->getPath() === $oldPath) {
65-
return $handler->handle($request);
68+
$response = $handler->handle($request);
69+
70+
if ($uri->getPath() === $oldPath || $response->getStatusCode() >= 400) {
71+
return $response;
6672
}
6773

68-
$response = $handler->handle($request);
74+
$statusCode = $this->configurationHelper->getStatusCode();
6975

70-
if ($response->getStatusCode() >= 400) {
71-
return $response;
76+
if ($statusCode >= 400) {
77+
throw new HttpException($statusCode);
7278
}
7379

74-
return $this->responseFactory->createResponse($this->configurationHelper->getStatusCode())
80+
return $this->responseFactory->createResponse($statusCode)
7581
->withAddedHeader('Location', (string)$uri);
7682
}
7783
}

Tests/Unit/RoutingMiddlewareTest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Flowpack\SeoRouting\Tests\Unit;
66

77
use Flowpack\SeoRouting\Enum\TrailingSlashModeEnum;
8+
use Flowpack\SeoRouting\Exceptions\Http\Exception as HttpException;
89
use Flowpack\SeoRouting\Helper\BlocklistHelper;
910
use Flowpack\SeoRouting\Helper\ConfigurationHelper;
1011
use Flowpack\SeoRouting\Helper\LowerCaseHelper;
@@ -21,8 +22,10 @@
2122
use Psr\Http\Message\ServerRequestInterface;
2223
use Psr\Http\Server\RequestHandlerInterface;
2324
use ReflectionClass;
25+
use Throwable;
2426

2527
#[CoversClass(RoutingMiddleware::class)]
28+
#[CoversClass(HttpException::class)]
2629
class RoutingMiddlewareTest extends TestCase
2730
{
2831
private readonly RoutingMiddleware $routingMiddleware;
@@ -72,6 +75,11 @@ protected function setUp(): void
7275
}
7376

7477

78+
/**
79+
* @param class-string<Throwable>|null $expectedException
80+
* @throws Exception
81+
* @throws HttpException
82+
*/
7583
#[DataProvider('urlsDataProvider')]
7684
public function testProcessShouldHandleUrlsCorrectly(
7785
string $originalUrl,
@@ -82,6 +90,7 @@ public function testProcessShouldHandleUrlsCorrectly(
8290
int $statusCode,
8391
TrailingSlashModeEnum $trailingSlashMode,
8492
int $handlerStatusCode = 200,
93+
?string $expectedException = null,
8594
): void {
8695
$originalUri = new Uri($originalUrl);
8796
$expectedUri = new Uri($expectedUrl);
@@ -98,8 +107,12 @@ public function testProcessShouldHandleUrlsCorrectly(
98107
$this->requestMock->expects($this->once())->method('getUri')->willReturn($originalUri);
99108

100109
$pathChanged = $originalUrl !== $expectedUrl;
101-
102-
if (!$pathChanged) {
110+
if (is_string($expectedException)) {
111+
$this->expectException($expectedException);
112+
$this->responseFactoryMock->expects($this->never())->method('createResponse');
113+
$this->routingMiddleware->process($this->requestMock, $this->requestHandlerMock);
114+
return;
115+
} elseif (!$pathChanged) {
103116
$this->requestHandlerMock->method('handle')->willReturn($this->responseMock);
104117
} elseif ($handlerStatusCode >= 400) {
105118
$this->responseMock->method('getStatusCode')->willReturn($handlerStatusCode);
@@ -122,7 +135,6 @@ public function testProcessShouldHandleUrlsCorrectly(
122135
->with('Location', (string)$expectedUri)
123136
->willReturnSelf();
124137
}
125-
126138
self::assertSame(
127139
$this->responseMock,
128140
$this->routingMiddleware->process($this->requestMock, $this->requestHandlerMock)
@@ -190,6 +202,16 @@ public static function urlsDataProvider(): array
190202
'trailingSlashMode' => TrailingSlashModeEnum::ADD,
191203
'handlerStatusCode' => 404,
192204
],
205+
[
206+
'originalUrl' => 'https://local.dev',
207+
'expectedUrl' => 'https://local.dev/',
208+
'isTrailingSlashEnabledResult' => true,
209+
'isToLowerCaseEnabledResult' => false,
210+
'isUriInBlocklistResult' => false,
211+
'statusCode' => 404,
212+
'trailingSlashMode' => TrailingSlashModeEnum::ADD,
213+
'expectedException' => HttpException::class
214+
],
193215
];
194216
}
195217
}

0 commit comments

Comments
 (0)