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

Skip to content

Commit 4015963

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

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 12 additions & 4 deletions
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,14 @@ 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+
public function followMetaRefresh(bool $followMetaRefresh = true)
76+
{
77+
$this->followMetaRefresh = $followMetaRefresh;
78+
}
79+
7180
/**
7281
* Returns whether client automatically follows redirects or not.
7382
*
@@ -369,9 +378,10 @@ public function request(string $method, string $uri, array $parameters = array()
369378

370379
$this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
371380

372-
// Check for meta refresh redirect.
373-
if ($this->followRedirects && null !== $redirect = $this->getMetaRefreshUrl()) {
381+
// Check for meta refresh redirect
382+
if ($this->followMetaRefresh && null !== $redirect = $this->getMetaRefreshUrl()) {
374383
$this->redirect = $redirect;
384+
$this->redirects[serialize($this->history->current())] = true;
375385
$this->crawler = $this->followRedirect();
376386
}
377387

@@ -572,8 +582,6 @@ public function followRedirect()
572582
}
573583

574584
/**
575-
* Get the meta refresh URL if the response has one.
576-
*
577585
* @see https://dev.w3.org/html5/spec-preview/the-meta-element.html#attr-meta-http-equiv-refresh
578586
*/
579587
private function getMetaRefreshUrl(): ?string

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