diff --git a/system/HTTP/Exceptions/RedirectException.php b/system/HTTP/Exceptions/RedirectException.php index 5ec01110a4a7..2f26013876b2 100644 --- a/system/HTTP/Exceptions/RedirectException.php +++ b/system/HTTP/Exceptions/RedirectException.php @@ -26,10 +26,12 @@ */ class RedirectException extends RuntimeException implements ExceptionInterface, ResponsableInterface, HTTPExceptionInterface { + private const DEFAULT_STATUS_CODE = 302; + /** * Status code applied to a Response whose status is outside the 301-308 range. */ - protected int $defaultStatusCode = 302; + protected int $defaultStatusCode = self::DEFAULT_STATUS_CODE; protected ?ResponseInterface $response = null; @@ -37,7 +39,7 @@ class RedirectException extends RuntimeException implements ExceptionInterface, * @param ResponseInterface|string $message Response object or a string containing a relative URI. * @param int $code HTTP status code to redirect if $message is a string. */ - public function __construct($message = '', int $code = 0, ?Throwable $previous = null) + public function __construct($message = '', int $code = self::DEFAULT_STATUS_CODE, ?Throwable $previous = null) { if (! is_string($message) && ! $message instanceof ResponseInterface) { throw new InvalidArgumentException( diff --git a/tests/system/HTTP/RedirectExceptionTest.php b/tests/system/HTTP/RedirectExceptionTest.php index a533f3a8c011..4a787830c1f4 100644 --- a/tests/system/HTTP/RedirectExceptionTest.php +++ b/tests/system/HTTP/RedirectExceptionTest.php @@ -88,6 +88,17 @@ public function testResponseWithoutStatusCodeUsesSubclassDefault(): void $this->assertSame(307, $response->getStatusCode()); } + public function testStringMessageWithoutExplicitCodeDefaultsTo302(): void + { + $exception = new RedirectException('relative/uri'); + + $this->assertSame(302, $exception->getCode()); + + $response = $exception->getResponse(); + + $this->assertSame(302, $response->getStatusCode()); + } + public function testLoggingLocationHeader(): void { Time::setTestNow('2023-11-25 12:00:00');