Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[BrowserKit | WCM] added override power to server parameters provided on request method #9821

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
added override power to server parameters provided on request method
  • Loading branch information
cordoval committed Dec 30, 2013
commit 162f8789e1105069307683dc73e42cc505f82649
13 changes: 13 additions & 0 deletions 13 src/Symfony/Component/BrowserKit/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,9 @@ 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);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would fix the getAbsoluteUri() instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you mean by fixing getAbsoluteUri()? adding a new argument getAbsoluteUri($uri, $server) ? I think this is ok as-is but let me know.

$server = array_merge($this->server, $server);
if (!$this->history->isEmpty()) {
$server['HTTP_REFERER'] = $this->history->current()->getUri();
Expand Down Expand Up @@ -512,6 +515,8 @@ public function followRedirect()
}

$server = $request->getServer();
$server = $this->updateServerFromUri($server, $this->redirect);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we have more and more such things, I would create a method to gather them all, something like updateServerFromUri() (make it private).

unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']);

$this->isMainRequest = false;
Expand Down Expand Up @@ -594,4 +599,12 @@ 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'] = parse_url($uri, PHP_URL_SCHEME);

return $server;
}
}
28 changes: 26 additions & 2 deletions 28 src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,11 @@ public function testFollowRedirectWithMaxRedirects()

$client->setNextResponse(new Response('', 302, array('Location' => '/redirected')));
$client->request('GET', 'http://www.example.com/foo/foobar');

$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->request('GET', 'https://www.example.com/');

$this->assertEquals('https://www.example.org/', $client->getRequest()->getUri(), '->followRedirect() follows protocol-relative URLs');

$client = new TestClient();
Expand Down Expand Up @@ -562,4 +560,30 @@ 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', 'http://www.example.com/foo', array(), array(), array(
'HTTP_HOST' => 'testhost',
'HTTP_USER_AGENT' => 'testua',
'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']);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.