Skip to content

Commit

Permalink
added override power to server parameters provided on request method
Browse files Browse the repository at this point in the history
  • Loading branch information
cordoval authored and fabpot committed Mar 27, 2014
1 parent 9068875 commit 289da16
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -302,6 +302,15 @@ public function request($method, $uri, array $parameters = array(), array $files
}

$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()) {
Expand Down Expand Up @@ -518,7 +527,7 @@ public function followRedirect()
}

$server = $request->getServer();
unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']);
$server = $this->updateServerFromUri($server, $this->redirect);

$this->isMainRequest = false;

Expand Down Expand Up @@ -600,4 +609,13 @@ protected function requestFromRequest(Request $request, $changeHistory = true)
{
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;
}
}
36 changes: 35 additions & 1 deletion src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Expand Up @@ -399,7 +399,7 @@ public function testFollowRedirectWithMaxRedirects()
$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');
Expand Down Expand Up @@ -585,4 +585,38 @@ public function testSetServerParameter()
$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']);
}
}

0 comments on commit 289da16

Please sign in to comment.