Skip to content

Commit b536225

Browse files
committed
fix: RedirectException no longer defaults to a 302 status code
1 parent 8a5d3cb commit b536225

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎system/HTTP/Exceptions/RedirectException.php‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@
2626
*/
2727
class RedirectException extends RuntimeException implements ExceptionInterface, ResponsableInterface, HTTPExceptionInterface
2828
{
29+
private const DEFAULT_STATUS_CODE = 302;
30+
2931
/**
3032
* Status code applied to a Response whose status is outside the 301-308 range.
3133
*/
32-
protected int $defaultStatusCode = 302;
34+
protected int $defaultStatusCode = self::DEFAULT_STATUS_CODE;
3335

3436
protected ?ResponseInterface $response = null;
3537

3638
/**
3739
* @param ResponseInterface|string $message Response object or a string containing a relative URI.
3840
* @param int $code HTTP status code to redirect if $message is a string.
3941
*/
40-
public function __construct($message = '', int $code = 0, ?Throwable $previous = null)
42+
public function __construct($message = '', int $code = self::DEFAULT_STATUS_CODE, ?Throwable $previous = null)
4143
{
4244
if (! is_string($message) && ! $message instanceof ResponseInterface) {
4345
throw new InvalidArgumentException(

‎tests/system/HTTP/RedirectExceptionTest.php‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ public function testResponseWithoutStatusCodeUsesSubclassDefault(): void
8888
$this->assertSame(307, $response->getStatusCode());
8989
}
9090

91+
public function testStringMessageWithoutExplicitCodeDefaultsTo302(): void
92+
{
93+
$exception = new RedirectException('relative/uri');
94+
95+
$this->assertSame(302, $exception->getCode());
96+
97+
$response = $exception->getResponse();
98+
99+
$this->assertSame(302, $response->getStatusCode());
100+
}
101+
91102
public function testLoggingLocationHeader(): void
92103
{
93104
Time::setTestNow('2023-11-25 12:00:00');

0 commit comments

Comments
 (0)