Skip to content

Commit

Permalink
[6.x] Fix testing with unencrypted cookies (#31390)
Browse files Browse the repository at this point in the history
* fix testing with unencrypted cookies

* remove extra blank line

* fix test failed

* make method unencryptedCookie to prevent making breaking change
  • Loading branch information
gentcys authored Feb 10, 2020
1 parent 456d018 commit 6088728
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
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

0 comments on commit 6088728

Please sign in to comment.