From 6ce800fdafdb0ed17ae62c65a78f8ced9b10003d Mon Sep 17 00:00:00 2001 From: Milwad Khosravi <98118400+milwad-dev@users.noreply.github.com> Date: Fri, 9 Aug 2024 15:32:01 +0330 Subject: [PATCH] [11.x] Add `withoutHeaders` method (#52435) * add `testFromRemoveHeader` * add `withoutHeaders` * add `testFromRemoveHeaders` * Update MakesHttpRequestsTest.php --- .../Testing/Concerns/MakesHttpRequests.php | 15 +++++++++++ .../Concerns/MakesHttpRequestsTest.php | 27 +++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php index 85eabee130b6..c24799df0b45 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php +++ b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php @@ -103,6 +103,21 @@ public function withoutHeader(string $name) return $this; } + /** + * Remove headers from the request. + * + * @param array $headers + * @return $this + */ + public function withoutHeaders(array $headers) + { + foreach ($headers as $name) { + $this->withoutHeader($name); + } + + return $this; + } + /** * Add an authorization token for the request. * diff --git a/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php b/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php index e848cf378a61..3a5332fdf20e 100644 --- a/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php +++ b/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php @@ -30,6 +30,33 @@ public function testFromRouteSetsHeaderAndSession() $this->assertSame('http://localhost/previous/url', $this->app['session']->previousUrl()); } + public function testFromRemoveHeader() + { + $this->withHeader('name', 'Milwad')->from('previous/url'); + + $this->assertEquals('Milwad', $this->defaultHeaders['name']); + + $this->withoutHeader('name')->from('previous/url'); + + $this->assertArrayNotHasKey('name', $this->defaultHeaders); + } + + public function testFromRemoveHeaders() + { + $this->withHeaders([ + 'name' => 'Milwad', + 'foo' => 'bar', + ])->from('previous/url'); + + $this->assertEquals('Milwad', $this->defaultHeaders['name']); + $this->assertEquals('bar', $this->defaultHeaders['foo']); + + $this->withoutHeaders(['name', 'foo'])->from('previous/url'); + + $this->assertArrayNotHasKey('name', $this->defaultHeaders); + $this->assertArrayNotHasKey('foo', $this->defaultHeaders); + } + public function testWithTokenSetsAuthorizationHeader() { $this->withToken('foobar');