8000 bug #26244 [BrowserKit] fixed BC Break for HTTP_HOST header (brizzz) · symfony/symfony@8a60f98 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a60f98

Browse files
committed
bug #26244 [BrowserKit] fixed BC Break for HTTP_HOST header (brizzz)
This PR was merged into the 2.8 branch. Discussion ---------- [BrowserKit] fixed BC Break for HTTP_HOST header | Q | A | ------------- | --- | Branch? | 2.7, 2.8, 3.x, 4.x | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes/no | Fixed tickets | #22933 | License | MIT | Doc PR | n/a The situation well described in the original issue. I will add only: - #10549 - makes server parameters to take precedence over URI. - #16265 - partially revererts #10549. Makes server parameters do not affect URI. But this is only true for `Client::request()`. It is still possible to set host for URI by `Client::setServerParameters()` when URI is realative (see examples below). I propose a compromise solution: add to HTTP_HOST header power to override URI when it is relative. Proposed solution: - if the request URI is relative, then use the HTTP_HOST header passed to Client::request() to generate an absolute URI - if the request URI is absolute, then ignore the HTTP_HOST header (as it now works) - do the same with HTTPS server parameter Profit: - fix BC Break - the documentation will be correct - http://symfony.com/doc/2.8/routing/hostname_pattern.html#testing-your-controllers - https://symfony.com/doc/2.8/testing.html#testing-configuration Before: ``` $client->setServerParameters(['HTTP_HOST' => 'example.com']); $client->request('GET', '/'); $this->assertEquals('http://example.com/', $client->getRequest()->getUri()); $client->request('GET', '/', [], [], ['HTTP_HOST' => 'example.com']); $this->assertEquals('http://localhost/', $client->getRequest()->getUri()); ``` Fixed (see last line): ``` $client->setServerParameters(['HTTP_HOST' => 'example.com']); $client->request('GET', '/'); $this->assertEquals('http://example.com/', $client->getRequest()->getUri()); $client->request('GET', '/', [], [], ['HTTP_HOST' => 'example.com']); $this->assertEquals('http://example.com/', $client->getRequest()->getUri()); ``` Commits ------- 8c4a594 [BrowserKit] fixed BC Break for HTTP_HOST header; implemented same behaviour for HTTPS server parameter
2 parents 57c1432 + 8c4a594 commit 8a60f98

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,17 @@ public function request($method, $uri, array $parameters = array(), array $files
279279
++$this->redirectCount;
280280
}
281281

282+
$originalUri = $uri;
283+
282284
$uri = $this->getAbsoluteUri($uri);
283285

284286
$server = array_merge($this->server, $server);
285287

286-
if (isset($server['HTTPS'])) {
288+
if (!empty($server['HTTP_HOST']) && null === parse_url($originalUri, PHP_URL_HOST)) {
289+
$uri = preg_replace('{^(https?\://)'.preg_quote($this->extractHost($uri)).'}', '${1}'.$server['HTTP_HOST'], $uri);
290+
}
291+
292+
if (isset($server['HTTPS']) && null === parse_url($originalUri, PHP_URL_SCHEME)) {
287293
$uri = preg_replace('{^'.parse_url($uri, PHP_URL_SCHEME).'}', $server['HTTPS'] ? 'https' : 'http', $uri);
288294
}
289295

src/Symfony/Component/BrowserKit/Tests/ClientTest.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ public function testSetServerParameterInRequest()
638638
$this->assertEquals('', $client->getServerParameter('HTTP_HOST'));
639639
$this->assertEquals('Symfony2 BrowserKit', $client->getServerParameter('HTTP_USER_AGENT'));
640640

641-
$this->assertEquals('http://www.example.com/https/www.example.com', $client->getRequest()->getUri());
641+
$this->assertEquals('https://www.example.com/https/www.example.com', $client->getRequest()->getUri());
642642

643643
$server = $client->getRequest()->getServer();
644644

@@ -652,7 +652,24 @@ public function testSetServerParameterInRequest()
652652
$this->assertEquals('new-server-key-value', $server['NEW_SERVER_KEY']);
653653

654654
$this->assertArrayHasKey('HTTPS', $server);
655-
$this->assertFalse($server['HTTPS']);
655+
$this->assertTrue($server['HTTPS']);
656+
}
657+
658+
public function testRequestWithRelativeUri()
659+
{
660+
$client = new TestClient();
661+
662+
$client->request('GET', '/', array(), array(), array(
663+
'HTTP_HOST' => 'testhost',
664+
'HTTPS' => true,
665+
));
666+
$this->assertEquals('https://testhost/', $client->getRequest()->getUri());
667+
668+
$client->request('GET', 'https://www.example.com/', array(), array(), array(
669+
'HTTP_HOST' => 'testhost',
670+
'HTTPS' => false,
671+
));
672+
$this->assertEquals('https://www.example.com/', $client->getRequest()->getUri());
656673
}
657674

658675
public function testInternalRequest()

0 commit comments

Comments
 (0)
0