diff --git a/src/Http/Cookie/CookieCollection.php b/src/Http/Cookie/CookieCollection.php index bef6997a54c..c02a4f3dc1d 100644 --- a/src/Http/Cookie/CookieCollection.php +++ b/src/Http/Cookie/CookieCollection.php @@ -226,6 +226,9 @@ public function addToRequest(RequestInterface $request, array $extraCookies = [] foreach ($cookies as $key => $value) { $cookiePairs[] = sprintf("%s=%s", rawurlencode($key), rawurlencode($value)); } + if (empty($cookiePairs)) { + return $request; + } return $request->withHeader('Cookie', implode('; ', $cookiePairs)); } diff --git a/tests/TestCase/Http/Cookie/CookieCollectionTest.php b/tests/TestCase/Http/Cookie/CookieCollectionTest.php index 60637bf87cf..93a8c52fd36 100644 --- a/tests/TestCase/Http/Cookie/CookieCollectionTest.php +++ b/tests/TestCase/Http/Cookie/CookieCollectionTest.php @@ -356,6 +356,19 @@ public function testAddToRequest() $this->assertSame('blog=b', $request->getHeaderLine('Cookie')); } + /** + * Test adding no cookies + * + * @return void + */ + public function testAddToRequestNoCookies() + { + $collection = new CookieCollection(); + $request = new ClientRequest('http://example.com/api'); + $request = $collection->addToRequest($request); + $this->assertFalse($request->hasHeader('Cookie'), 'No header should be set.'); + } + /** * Test adding cookies from the collection to request. *