From d5b1bb1ab604a4d2f00e5c983adda3778c7c304e Mon Sep 17 00:00:00 2001 From: Kieran Brahney Date: Thu, 30 Jul 2020 14:20:40 +0100 Subject: [PATCH 1/2] Copied #30101 --- src/Concerns/MakesHttpRequests.php | 123 ++++++++++++++++++++++++++- tests/Unit/MakesHttpRequestsTest.php | 21 +++++ 2 files changed, 140 insertions(+), 4 deletions(-) diff --git a/src/Concerns/MakesHttpRequests.php b/src/Concerns/MakesHttpRequests.php index 7ba714c..51b9b13 100644 --- a/src/Concerns/MakesHttpRequests.php +++ b/src/Concerns/MakesHttpRequests.php @@ -4,6 +4,7 @@ use Closure; use Illuminate\Contracts\View\View; +use Illuminate\Cookie\CookieValuePrefix; use Illuminate\Http\Request; use Illuminate\Http\UploadedFile; use Illuminate\Support\Arr; @@ -38,6 +39,27 @@ trait MakesHttpRequests */ protected $serverVariables = []; + /** + * Additional cookies for the request. + * + * @var array + */ + protected $defaultCookies = []; + + /** + * Additional cookies will not be encrypted for the request. + * + * @var array + */ + protected $unencryptedCookies = []; + + /** + * Indicates whether cookies should be encrypted. + * + * @var bool + */ + protected $encryptCookies = true; + /** * Disable middleware for the test. * @@ -65,6 +87,72 @@ public function handle($request, $next) return $this; } + /** + * Define additional cookies to be sent with the request. + * + * @param array $cookies + * @return $this + */ + public function withCookies(array $cookies) + { + $this->defaultCookies = array_merge($this->defaultCookies, $cookies); + + return $this; + } + + /** + * Add a cookie to be sent with the request. + * + * @param string $name + * @param string $value + * @return $this + */ + public function withCookie(string $name, string $value) + { + $this->defaultCookies[$name] = $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; + } + + /** + * Disable automatic encryption of cookie values. + * + * @return $this + */ + public function disableCookieEncryption() + { + $this->encryptCookies = false; + + return $this; + } + /** * Visit the given URI with a JSON request. * @@ -77,6 +165,7 @@ public function handle($request, $next) public function json($method, $uri, array $data = [], array $headers = []) { $files = $this->extractFilesFromDataArray($data); + $cookies = $this->prepareCookiesForRequest(); $content = json_encode($data); @@ -124,6 +213,7 @@ protected function extractFilesFromDataArray(&$data) public function get($uri, array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); $this->call('GET', $uri, [], [], [], $server); @@ -153,8 +243,9 @@ public function getJson($uri, array $headers = []) public function post($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - $this->call('POST', $uri, $data, [], [], $server); + $this->call('POST', $uri, $data, $cookies, [], $server); return $this; } @@ -183,8 +274,9 @@ public function postJson($uri, array $data = [], array $headers = []) public function put($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - $this->call('PUT', $uri, $data, [], [], $server); + $this->call('PUT', $uri, $data, $cookies, [], $server); return $this; } @@ -213,8 +305,9 @@ public function putJson($uri, array $data = [], array $headers = []) public function patch($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - $this->call('PATCH', $uri, $data, [], [], $server); + $this->call('PATCH', $uri, $data, $cookies, [], $server); return $this; } @@ -243,8 +336,9 @@ public function patchJson($uri, array $data = [], array $headers = []) public function delete($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - $this->call('DELETE', $uri, $data, [], [], $server); + $this->call('DELETE', $uri, $data, $cookies, [], $server); return $this; } @@ -706,6 +800,27 @@ protected function transformHeadersToServerVars(array $headers) return $server; } + /** + * If enabled, encrypt cookie values for request. + * + * @return array + */ + protected function prepareCookiesForRequest() + { + if (! $this->encryptCookies) { + return array_merge($this->defaultCookies, $this->unencryptedCookies); + } + + $appKey = config('app.key'); + if (Str::startsWith($appKey, 'base64:')) { + $appKey = base64_decode(substr($appKey, 7)); + } + + return collect($this->defaultCookies)->map(function ($value, $key) use ($appKey) { + return encrypt(CookieValuePrefix::create($key, $appKey).$value, false); + })->merge($this->unencryptedCookies)->all(); + } + /** * Filter the given array of files, removing any empty values. * diff --git a/tests/Unit/MakesHttpRequestsTest.php b/tests/Unit/MakesHttpRequestsTest.php index aced6ee..339aee0 100644 --- a/tests/Unit/MakesHttpRequestsTest.php +++ b/tests/Unit/MakesHttpRequestsTest.php @@ -120,4 +120,25 @@ public function getStatusCode() }; $this->assertResponseStatus(404); } + + public function testWithCookieSetCookie() + { + $this->withCookie('foo', 'bar'); + + $this->assertCount(1, $this->defaultCookies); + $this->assertSame('bar', $this->defaultCookies['foo']); + } + + public function testWithCookiesSetsCookiesAndOverwritesPreviousValues() + { + $this->withCookie('foo', 'bar'); + $this->withCookies([ + 'foo' => 'baz', + 'new-cookie' => 'new-value', + ]); + + $this->assertCount(2, $this->defaultCookies); + $this->assertSame('baz', $this->defaultCookies['foo']); + $this->assertSame('new-value', $this->defaultCookies['new-cookie']); + } } From e5d938e80c359060080343761f2e1ec43f14a7e1 Mon Sep 17 00:00:00 2001 From: Kieran Brahney Date: Thu, 30 Jul 2020 14:28:25 +0100 Subject: [PATCH 2/2] Updated key handling --- src/Concerns/MakesHttpRequests.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Concerns/MakesHttpRequests.php b/src/Concerns/MakesHttpRequests.php index 51b9b13..8e7bd32 100644 --- a/src/Concerns/MakesHttpRequests.php +++ b/src/Concerns/MakesHttpRequests.php @@ -811,13 +811,8 @@ protected function prepareCookiesForRequest() return array_merge($this->defaultCookies, $this->unencryptedCookies); } - $appKey = config('app.key'); - if (Str::startsWith($appKey, 'base64:')) { - $appKey = base64_decode(substr($appKey, 7)); - } - - return collect($this->defaultCookies)->map(function ($value, $key) use ($appKey) { - return encrypt(CookieValuePrefix::create($key, $appKey).$value, false); + return collect($this->defaultCookies)->map(function ($value, $key) { + return encrypt(CookieValuePrefix::create($key, app('encrypter')->getKey()).$value, false); })->merge($this->unencryptedCookies)->all(); }