Skip to content

Commit 4f443ee

Browse files
committed
[BrowserKit] Adds support for meta refresh
1 parent e6f99da commit 4f443ee

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/Symfony/Component/BrowserKit/Client.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,18 @@ public function request(string $method, string $uri, array $parameters = array()
367367
return $this->crawler = $this->followRedirect();
368368
}
369369

370-
return $this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
370+
$this->crawler = $this->createCrawlerFromContent($this->internalRequest->getUri(), $this->internalResponse->getContent(), $this->internalResponse->getHeader('Content-Type'));
371+
372+
if ($this->followRedirects) {
373+
// Check for meta refresh redirect.
374+
$redirect = $this->hasMetaRefresh();
375+
if ($redirect) {
376+
$this->redirect = $redirect;
377+
$this->crawler = $this->followRedirect();
378+
}
379+
}
380+
381+
return $this->crawler;
371382
}
372383

373384
/**
@@ -563,6 +574,23 @@ public function followRedirect()
563574
return $response;
564575
}
565576

577+
/**
578+
* Determines if the response has a meta refresh.
579+
*
580+
* @return string|false Redirect URI if response has a meta refresh
581+
*/
582+
protected function hasMetaRefresh()
583+
{
584+
$url = false;
585+
$metaRefresh = $this->getCrawler()->filter('head meta[http-equiv="refresh"]');
586+
$content = $metaRefresh->extract(array('content'));
587+
if (!empty($content) && preg_match('/.*URL=(.*)$/i', $content[0], $m)) {
588+
$url = $m[1];
589+
}
590+
591+
return $url;
592+
}
593+
566594
/**
567595
* Restarts the client.
568596
*

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

+9
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,15 @@ public function testFollowRedirectDropPostMethod()
594594
}
595595
}
596596

597+
public function testFollowMetaRefresh()
598+
{
599+
$content = '<html><head><meta http-equiv="refresh" content="0; URL=http://www.example.com/redirected"/></head></html>';
600+
$client = new TestClient();
601+
$client->setNextResponse(new Response($content));
602+
$client->request('GET', 'http://www.example.com/foo/foobar');
603+
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri());
604+
}
605+
597606
public function testBack()
598607
{
599608
$client = new TestClient();

0 commit comments

Comments
 (0)