Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add new HTTP status assertions #46841

Merged
merged 3 commits into from
Apr 20, 2023
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
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