Skip to content
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
6 changes: 4 additions & 2 deletions system/HTTP/Exceptions/RedirectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@
*/
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;

/**
* @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(
Expand Down
11 changes: 11 additions & 0 deletions tests/system/HTTP/RedirectExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading