From 289da16681bce6af97ff4e5049659daadf1826e1 Mon Sep 17 00:00:00 2001 From: Luis Cordova Date: Thu, 19 Dec 2013 05:24:33 -0500 Subject: [PATCH 1/3] added override power to server parameters provided on request method --- src/Symfony/Component/BrowserKit/Client.php | 20 ++++++++++- .../Component/BrowserKit/Tests/ClientTest.php | 36 ++++++++++++++++++- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index cf1445c35b..aa02331176 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -302,6 +302,15 @@ abstract class Client } $uri = $this->getAbsoluteUri($uri); + + if (isset($server['HTTP_HOST'])) { + $uri = str_replace(parse_url($uri, PHP_URL_HOST), $server['HTTP_HOST'], $uri); + } + + if (isset($server['HTTPS'])) { + $uri = str_replace(parse_url($uri, PHP_URL_SCHEME), $server['HTTPS'] ? 'https' : 'http', $uri); + } + $server = array_merge($this->server, $server); if (!$this->history->isEmpty()) { @@ -518,7 +527,7 @@ abstract class Client } $server = $request->getServer(); - unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']); + $server = $this->updateServerFromUri($server, $this->redirect); $this->isMainRequest = false; @@ -600,4 +609,13 @@ abstract class Client { return $this->request($request->getMethod(), $request->getUri(), $request->getParameters(), $request->getFiles(), $request->getServer(), $request->getContent(), $changeHistory); } + + private function updateServerFromUri($server, $uri) + { + $server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST); + $server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME); + unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']); + + return $server; + } } diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index 8eb2549d41..c52e75076b 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -399,7 +399,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows relative URLs'); $client = new TestClient(); - $client->setNextResponse(new Response('', 302, array('Location' => '//www.example.org/'))); + $client->setNextResponse(new Response('', 302, array('Location' => 'https://www.example.org/'))); $client->request('GET', 'https://www.example.com/'); $this->assertEquals('https://www.example.org/', $client->getRequest()->getUri(), '->followRedirect() follows protocol-relative URLs'); @@ -585,4 +585,38 @@ class ClientTest extends \PHPUnit_Framework_TestCase $client->setServerParameter('HTTP_USER_AGENT', 'testua'); $this->assertEquals('testua', $client->getServerParameter('HTTP_USER_AGENT')); } + + public function testSetServerParameterInRequest() + { + $client = new TestClient(); + + $this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST')); + $this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT')); + + $client->request('GET', 'https://www.example.com/foo', array(), array(), array( + 'HTTP_HOST' => 'testhost', + 'HTTP_USER_AGENT' => 'testua', + 'HTTPS' => false, + 'NEW_SERVER_KEY' => 'new-server-key-value' + )); + + $this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST')); + $this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT')); + + $this->assertEquals('http://testhost/foo', $client->getRequest()->getUri()); + + $server = $client->getRequest()->getServer(); + + $this->assertArrayHasKey('HTTP_USER_AGENT', $server); + $this->assertEquals('testua', $server['HTTP_USER_AGENT']); + + $this->assertArrayHasKey('HTTP_HOST', $server); + $this->assertEquals('testhost', $server['HTTP_HOST']); + + $this->assertArrayHasKey('NEW_SERVER_KEY', $server); + $this->assertEquals('new-server-key-value', $server['NEW_SERVER_KEY']); + + $this->assertArrayHasKey('HTTPS', $server); + $this->assertFalse($server['HTTPS']); + } } From d9cf28d016bee7b78494f20d8712fa062b0b2884 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 27 Mar 2014 10:02:54 +0100 Subject: [PATCH 2/3] fixed protocol-relative URLs --- src/Symfony/Component/BrowserKit/Client.php | 3 ++- src/Symfony/Component/BrowserKit/Tests/ClientTest.php | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index aa02331176..2e5aedc529 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -613,7 +613,8 @@ abstract class Client private function updateServerFromUri($server, $uri) { $server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST); - $server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME); + $scheme = parse_url($uri, PHP_URL_SCHEME); + $server['HTTPS'] = null === $scheme ? $server['HTTPS'] : 'https' == $scheme; unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']); return $server; diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index c52e75076b..4ebe96e7e9 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -399,7 +399,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows relative URLs'); $client = new TestClient(); - $client->setNextResponse(new Response('', 302, array('Location' => 'https://www.example.org/'))); + $client->setNextResponse(new Response('', 302, array('Location' => '//www.example.org/'))); $client->request('GET', 'https://www.example.com/'); $this->assertEquals('https://www.example.org/', $client->getRequest()->getUri(), '->followRedirect() follows protocol-relative URLs'); From 65b98102d16d99db96bf509b270b2a304f38adbb Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 27 Mar 2014 10:08:38 +0100 Subject: [PATCH 3/3] fixed too greedy replacements --- src/Symfony/Component/BrowserKit/Client.php | 4 ++-- src/Symfony/Component/BrowserKit/Tests/ClientTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 2e5aedc529..ecabc680e2 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -304,11 +304,11 @@ abstract class Client $uri = $this->getAbsoluteUri($uri); if (isset($server['HTTP_HOST'])) { - $uri = str_replace(parse_url($uri, PHP_URL_HOST), $server['HTTP_HOST'], $uri); + $uri = preg_replace('{^(https?\://)'.parse_url($uri, PHP_URL_HOST).'}', '\\1'.$server['HTTP_HOST'], $uri); } if (isset($server['HTTPS'])) { - $uri = str_replace(parse_url($uri, PHP_URL_SCHEME), $server['HTTPS'] ? 'https' : 'http', $uri); + $uri = preg_replace('{^'.parse_url($uri, PHP_URL_SCHEME).'}', $server['HTTPS'] ? 'https' : 'http', $uri); } $server = array_merge($this->server, $server); diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index 4ebe96e7e9..f8c128f138 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -593,7 +593,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST')); $this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT')); - $client->request('GET', 'https://www.example.com/foo', array(), array(), array( + $client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array( 'HTTP_HOST' => 'testhost', 'HTTP_USER_AGENT' => 'testua', 'HTTPS' => false, @@ -603,7 +603,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST')); $this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT')); - $this->assertEquals('http://testhost/foo', $client->getRequest()->getUri()); + $this->assertEquals('http://testhost/https/www.example.com', $client->getRequest()->getUri()); $server = $client->getRequest()->getServer();