From 5f25639183cf1a521af5d9245fc11d1071e4ab3b Mon Sep 17 00:00:00 2001 From: Kevin Bond Date: Sun, 19 Jan 2014 20:12:37 -0500 Subject: [PATCH] [BrowserKit] add non-standard port to HTTP_HOST --- src/Symfony/Component/BrowserKit/Client.php | 8 ++++++- .../Component/BrowserKit/Tests/ClientTest.php | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 4d94fc41f407a..1cdab2e417a4a 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -295,12 +295,18 @@ public function request($method, $uri, array $parameters = array(), array $files } $uri = $this->getAbsoluteUri($uri); - $server = array_merge($this->server, $server); + if (!$this->history->isEmpty()) { $server['HTTP_REFERER'] = $this->history->current()->getUri(); } + $server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST); + + if ($port = parse_url($uri, PHP_URL_PORT)) { + $server['HTTP_HOST'] .= ':'.$port; + } + $server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME); $this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content); diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index ee827e00fdea1..45ac52cfca2b4 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -160,6 +160,11 @@ public function testRequestHttpHeaders() $client->request('GET', 'https://www.example.com'); $headers = $client->getRequest()->getServer(); $this->assertTrue($headers['HTTPS'], '->request() sets the HTTPS header'); + + $client = new TestClient(); + $client->request('GET', 'http://www.example.com:8080'); + $headers = $client->getRequest()->getServer(); + $this->assertEquals('www.example.com:8080', $headers['HTTP_HOST'], '->request() sets the HTTP_HOST header with port'); } public function testRequestURIConversion() @@ -416,6 +421,24 @@ public function testFollowRedirectWithHeaders() $this->assertEquals($headers, $client->getRequest()->getServer()); } + public function testFollowRedirectWithPort() + { + $headers = array( + 'HTTP_HOST' => 'www.example.com:8080', + 'HTTP_USER_AGENT' => 'Symfony2 BrowserKit', + 'HTTPS' => false + ); + + $client = new TestClient(); + $client->followRedirects(false); + $client->setNextResponse(new Response('', 302, array( + 'Location' => 'http://www.example.com:8080/redirected', + ))); + $client->request('GET', 'http://www.example.com:8080/'); + + $this->assertEquals($headers, $client->getRequest()->getServer()); + } + public function testBack() { $client = new TestClient();