Browse Source

Expose the CookieCollection in a response.

Allow developer to interact with a response's CookieCollection if they
want to.
Mark Story 9 years ago
parent
commit
666c31222e
2 changed files with 39 additions and 0 deletions
  1. 15 0
      src/Http/Client/Response.php
  2. 24 0
      tests/TestCase/Http/Client/ResponseTest.php

+ 15 - 0
src/Http/Client/Response.php

@@ -396,6 +396,21 @@ class Response extends Message implements ResponseInterface
     }
 
     /**
+     * Get the cookie collection from this response.
+     *
+     * This method exposes the response's CookieCollection
+     * instance allowing you to interact with cookie objects directly.
+     *
+     * @return \Cake\Http\Cookie\CookieCollection
+     */
+    public function getCookieCollection()
+    {
+        $this->buildCookieCollection();
+
+        return $this->cookies;
+    }
+
+    /**
      * Get the value of a single cookie.
      *
      * @param string $name The name of the cookie value.

+ 24 - 0
tests/TestCase/Http/Client/ResponseTest.php

@@ -14,6 +14,7 @@
 namespace Cake\Test\TestCase\Http\Client;
 
 use Cake\Http\Client\Response;
+use Cake\Http\Cookie\CookieCollection;
 use Cake\TestSuite\TestCase;
 
 /**
@@ -331,6 +332,29 @@ XML;
     }
 
     /**
+     * Test accessing cookie collection
+     *
+     * @return void
+     */
+    public function testGetCookieCollection()
+    {
+        $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, '');
+
+        $cookies = $response->getCookieCollection();
+        $this->assertInstanceOf(CookieCollection::class, $cookies);
+        $this->assertTrue($cookies->has('test'));
+        $this->assertTrue($cookies->has('session'));
+        $this->assertTrue($cookies->has('expiring'));
+        $this->assertSame('123abc', $cookies->get('session')->getValue());
+    }
+
+    /**
      * Test statusCode()
      *
      * @return void