8000 [BrowserKit] Adds option to follow meta refresh. · symfony/symfony@e2f175e · GitHub
[go: up one dir, main page]

Skip to content

Commit e2f175e

Browse files
committed
[BrowserKit] Adds option to follow meta refresh.
1 parent 06aeed6 commit e2f175e

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ abstract class Client
4040
protected $insulated = false;
4141
protected $redirect;
4242
protected $followRedirects = true;
43+
protected $followMetaRefresh = false;
4344

4445
private $maxRedirects = -1;
4546
private $redirectCount = 0;
@@ -68,6 +69,16 @@ public function followRedirects($followRedirect = true)
6869
$this->followRedirects = (bool) $followRedirect;
6970
}
7071

72+
/**
73+
* Sets whether to automatically follow meta refresh redirects or not.
74+
*
75+
* @param bool $followMetaRefresh
76+
*/
77+
public function followMetaRefresh(bool $followMetaRefresh = true)
78+
{
79+
$this->followMetaRefresh = $followMetaRefresh;
80+
}
81+
7182
/**
7283
* Returns whether client automatically follows redirects or not.
7384
*
@@ -370,7 +381,7 @@ public function request(string $method, string $uri, array $parameters = array()
370381
$this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
371382

372383
// Check for meta refresh redirect.
373-
if ($this->followRedirects && null !== $redirect = $this->getMetaRefreshUrl()) {
384+
if ($this->followMetaRefresh && null !== $redirect = $this->getMetaRefreshUrl()) {
374385
$this->redirect = $redirect;
375386
$this->crawler = $this->followRedirect();
376387
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,10 @@ public function testFollowRedirectDropPostMethod()
597597
/**
598598
* @dataProvider getTestsForMetaRefresh
599599
*/
600-
public function testFollowMetaRefresh(string $content, string $expectedEndingUrl)
600+
public function testFollowMetaRefresh(string $content, string $expectedEndingUrl, bool $followMetaRefresh = true)
601601
{
602602
$client = new TestClient();
603+
$client->followMetaRefresh($followMetaRefresh);
603604
$client->setNextResponse(new Response($content));
604605
$client->request('GET', 'http://www.example.com/foo/foobar');
605606
$this->assertEquals($expectedEndingUrl, $client->getRequest()->getUri());
@@ -621,6 +622,8 @@ public function getTestsForMetaRefresh()
621622
array('<html><body></body></html>', 'http://www.example.com/foo/foobar'),
622623
// Invalid meta tag placement should not result in a redirect.
623624
array('<html><body><meta http-equiv="refresh" content="0;url=http://www.example.com/redirected"/></body></html>', 'http://www.example.com/foo/foobar'),
625+
// Valid meta refresh should not be followed if disabled.
626+
array('<html><head><meta http-equiv="refresh" content="0;URL=http://www.example.com/redirected"/></head></html>', 'http://www.example.com/foo/foobar', false),
624627
);
625628
}
626629

0 commit comments

Comments
 (0)
0