From ece34f1670c549f07922bf06d9d7579dfa844d0e Mon Sep 17 00:00:00 2001 From: Lucas Michot Date: Wed, 13 Sep 2023 16:33:22 +0200 Subject: [PATCH] Add notModified method to HTTP client (#48379) --- .../Client/Concerns/DeterminesStatusCode.php | 10 ++++++++++ tests/Http/HttpClientTest.php | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/Illuminate/Http/Client/Concerns/DeterminesStatusCode.php b/src/Illuminate/Http/Client/Concerns/DeterminesStatusCode.php index ab9132006ecf..29a33c07c7c1 100644 --- a/src/Illuminate/Http/Client/Concerns/DeterminesStatusCode.php +++ b/src/Illuminate/Http/Client/Concerns/DeterminesStatusCode.php @@ -65,6 +65,16 @@ public function found() return $this->status() === 302; } + /** + * Determine if the response code was a 304 "Not Modified" response. + * + * @return bool + */ + public function notModified() + { + return $this->status() === 304; + } + /** * Determine if the response was a 400 "Bad Request" response. * diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 6d8ab2867ee4..c2056867c970 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -52,6 +52,8 @@ protected function setUp(): void protected function tearDown(): void { m::close(); + + parent::tearDown(); } public function testStubbedResponsesAreReturnedAfterFaking() @@ -133,6 +135,20 @@ public function testFoundRequest() $this->assertFalse($response->found()); } + public function testNotModifiedRequest(): void + { + $this->factory->fake([ + 'vapor.laravel.com' => $this->factory::response('', HttpResponse::HTTP_NOT_MODIFIED), + 'forge.laravel.com' => $this->factory::response('', HttpResponse::HTTP_OK), + ]); + + $response = $this->factory->post('https://vapor.laravel.com'); + $this->assertTrue($response->notModified()); + + $response = $this->factory->post('https://forge.laravel.com'); + $this->assertFalse($response->notModified()); + } + public function testBadRequestRequest() { $this->factory->fake([