Skip to content

Commit

Permalink
Add new HTTP status assertions (laravel#46841)
Browse files Browse the repository at this point in the history
* Adds gone status check

* Adds service unavailable status check

* Adds internal server error status check
  • Loading branch information
miranalmehrab committed Apr 20, 2023
1 parent a3cfd2d commit 2b463dd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Illuminate/Testing/Concerns/AssertsStatusCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ public function assertConflict()
return $this->assertStatus(409);
}

/**
* Assert that the response has a 410 "Gone" status code.
*
* @return $this
*/
public function assertGone()
{
return $this->assertStatus(410);
}

/**
* Assert that the response has a 415 "Unsupported Media Type" status code.
*
Expand Down Expand Up @@ -170,4 +180,24 @@ public function assertTooManyRequests()
{
return $this->assertStatus(429);
}

/**
* Assert that the response has a 500 "Internal Server Error" status code.
*
* @return $this
*/
public function assertInternalServerError()
{
return $this->assertStatus(500);
}

/**
* Assert that the response has a 503 "Service Unavailable" status code.
*
* @return $this
*/
public function assertServiceUnavailable()
{
return $this->assertStatus(503);
}
}
54 changes: 54 additions & 0 deletions tests/Testing/TestResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,24 @@ public function testAssertConflict()
$this->fail();
}

public function testAssertGone()
{
$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_GONE)
);

$response->assertGone();

$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_OK)
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Expected response status code [410] but received 200.\nFailed asserting that 410 is identical to 200.");

$response->assertGone();
}

public function testAssertTooManyRequests()
{
$response = TestResponse::fromBaseResponse(
Expand Down Expand Up @@ -773,6 +791,42 @@ public function testAssertServerError()
$response->assertServerError();
}

public function testAssertInternalServerError()
{
$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR)
);

$response->assertInternalServerError();

$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_OK)
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Expected response status code [500] but received 200.\nFailed asserting that 500 is identical to 200.");

$response->assertInternalServerError();
}

public function testAssertServiceUnavailable()
{
$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_SERVICE_UNAVAILABLE)
);

$response->assertServiceUnavailable();

$response = TestResponse::fromBaseResponse(
(new Response)->setStatusCode(Response::HTTP_OK)
);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage("Expected response status code [503] but received 200.\nFailed asserting that 503 is identical to 200.");

$response->assertServiceUnavailable();
}

public function testAssertNoContentAsserts204StatusCodeByDefault()
{
$statusCode = 500;
Expand Down

0 comments on commit 2b463dd

Please sign in to comment.