Skip to content

Commit

Permalink
Move Cookie::toArrayClient() to Response::convertCookie()
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Pustułka committed Apr 21, 2017
1 parent c2876db commit 3db767b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 61 deletions.
18 changes: 4 additions & 14 deletions src/Http/Client/Response.php
Expand Up @@ -446,7 +446,7 @@ public function getCookieData($name)

$cookie = $this->cookies->get($name);

return $this->toArrayClient($cookie);
return $this->convertCookie($cookie);
}

/**
Expand All @@ -458,26 +458,16 @@ public function getCookieData($name)
* @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
* @return array
*/
protected function toArrayClient(CookieInterface $cookie)
public function convertCookie(CookieInterface $cookie)
{
if ($cookie instanceof Cookie) {
return $cookie->toArrayClient();
}

if ($cookie->getExpiry()) {
$expires = $cookie->getExpiry()->format(Cookie::EXPIRES_FORMAT);
} else {
$expires = '';
}

return [
'name' => $cookie->getName(),
'value' => $cookie->getValue(),
'path' => $cookie->getPath(),
'domain' => $cookie->getDomain(),
'secure' => $cookie->isSecure(),
'httponly' => $cookie->isHttpOnly(),
'expires' => $expires
'expires' => $cookie->getFormattedExpires()
];
}

Expand Down Expand Up @@ -505,7 +495,7 @@ protected function _getCookies()

$cookies = [];
foreach ($this->cookies as $cookie) {
$cookies[$cookie->getName()] = $this->toArrayClient($cookie);
$cookies[$cookie->getName()] = $this->convertCookie($cookie);
}

return $cookies;
Expand Down
21 changes: 0 additions & 21 deletions src/Http/Cookie/Cookie.php
Expand Up @@ -619,27 +619,6 @@ public function toArray()
];
}

/**
* Convert the cookie into an array of its properties.
*
* This method is compatible with older client code that
* expects date strings instead of timestamps.
*
* @return array
*/
public function toArrayClient()
{
return [
'name' => $this->getName(),
'value' => $this->getValue(),
'path' => $this->getPath(),
'domain' => $this->getDomain(),
'secure' => $this->isSecure(),
'httponly' => $this->isHttpOnly(),
'expires' => $this->getFormattedExpires()
];
}

/**
* Convert the cookie into an array of its properties.
*
Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/Http/Client/ResponseTest.php
Expand Up @@ -13,7 +13,9 @@
*/
namespace Cake\Test\TestCase\Http\Client;

use Cake\Chronos\Chronos;
use Cake\Http\Client\Response;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -428,4 +430,31 @@ public function testAutoDecodeGzipBody()
$response = new Response($headers, $body);
$this->assertEquals('Hello world!', $response->body);
}

/**
* Test convertCookie
*
* @return void
*/
public function testConvertCookie()
{
$date = Chronos::parse('2017-03-31 12:34:56');
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie = $cookie->withDomain('cakephp.org')
->withPath('/api')
->withExpiry($date)
->withHttpOnly(true)
->withSecure(true);
$expected = [
'name' => 'cakephp',
'value' => 'cakephp-rocks',
'path' => '/api',
'domain' => 'cakephp.org',
'expires' => 'Fri, 31-Mar-2017 12:34:56 GMT',
'secure' => true,
'httponly' => true
];
$response = new Response([], '');
$this->assertEquals($expected, $response->convertCookie($cookie));
}
}
26 changes: 0 additions & 26 deletions tests/TestCase/Http/Cookie/CookieTest.php
Expand Up @@ -569,32 +569,6 @@ public function testToArray()
$this->assertEquals($expected, $cookie->toArray());
}

/**
* Test toArrayClient
*
* @return void
*/
public function testToArrayClient()
{
$date = Chronos::parse('2017-03-31 12:34:56');
$cookie = new Cookie('cakephp', 'cakephp-rocks');
$cookie = $cookie->withDomain('cakephp.org')
->withPath('/api')
->withExpiry($date)
->withHttpOnly(true)
->withSecure(true);
$expected = [
'name' => 'cakephp',
'value' => 'cakephp-rocks',
'path' => '/api',
'domain' => 'cakephp.org',
'expires' => 'Fri, 31-Mar-2017 12:34:56 GMT',
'secure' => true,
'httponly' => true
];
$this->assertEquals($expected, $cookie->toArrayClient());
}

/**
* Test toArrayResponse
*
Expand Down

0 comments on commit 3db767b

Please sign in to comment.