Skip to content

Commit

Permalink
Merge pull request #137 from bytestream/feat-cookies
Browse files Browse the repository at this point in the history
[5.x] Copied #30101
  • Loading branch information
taylorotwell authored Jul 30, 2020
2 parents 348757f + e5d938e commit c202f99
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 4 deletions.
118 changes: 114 additions & 4 deletions src/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -706,6 +800,22 @@ 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);
}

return collect($this->defaultCookies)->map(function ($value, $key) {
return encrypt(CookieValuePrefix::create($key, app('encrypter')->getKey()).$value, false);
})->merge($this->unencryptedCookies)->all();
}

/**
* Filter the given array of files, removing any empty values.
*
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

0 comments on commit c202f99

Please sign in to comment.