Skip to content
Permalink
Browse files

Add new psr7-like methods for reading cookie data.

Add separate getters for each of the modes that cookie() provided.
Deprecate the combo method in favor of discrete methods.
  • Loading branch information...
markstory committed May 14, 2016
1 parent aa90dce commit fec9e58effbda11781ff77b7bf852f2932675ccb
Showing with 80 additions and 5 deletions.
  1. +43 −0 src/Http/Client/Response.php
  2. +37 −5 tests/TestCase/Network/Http/ResponseTest.php
@@ -383,11 +383,16 @@ public function header($name = null)
/**
* Read single/multiple cookie values out.
*
* *Note* This method will only provide access to cookies that
* were added as part of the constructor. If cookies are added post
* construction they will not be accessible via this method.
*
* @param string|null $name The name of the cookie you want. Leave
* null to get all cookies.
* @param bool $all Get all parts of the cookie. When false only
* the value will be returned.
* @return mixed
* @deprecated 3.3.0 Use getCookie(), getCookieData() or getCookies() instead.
*/
public function cookie($name = null, $all = false)
{
@@ -403,6 +408,44 @@ public function cookie($name = null, $all = false)
return $this->_cookies[$name]['value'];
}
/**
* Get the all cookie data.
*
* @return array The cookie data
*/
public function getCookies()
{
return $this->_cookies;
}
/**
* Get the value of a single cookie.
*
* @param string $name The name of the cookie value.
* @return string|null Either the cookie's value or null when the cookie is undefined.
*/
public function getCookie($name)
{
if (!isset($this->_cookies[$name])) {
return null;
}
return $this->_cookies[$name]['value'];
}
/**
* Get the full data for a single cookie.
*
* @param string $name The name of the cookie value.
* @return array|null Either the cookie's data or null when the cookie is undefined.
*/
public function getCookieData($name)
{
if (!isset($this->_cookies[$name])) {
return null;
}
return $this->_cookies[$name];
}
/**
* Get the HTTP version used.
*
@@ -147,13 +147,19 @@ public function testBodyJson()
}
/**
* Test accessor for json
* Test accessor for json when set with PSR7 methods.
*
* @return void
*/
public function testBodyJsonPsr7()
{
$this->markTestIncomplete();
$data = [
'property' => 'value'
];
$encoded = json_encode($data);
$response = new Response([], '');
$response->getBody()->write($encoded);
$this->assertEquals($data, $response->json);
}
/**
@@ -289,13 +295,39 @@ public function testCookie()
}
/**
* Test accessing cookies set through the PSR7 interface.
* Test accessing cookies through the PSR7-like methods
*
* @return void
*/
public function testCookiesPsr7()
public function testGetCookies()
{
$this->markTestIncomplete();
$headers = [
'HTTP/1.0 200 Ok',
'Set-Cookie: test=value',
'Set-Cookie: session=123abc',
'Set-Cookie: expiring=soon; Expires=Wed, 09-Jun-2021 10:18:14 GMT; Path=/; HttpOnly; Secure;',
];
$response = new Response($headers, '');
$this->assertNull($response->getCookie('undef'));
$this->assertEquals('value', $response->getCookie('test'));
$this->assertEquals('soon', $response->getCookie('expiring'));
$result = $response->getCookieData('expiring');
$this->assertEquals('soon', $result['value']);
$this->assertTrue($result['httponly']);
$this->assertTrue($result['secure']);
$this->assertEquals(
'Wed, 09-Jun-2021 10:18:14 GMT',
$result['expires']
);
$this->assertEquals('/', $result['path']);
$result = $response->getCookies();
$this->assertCount(3, $result);
$this->assertArrayHasKey('test', $result);
$this->assertArrayHasKey('session', $result);
$this->assertArrayHasKey('expiring', $result);
}
/**

0 comments on commit fec9e58

Please sign in to comment.
You can’t perform that action at this time.