diff --git a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php index 7dc6bb986a3a..149edd6c903d 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php +++ b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php @@ -25,6 +25,13 @@ trait MakesHttpRequests */ protected $defaultCookies = []; + /** + * Additional cookies will not be encrypted for the request. + * + * @var array + */ + protected $unencryptedCookies = []; + /** * Additional server variables for the request. * @@ -172,6 +179,33 @@ public function withCookie(string $name, string $value) return $this; } + /** + * Define additional cookies will not be encrypted before sending with the request. + * + * @param array $cookies + * @return $this + */ + public function withUnencryptedCookies(array $cookies) + { + $this->unencryptedCookies = array_merge($this->unencryptedCookies, $cookies); + + return $this; + } + + /** + * Add a cookie will not be encrypted before sending with the request. + * + * @param string $name + * @param string $value + * @return $this + */ + public function withUnencryptedCookie(string $name, string $value) + { + $this->unencryptedCookies[$name] = $value; + + return $this; + } + /** * Automatically follow any redirects returned from the response. * @@ -527,12 +561,12 @@ protected function extractFilesFromDataArray(&$data) protected function prepareCookiesForRequest() { if (! $this->encryptCookies) { - return $this->defaultCookies; + return array_merge($this->defaultCookies, $this->unencryptedCookies); } return collect($this->defaultCookies)->map(function ($value) { return encrypt($value, false); - })->all(); + })->merge($this->unencryptedCookies)->all(); } /** diff --git a/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php b/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php index e0c1533804bb..f1f696832ab1 100644 --- a/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php +++ b/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php @@ -73,6 +73,27 @@ public function testWithCookiesSetsCookiesAndOverwritesPreviousValues() $this->assertSame('baz', $this->defaultCookies['foo']); $this->assertSame('new-value', $this->defaultCookies['new-cookie']); } + + public function testWithUnencryptedCookieSetCookie() + { + $this->withUnencryptedCookie('foo', 'bar'); + + $this->assertCount(1, $this->unencryptedCookies); + $this->assertSame('bar', $this->unencryptedCookies['foo']); + } + + public function testWithUnencryptedCookiesSetsCookiesAndOverwritesPreviousValues() + { + $this->withUnencryptedCookie('foo', 'bar'); + $this->withUnencryptedCookies([ + 'foo' => 'baz', + 'new-cookie' => 'new-value', + ]); + + $this->assertCount(2, $this->unencryptedCookies); + $this->assertSame('baz', $this->unencryptedCookies['foo']); + $this->assertSame('new-value', $this->unencryptedCookies['new-cookie']); + } } class MyMiddleware