[BrowserKit] Corrected HTTP_HOST logic #15398
This commit is contained in:
parent
72fa3bdda9
commit
b5f524d40b
@ -110,7 +110,6 @@ abstract class Client
|
|||||||
public function setServerParameters(array $server)
|
public function setServerParameters(array $server)
|
||||||
{
|
{
|
||||||
$this->server = array_merge(array(
|
$this->server = array_merge(array(
|
||||||
'HTTP_HOST' => 'localhost',
|
|
||||||
'HTTP_USER_AGENT' => 'Symfony2 BrowserKit',
|
'HTTP_USER_AGENT' => 'Symfony2 BrowserKit',
|
||||||
), $server);
|
), $server);
|
||||||
}
|
}
|
||||||
@ -273,21 +272,19 @@ abstract class Client
|
|||||||
|
|
||||||
$uri = $this->getAbsoluteUri($uri);
|
$uri = $this->getAbsoluteUri($uri);
|
||||||
|
|
||||||
if (!empty($server['HTTP_HOST'])) {
|
$server = array_merge($this->server, $server);
|
||||||
$uri = preg_replace('{^(https?\://)'.preg_quote($this->extractHost($uri)).'}', '${1}'.$server['HTTP_HOST'], $uri);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isset($server['HTTPS'])) {
|
if (isset($server['HTTPS'])) {
|
||||||
$uri = preg_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);
|
|
||||||
|
|
||||||
if (!$this->history->isEmpty()) {
|
if (!$this->history->isEmpty()) {
|
||||||
$server['HTTP_REFERER'] = $this->history->current()->getUri();
|
$server['HTTP_REFERER'] = $this->history->current()->getUri();
|
||||||
}
|
}
|
||||||
|
|
||||||
$server['HTTP_HOST'] = $this->extractHost($uri);
|
if (empty($server['HTTP_HOST'])) {
|
||||||
|
$server['HTTP_HOST'] = $this->extractHost($uri);
|
||||||
|
}
|
||||||
$server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);
|
$server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);
|
||||||
|
|
||||||
$this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
|
$this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
|
||||||
|
@ -93,12 +93,14 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
|
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetRequestWithIpAsHost()
|
public function testGetRequestWithIpAsHttpHost()
|
||||||
{
|
{
|
||||||
$client = new TestClient();
|
$client = new TestClient();
|
||||||
$client->request('GET', 'https://example.com/foo', array(), array(), array('HTTP_HOST' => '127.0.0.1'));
|
$client->request('GET', 'https://example.com/foo', array(), array(), array('HTTP_HOST' => '127.0.0.1'));
|
||||||
|
|
||||||
$this->assertEquals('https://127.0.0.1/foo', $client->getRequest()->getUri());
|
$this->assertEquals('https://example.com/foo', $client->getRequest()->getUri());
|
||||||
|
$headers = $client->getRequest()->getServer();
|
||||||
|
$this->assertEquals('127.0.0.1', $headers['HTTP_HOST']);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetResponse()
|
public function testGetResponse()
|
||||||
@ -212,24 +214,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('http://www.example.com/http', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
|
$this->assertEquals('http://www.example.com/http', $client->getRequest()->getUri(), '->request() uses the previous request for relative URLs');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testRequestURIConversionByServerHost()
|
|
||||||
{
|
|
||||||
$client = new TestClient();
|
|
||||||
|
|
||||||
$server = array('HTTP_HOST' => 'www.exampl+e.com:8000');
|
|
||||||
$parameters = array();
|
|
||||||
$files = array();
|
|
||||||
|
|
||||||
$client->request('GET', 'http://exampl+e.com', $parameters, $files, $server);
|
|
||||||
$this->assertEquals('http://www.exampl+e.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST to add port');
|
|
||||||
|
|
||||||
$client->request('GET', 'http://exampl+e.com:8888', $parameters, $files, $server);
|
|
||||||
$this->assertEquals('http://www.exampl+e.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST to modify existing port');
|
|
||||||
|
|
||||||
$client->request('GET', 'http://exampl+e.com:8000', $parameters, $files, $server);
|
|
||||||
$this->assertEquals('http://www.exampl+e.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST respects correct set port');
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testRequestReferer()
|
public function testRequestReferer()
|
||||||
{
|
{
|
||||||
$client = new TestClient();
|
$client = new TestClient();
|
||||||
@ -572,7 +556,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testGetServerParameter()
|
public function testGetServerParameter()
|
||||||
{
|
{
|
||||||
$client = new TestClient();
|
$client = new TestClient();
|
||||||
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
|
||||||
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||||
$this->assertEquals('testvalue', $client->getServerParameter('testkey', 'testvalue'));
|
$this->assertEquals('testvalue', $client->getServerParameter('testkey', 'testvalue'));
|
||||||
}
|
}
|
||||||
@ -581,7 +565,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$client = new TestClient();
|
$client = new TestClient();
|
||||||
|
|
||||||
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
|
||||||
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||||
|
|
||||||
$client->setServerParameter('HTTP_HOST', 'testhost');
|
$client->setServerParameter('HTTP_HOST', 'testhost');
|
||||||
@ -595,7 +579,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$client = new TestClient();
|
$client = new TestClient();
|
||||||
|
|
||||||
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
|
||||||
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||||
|
|
||||||
$client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array(
|
$client->request('GET', 'https://www.example.com/https/www.example.com', array(), array(), array(
|
||||||
@ -605,10 +589,10 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
|||||||
'NEW_SERVER_KEY' => 'new-server-key-value',
|
'NEW_SERVER_KEY' => 'new-server-key-value',
|
||||||
));
|
));
|
||||||
|
|
||||||
$this->assertEquals('localhost', $client->getServerParameter('HTTP_HOST'));
|
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
|
||||||
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
|
||||||
|
|
||||||
$this->assertEquals('http://testhost/https/www.example.com', $client->getRequest()->getUri());
|
$this->assertEquals('http://www.example.com/https/www.example.com', $client->getRequest()->getUri());
|
||||||
|
|
||||||
$server = $client->getRequest()->getServer();
|
$server = $client->getRequest()->getServer();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user