Skip to content

Commit

Permalink
Don't append an empty Cookie header
Browse files Browse the repository at this point in the history
The HttpClient should not append `Cookie:` to the request if there are
no cookies.
  • Loading branch information
markstory committed Aug 4, 2017
1 parent 4253fe7 commit 1f87637
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Http/Cookie/CookieCollection.php
Expand Up @@ -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));
}
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Http/Cookie/CookieCollectionTest.php
Expand Up @@ -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.
*
Expand Down

0 comments on commit 1f87637

Please sign in to comment.