Skip to content

Commit

Permalink
Added addCookie() and removeCookie() methods to Cake\Http\Client.
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Pustułka committed May 30, 2017
1 parent 19935c0 commit 342d624
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Http/Client.php
Expand Up @@ -18,6 +18,7 @@
use Cake\Core\InstanceConfigTrait;
use Cake\Http\Client\CookieCollection;
use Cake\Http\Client\Request;
use Cake\Http\Cookie\CookieInterface;
use Cake\Utility\Hash;

/**
Expand Down Expand Up @@ -182,6 +183,32 @@ public function cookies()
return $this->_cookies;
}

/**
* Adds a cookie to the Client collection.
*
* @param \Cake\Http\Cookie\CookieInterface $cookie Cookie object.
* @return $this
*/
public function addCookie(CookieInterface $cookie)
{
$this->_cookies = $this->_cookies->add($cookie);

return $this;
}

/**
* Removes a cookie from the Client collection.
*
* @param string $name Cookie name.
* @return $this
*/
public function removeCookie($name)
{
$this->_cookies = $this->_cookies->remove($name);

return $this;
}

/**
* Do a GET request.
*
Expand Down
36 changes: 36 additions & 0 deletions tests/TestCase/Http/ClientTest.php
Expand Up @@ -17,6 +17,7 @@
use Cake\Http\Client;
use Cake\Http\Client\Request;
use Cake\Http\Client\Response;
use Cake\Http\Cookie\Cookie;
use Cake\Http\Cookie\CookieCollection;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -636,6 +637,41 @@ public function testCookieJar()
$this->assertSame($jar, $http->cookies());
}

/**
* Test addCookie() method.
*
* @return void
*/
public function testAddCookie()
{
$client = new Client();
$cookie = new Cookie('foo');

$this->assertFalse($client->cookies()->has('foo'));

$client->addCookie($cookie);
$this->assertTrue($client->cookies()->has('foo'));
}

/**
* Test removeCookie() method.
*
* @return void
*/
public function testRemoveCookie()
{
$cookie = new Cookie('foo');
$jar = new CookieCollection([$cookie]);
$client = new Client([
'cookieJar' => $jar
]);

$this->assertTrue($client->cookies()->has('foo'));

$client->removeCookie('foo');
$this->assertFalse($client->cookies()->has('foo'));
}

/**
* test head request with querystring data
*
Expand Down

0 comments on commit 342d624

Please sign in to comment.