Skip to content

Commit

Permalink
add 404 helpers to Response
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Jul 8, 2022
1 parent eb51a18 commit 7b2fe96
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
22 changes: 22 additions & 0 deletions src/Illuminate/Auth/Access/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public static function denyWithStatus($status, $message = null, $code = null)
return static::deny($message, $code)->withStatus($status);
}

/**
* Create a new "deny" Response with a 404 HTTP status code.
*
* @param string|null $message
* @param mixed $code
* @return \Illuminate\Auth\Access\Response
*/
public static function denyAsNotFound($message = null, $code = null)
{
return static::denyWithStatus(404, $message, $code);
}

/**
* Determine if the response was allowed.
*
Expand Down Expand Up @@ -157,6 +169,16 @@ public function withStatus($status)
return $this;
}

/**
* Set the HTTP response status code to 404.
*
* @return $this
*/
public function asNotFound()
{
return $this->withStatus(404);
}

/**
* Get the HTTP status code.
*
Expand Down
32 changes: 29 additions & 3 deletions tests/Auth/AuthAccessResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,56 @@ public function testDenyMethodWithNoMessageReturnsNull()
public function testItSetsEmptyStatusOnExceptionWhenAuthorizing()
{
try {
Response::deny()->authorize();
Response::deny('foo', 3)->authorize();
$this->fail();
} catch (AuthorizationException $e) {
$this->assertNull($e->status());
$this->assertFalse($e->hasStatus());
$this->assertSame('foo', $e->getMessage());
$this->assertSame(3, $e->getCode());
}
}

public function testItSetsStatusOnExceptionWhenAuthorizing()
{
try {
Response::deny()->withStatus(404)->authorize();
Response::deny('foo', 3)->withStatus(418)->authorize();
$this->fail();
} catch (AuthorizationException $e) {
$this->assertSame(418, $e->status());
$this->assertTrue($e->hasStatus());
$this->assertSame('foo', $e->getMessage());
$this->assertSame(3, $e->getCode());
}

try {
Response::deny('foo', 3)->asNotFound()->authorize();
$this->fail();
} catch (AuthorizationException $e) {
$this->assertSame(404, $e->status());
$this->assertTrue($e->hasStatus());
$this->assertSame('foo', $e->getMessage());
$this->assertSame(3, $e->getCode());
}

try {
Response::denyWithStatus(444, 'foo', 3)->authorize();
$this->fail();
} catch (AuthorizationException $e) {
$this->assertSame(444, $e->status());
$this->assertTrue($e->hasStatus());
$this->assertSame('foo', $e->getMessage());
$this->assertSame(3, $e->getCode());
}

try {
Response::denyWithStatus(404)->authorize();
Response::denyAsNotFound('foo', 3)->authorize();
$this->fail();
} catch (AuthorizationException $e) {
$this->assertSame(404, $e->status());
$this->assertTrue($e->hasStatus());
$this->assertSame('foo', $e->getMessage());
$this->assertSame(3, $e->getCode());
}
}

Expand Down

0 comments on commit 7b2fe96

Please sign in to comment.