Skip to content

Commit 2b463dd

Browse files
Add new HTTP status assertions (#46841)
* Adds gone status check * Adds service unavailable status check * Adds internal server error status check
1 parent a3cfd2d commit 2b463dd

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

src/Illuminate/Testing/Concerns/AssertsStatusCodes.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ public function assertConflict()
141141
return $this->assertStatus(409);
142142
}
143143

144+
/**
145+
* Assert that the response has a 410 "Gone" status code.
146+
*
147+
* @return $this
148+
*/
149+
public function assertGone()
150+
{
151+
return $this->assertStatus(410);
152+
}
153+
144154
/**
145155
* Assert that the response has a 415 "Unsupported Media Type" status code.
146156
*
@@ -170,4 +180,24 @@ public function assertTooManyRequests()
170180
{
171181
return $this->assertStatus(429);
172182
}
183+
184+
/**
185+
* Assert that the response has a 500 "Internal Server Error" status code.
186+
*
187+
* @return $this
188+
*/
189+
public function assertInternalServerError()
190+
{
191+
return $this->assertStatus(500);
192+
}
193+
194+
/**
195+
* Assert that the response has a 503 "Service Unavailable" status code.
196+
*
197+
* @return $this
198+
*/
199+
public function assertServiceUnavailable()
200+
{
201+
return $this->assertStatus(503);
202+
}
173203
}

tests/Testing/TestResponseTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,24 @@ public function testAssertConflict()
707707
$this->fail();
708708
}
709709

710+
public function testAssertGone()
711+
{
712+
$response = TestResponse::fromBaseResponse(
713+
(new Response)->setStatusCode(Response::HTTP_GONE)
714+
);
715+
716+
$response->assertGone();
717+
718+
$response = TestResponse::fromBaseResponse(
719+
(new Response)->setStatusCode(Response::HTTP_OK)
720+
);
721+
722+
$this->expectException(AssertionFailedError::class);
723+
$this->expectExceptionMessage("Expected response status code [410] but received 200.\nFailed asserting that 410 is identical to 200.");
724+
725+
$response->assertGone();
726+
}
727+
710728
public function testAssertTooManyRequests()
711729
{
712730
$response = TestResponse::fromBaseResponse(
@@ -773,6 +791,42 @@ public function testAssertServerError()
773791
$response->assertServerError();
774792
}
775793

794+
public function testAssertInternalServerError()
795+
{
796+
$response = TestResponse::fromBaseResponse(
797+
(new Response)->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR)
798+
);
799+
800+
$response->assertInternalServerError();
801+
802+
$response = TestResponse::fromBaseResponse(
803+
(new Response)->setStatusCode(Response::HTTP_OK)
804+
);
805+
806+
$this->expectException(AssertionFailedError::class);
807+
$this->expectExceptionMessage("Expected response status code [500] but received 200.\nFailed asserting that 500 is identical to 200.");
808+
809+
$response->assertInternalServerError();
810+
}
811+
812+
public function testAssertServiceUnavailable()
813+
{
814+
$response = TestResponse::fromBaseResponse(
815+
(new Response)->setStatusCode(Response::HTTP_SERVICE_UNAVAILABLE)
816+
);
817+
818+
$response->assertServiceUnavailable();
819+
820+
$response = TestResponse::fromBaseResponse(
821+
(new Response)->setStatusCode(Response::HTTP_OK)
822+
);
823+
824+
$this->expectException(AssertionFailedError::class);
825+
$this->expectExceptionMessage("Expected response status code [503] but received 200.\nFailed asserting that 503 is identical to 200.");
826+
827+
$response->assertServiceUnavailable();
828+
}
829+
776830
public function testAssertNoContentAsserts204StatusCodeByDefault()
777831
{
778832
$statusCode = 500;

0 commit comments

Comments
 (0)