diff --git a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php index 61c3ec0869b0..eb7a5a438801 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php +++ b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php @@ -11,6 +11,13 @@ trait MakesHttpRequests { + /** + * Additional cookies for the request. + * + * @var array + */ + protected $defaultCookies = []; + /** * Additional headers for the request. * @@ -32,6 +39,13 @@ trait MakesHttpRequests */ protected $followRedirects = false; + /** + * Indicates whether cookies should be encrypted. + * + * @var bool + */ + protected $encryptCookies = true; + /** * Define additional headers to be sent with the request. * @@ -131,6 +145,33 @@ public function withMiddleware($middleware = null) 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; + } + /** * Automatically follow any redirects returned from the response. * @@ -143,6 +184,18 @@ public function followingRedirects() return $this; } + /** + * Disable automatic encryption of cookie values. + * + * @return $this + */ + public function disableCookieEncryption() + { + $this->encryptCookies = false; + + return $this; + } + /** * Set the referer header and previous URL session value in order to simulate a previous request. * @@ -166,8 +219,9 @@ public function from(string $url) public function get($uri, array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - return $this->call('GET', $uri, [], [], [], $server); + return $this->call('GET', $uri, [], $cookies, [], $server); } /** @@ -193,8 +247,9 @@ public function getJson($uri, array $headers = []) public function post($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - return $this->call('POST', $uri, $data, [], [], $server); + return $this->call('POST', $uri, $data, $cookies, [], $server); } /** @@ -221,8 +276,9 @@ public function postJson($uri, array $data = [], array $headers = []) public function put($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - return $this->call('PUT', $uri, $data, [], [], $server); + return $this->call('PUT', $uri, $data, $cookies, [], $server); } /** @@ -249,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(); - return $this->call('PATCH', $uri, $data, [], [], $server); + return $this->call('PATCH', $uri, $data, $cookies, [], $server); } /** @@ -277,8 +334,9 @@ public function patchJson($uri, array $data = [], array $headers = []) public function delete($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - return $this->call('DELETE', $uri, $data, [], [], $server); + return $this->call('DELETE', $uri, $data, $cookies, [], $server); } /** @@ -305,8 +363,9 @@ public function deleteJson($uri, array $data = [], array $headers = []) public function options($uri, array $data = [], array $headers = []) { $server = $this->transformHeadersToServerVars($headers); + $cookies = $this->prepareCookiesForRequest(); - return $this->call('OPTIONS', $uri, $data, [], [], $server); + return $this->call('OPTIONS', $uri, $data, $cookies, [], $server); } /** @@ -460,6 +519,22 @@ protected function extractFilesFromDataArray(&$data) return $files; } + /** + * If enabled, encrypt cookie values for request. + * + * @return array + */ + protected function prepareCookiesForRequest() + { + if (!$this->encryptCookies) { + return $this->defaultCookies; + } + + return collect($this->defaultCookies)->map(function ($value) { + return encrypt($value, false); + })->all(); + } + /** * Follow a redirect chain until a non-redirect is received. * diff --git a/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php b/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php index 26d4b5092d9a..9725679ecacd 100644 --- a/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php +++ b/tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php @@ -52,6 +52,27 @@ public function testWithoutAndWithMiddlewareWithParameter() $this->app->make(MyMiddleware::class)->handle('foo', $next) ); } + + 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']); + } } class MyMiddleware