Browse Source

Merge branch '3.next' of github.com:cakephp/cakephp into 3.next

Mark Story 8 years ago
parent
commit
b342625839
2 changed files with 47 additions and 2 deletions
  1. 14 2
      src/Http/Client.php
  2. 33 0
      tests/TestCase/Http/ClientTest.php

+ 14 - 2
src/Http/Client.php

@@ -18,6 +18,7 @@ use Cake\Core\Exception\Exception;
 use Cake\Core\InstanceConfigTrait;
 use Cake\Http\Client\CookieCollection;
 use Cake\Http\Client\Request;
+use Cake\Http\Cookie\CookieInterface;
 use Cake\Utility\Hash;
 
 /**
@@ -175,8 +176,6 @@ class Client
     /**
      * Get the cookies stored in the Client.
      *
-     * Returns an array of cookie data arrays.
-     *
      * @return \Cake\Http\Client\CookieCollection
      */
     public function cookies()
@@ -185,6 +184,19 @@ class Client
     }
 
     /**
+     * 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;
+    }
+
+    /**
      * Do a GET request.
      *
      * The $data argument supports a special `_content` key

+ 33 - 0
tests/TestCase/Http/ClientTest.php

@@ -17,6 +17,8 @@ use Cake\Core\Configure;
 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;
 
 /**
@@ -621,6 +623,37 @@ class ClientTest extends TestCase
     }
 
     /**
+     * Test cookieJar config option.
+     *
+     * @return void
+     */
+    public function testCookieJar()
+    {
+        $jar = new CookieCollection();
+        $http = new Client([
+            'cookieJar' => $jar
+        ]);
+
+        $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 head request with querystring data
      *
      * @return void