[BrowserKit] added a way to automatically follow redirects
This commit is contained in:
parent
fcda253a1b
commit
1194520b83
@ -38,6 +38,7 @@ abstract class Client
|
|||||||
protected $crawler;
|
protected $crawler;
|
||||||
protected $insulated;
|
protected $insulated;
|
||||||
protected $redirect;
|
protected $redirect;
|
||||||
|
protected $followRedirects;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
@ -52,6 +53,17 @@ abstract class Client
|
|||||||
$this->history = null === $history ? new History() : $history;
|
$this->history = null === $history ? new History() : $history;
|
||||||
$this->cookieJar = null === $cookieJar ? new CookieJar() : $cookieJar;
|
$this->cookieJar = null === $cookieJar ? new CookieJar() : $cookieJar;
|
||||||
$this->insulated = false;
|
$this->insulated = false;
|
||||||
|
$this->followRedirects = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets whether to automatically follow redirects or not.
|
||||||
|
*
|
||||||
|
* @param Boolean $followRedirect Whether to follow redirects
|
||||||
|
*/
|
||||||
|
public function followRedirects($followRedirect = true)
|
||||||
|
{
|
||||||
|
$this->followRedirects = (Boolean) $followRedirect;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -206,6 +218,11 @@ abstract class Client
|
|||||||
|
|
||||||
$this->redirect = $response->getHeader('Location');
|
$this->redirect = $response->getHeader('Location');
|
||||||
|
|
||||||
|
if ($this->followRedirects && $this->redirect)
|
||||||
|
{
|
||||||
|
return $this->crawler = $this->followRedirect();
|
||||||
|
}
|
||||||
|
|
||||||
return $this->crawler = $this->createCrawlerFromContent($request->getUri(), $response->getContent(), $response->getHeader('Content-Type'));
|
return $this->crawler = $this->createCrawlerFromContent($request->getUri(), $response->getContent(), $response->getHeader('Content-Type'));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,7 +318,7 @@ abstract class Client
|
|||||||
*/
|
*/
|
||||||
public function followRedirect()
|
public function followRedirect()
|
||||||
{
|
{
|
||||||
if (null === $this->redirect)
|
if (empty($this->redirect))
|
||||||
{
|
{
|
||||||
throw new \LogicException('The request was not redirected.');
|
throw new \LogicException('The request was not redirected.');
|
||||||
}
|
}
|
||||||
|
@ -46,6 +46,8 @@ class Client extends BaseClient
|
|||||||
$this->testers = array();
|
$this->testers = array();
|
||||||
|
|
||||||
parent::__construct($server, $history, $cookieJar);
|
parent::__construct($server, $history, $cookieJar);
|
||||||
|
|
||||||
|
$this->followRedirects = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -223,6 +223,7 @@ RETURN;
|
|||||||
public function testFollowRedirect()
|
public function testFollowRedirect()
|
||||||
{
|
{
|
||||||
$client = new TestClient();
|
$client = new TestClient();
|
||||||
|
$client->followRedirects(false);
|
||||||
$client->request('GET', 'http://www.example.com/foo/foobar');
|
$client->request('GET', 'http://www.example.com/foo/foobar');
|
||||||
|
|
||||||
try
|
try
|
||||||
@ -240,6 +241,12 @@ RETURN;
|
|||||||
$client->followRedirect();
|
$client->followRedirect();
|
||||||
|
|
||||||
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
|
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() follows a redirect if any');
|
||||||
|
|
||||||
|
$client = new TestClient();
|
||||||
|
$client->setNextResponse(new Response('', 200, array('Location' => 'http://www.example.com/redirected')));
|
||||||
|
$client->request('GET', 'http://www.example.com/foo/foobar');
|
||||||
|
|
||||||
|
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testBack()
|
public function testBack()
|
||||||
|
Reference in New Issue
Block a user