Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 751a3dc

Browse files
committed
bug FriendsOfPHP#236 Fix the handling of cookie for URLs with a custom port (stof)
This PR was merged into the 3.1-dev branch. Discussion ---------- Fix the handling of cookie for URLs with a custom port In recent Symfony version, BrowserKit includes the port in ``HTTP_HOST`` to match the specification of the header. But the Guzzle CookieJar does not want this host here, it wants the domain only. Otherwise the cookies won't match. closes FriendsOfPHP#232 Commits ------- 4c178c9 Fix the handling of cookie for URLs with a custom port
2 parents 8f99a86 + 4c178c9 commit 751a3dc

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Goutte/Client.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use GuzzleHttp\Exception\RequestException;
1818
use Psr\Http\Message\ResponseInterface;
1919
use Symfony\Component\BrowserKit\Client as BaseClient;
20+
use Symfony\Component\BrowserKit\Request;
2021
use Symfony\Component\BrowserKit\Response;
2122

2223
/**
@@ -75,6 +76,11 @@ public function resetAuth()
7576
return $this;
7677
}
7778

79+
/**
80+
* @param Request $request
81+
*
82+
* @return Response
83+
*/
7884
protected function doRequest($request)
7985
{
8086
$headers = array();
@@ -92,7 +98,7 @@ protected function doRequest($request)
9298

9399
$cookies = CookieJar::fromArray(
94100
$this->getCookieJar()->allRawValues($request->getUri()),
95-
$request->getServer()['HTTP_HOST']
101+
parse_url($request->getUri(), PHP_URL_HOST)
96102
);
97103

98104
$requestOptions = array(

Goutte/Tests/ClientTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ public function testUsesCookies()
114114
$this->assertEquals('test=123', $request->getHeaderLine('Cookie'));
115115
}
116116

117+
public function testUsesCookiesWithCustomPort()
118+
{
119+
$guzzle = $this->getGuzzle();
120+
$client = new Client();
121+
$client->setClient($guzzle);
122+
$client->getCookieJar()->set(new Cookie('test', '123'));
123+
$client->request('GET', 'http://www.example.com:8000/');
124+
$request = end($this->history)['request'];
125+
$this->assertEquals('test=123', $request->getHeaderLine('Cookie'));
126+
}
127+
117128
public function testUsesPostFiles()
118129
{
119130
$guzzle = $this->getGuzzle();

0 commit comments

Comments
 (0)