Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Fix testing with unencrypted cookies #31390

Merged
merged 4 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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();
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Foundation/Testing/Concerns/MakesHttpRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down