Skip to content

Commit

Permalink
[BrowserKit] refactor code and fix unquoted regex
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Jul 25, 2014
1 parent f401ab9 commit 103fd88
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
24 changes: 13 additions & 11 deletions src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -304,11 +304,7 @@ public function request($method, $uri, array $parameters = array(), array $files
$uri = $this->getAbsoluteUri($uri);

if (isset($server['HTTP_HOST'])) {
if ($port = parse_url($uri, PHP_URL_PORT)) {
$port = ':'.$port;
}

$uri = preg_replace('{^(https?\://)'.parse_url($uri, PHP_URL_HOST).$port.'}', '${1}'.$server['HTTP_HOST'], $uri);
$uri = preg_replace('{^(https?\://)'.preg_quote($this->extractHost($uri)).'}', '${1}'.$server['HTTP_HOST'], $uri);
}

if (isset($server['HTTPS'])) {
Expand All @@ -321,12 +317,7 @@ public function request($method, $uri, array $parameters = array(), array $files
$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['HTTP_HOST'] = $this->extractHost($uri);
$server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME);

$this->internalRequest = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content);
Expand Down Expand Up @@ -623,4 +614,15 @@ private function updateServerFromUri($server, $uri)

return $server;
}

private function extractHost($uri)
{
$host = parse_url($uri, PHP_URL_HOST);

if ($port = parse_url($uri, PHP_URL_PORT)) {
return $host.':'.$port;
}

return $host;
}
}
14 changes: 7 additions & 7 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Expand Up @@ -214,18 +214,18 @@ public function testRequestURIConversionByServerHost()
{
$client = new TestClient();

$server = array('HTTP_HOST' => 'www.example.com:8000');
$server = array('HTTP_HOST' => 'www.exampl+e.com:8000');
$parameters = array();
$files = array();

$client->request('GET', 'http://example.com', $parameters, $files, $server);
$this->assertEquals('http://www.example.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST to add port');
$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://example.com:8888', $parameters, $files, $server);
$this->assertEquals('http://www.example.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST to modify existing 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://example.com:8000', $parameters, $files, $server);
$this->assertEquals('http://www.example.com:8000', $client->getRequest()->getUri(), '->request() uses HTTP_HOST respects correct set 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()
Expand Down

0 comments on commit 103fd88

Please sign in to comment.