Browse Source

Add backwards compatibility mode for get()

This is somewhat ugly code, but it'll be going away in 4.0.0.
Mark Story 9 years ago
parent
commit
9860a554ae

+ 38 - 4
src/Http/Cookie/CookieCollection.php

@@ -82,7 +82,7 @@ class CookieCollection implements IteratorAggregate, Countable
     /**
      * Get the first cookie by name.
      *
-     * If the provided name matches a URL (matches `#^https?://#`) this method
+     * If the provided name matches a URL (starts with `http:`) this method
      * will assume you want a list of cookies that match that URL. This is
      * backwards compatible behavior that will be removed in 4.0.0
      *
@@ -92,6 +92,9 @@ class CookieCollection implements IteratorAggregate, Countable
      */
     public function get($name)
     {
+        if (substr($name, 0, 4) === 'http') {
+            return $this->getByUrl($name);
+        }
         $key = mb_strtolower($name);
         foreach ($this->cookies as $cookie) {
             if (mb_strtolower($cookie->getName()) === $key) {
@@ -103,6 +106,22 @@ class CookieCollection implements IteratorAggregate, Countable
     }
 
     /**
+     * Backwards compatibility helper for consumers of Client\CookieCollection
+     *
+     * @param string $url The url to get cookies for.
+     * @return array An array of matching cookies.
+     * @deprecated 3.5.0 Will be removed in 4.0.0. Use addToRequest() instead.
+     */
+    protected function getByUrl($url)
+    {
+        $path = parse_url($url, PHP_URL_PATH) ?: '/';
+        $host = parse_url($url, PHP_URL_HOST);
+        $scheme = parse_url($url, PHP_URL_SCHEME);
+
+        return $this->findMatchingCookies($scheme, $host, $path);
+    }
+
+    /**
      * Check if a cookie with the given name exists
      *
      * @param string $name The cookie name to check.
@@ -190,7 +209,22 @@ class CookieCollection implements IteratorAggregate, Countable
         $path = $uri->getPath();
         $host = $uri->getHost();
         $scheme = $uri->getScheme();
+        $cookies = $this->findMatchingCookies($scheme, $host, $path);
+        $cookies = array_merge($request->getCookieParams(), $cookies);
 
+        return $request->withCookieParams($cookies);
+    }
+
+    /**
+     * Find cookies matching the scheme, host, and path
+     *
+     * @param string $scheme The http scheme to match
+     * @param string $host The host to match.
+     * @param string $path The path to match
+     * @return array An array of cookie name/value pairs
+     */
+    protected function findMatchingCookies($scheme, $host, $path)
+    {
         $out = [];
         foreach ($this->cookies as $cookie) {
             if ($scheme === 'http' && $cookie->isSecure()) {
@@ -205,7 +239,8 @@ class CookieCollection implements IteratorAggregate, Countable
                 $domain = ltrim($domain, '.');
             }
 
-            if ($cookie->getExpiry() && time() > $cookie->getExpiry()) {
+            $expires = $cookie->getExpiry();
+            if ($expires && time() > $expires) {
                 continue;
             }
 
@@ -216,9 +251,8 @@ class CookieCollection implements IteratorAggregate, Countable
 
             $out[$cookie->getName()] = $cookie->getValue();
         }
-        $cookies = array_merge($request->getCookieParams(), $out);
 
-        return $request->withCookieParams($cookies);
+        return $out;
     }
 
     /**

+ 28 - 12
tests/TestCase/Http/Cookie/CookieCollectionTest.php

@@ -176,6 +176,34 @@ class CookieCollectionTest extends TestCase
     }
 
     /**
+     * Test that get() provides backwards compat behavior.
+     *
+     * When the parameter is a string that looks like a URL
+     *
+     * @return void
+     */
+    public function testGetBackwardsCompatibility()
+    {
+        $cookies = [
+            new Cookie('test', 'value', null, '/api', 'example.com', true),
+            new Cookie('test_two', 'value2', null, '/blog', 'blog.example.com', true),
+            new Cookie('test3', 'value3', null, '/blog', 'blog.example.com', false),
+        ];
+        $collection = new CookieCollection($cookies);
+        $result = $collection->get('http://example.com/api');
+        $this->assertSame([], $result);
+
+        $result = $collection->get('https://example.com/api');
+        $this->assertSame(['test' => 'value'], $result);
+
+        $result = $collection->get('http://foo.blog.example.com/blog/path');
+        $this->assertSame(['test3' => 'value3'], $result);
+
+        $result = $collection->get('https://foo.blog.example.com/blog/path');
+        $this->assertSame(['test_two' => 'value2', 'test3' => 'value3'], $result);
+    }
+
+    /**
      * Test that the constructor takes only an array of objects implementing
      * the CookieInterface
      *
@@ -440,18 +468,6 @@ class CookieCollectionTest extends TestCase
     }
 
     /**
-     * Test that get() provides backwards compat behavior.
-     *
-     * When the parameter is a string that looks like a URL
-     *
-     * @return void
-     */
-    public function testGetBackwardsCompatibility()
-    {
-        $this->markTestIncomplete();
-    }
-
-    /**
      * Test that getAll() provides backwards compat behavior.
      *
      * @return void