Skip to content

Commit

Permalink
[BrowserKit] isolated the max redirect count to a given main request …
Browse files Browse the repository at this point in the history
…(refs #7811)
  • Loading branch information
fabpot committed Apr 25, 2013
1 parent af96f9f commit 698d6aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
34 changes: 25 additions & 9 deletions Client.php
Expand Up @@ -41,8 +41,10 @@ abstract class Client
protected $insulated;
protected $redirect;
protected $followRedirects;
protected $maxRedirects = -1;
protected $redirectCount = 0;

private $maxRedirects;
private $redirectCount;
private $isMainRequest;

/**
* Constructor.
Expand All @@ -60,6 +62,9 @@ public function __construct(array $server = array(), History $history = null, Co
$this->cookieJar = null === $cookieJar ? new CookieJar() : $cookieJar;
$this->insulated = false;
$this->followRedirects = true;
$this->maxRedirects = -1;
$this->redirectCount = 0;
$this->isMainRequest = true;
}

/**
Expand All @@ -76,12 +81,13 @@ public function followRedirects($followRedirect = true)

/**
* Sets the maximum number of requests that crawler can follow.
*
* @param integer $maxRedirects
*
* @param integer $maxRedirects
*/
public function setMaxRedirects($maxRedirects)
{
$this->maxRedirects = $maxRedirects < 0 ? -1 : $maxRedirects;
$this->followRedirects = -1 != $this->maxRedirects;
}

/**
Expand Down Expand Up @@ -289,6 +295,12 @@ public function submit(Form $form, array $values = array())
*/
public function request($method, $uri, array $parameters = array(), array $files = array(), array $server = array(), $content = null, $changeHistory = true)
{
if ($this->isMainRequest) {
$this->redirectCount = 0;
} else {
++$this->redirectCount;
}

$uri = $this->getAbsoluteUri($uri);

$server = array_merge($this->server, $server);
Expand Down Expand Up @@ -467,16 +479,20 @@ public function followRedirect()
if (empty($this->redirect)) {
throw new \LogicException('The request was not redirected.');
}

if (-1 !== $this->maxRedirects) {
if ($this->redirectCount === $this->maxRedirects) {
if ($this->redirectCount > $this->maxRedirects) {
throw new \LogicException(sprintf('The maximum number (%d) of redirections was reached.', $this->maxRedirects));
}

++$this->redirectCount;
}

return $this->request('get', $this->redirect);
$this->isMainRequest = false;

$response = $this->request('get', $this->redirect);

$this->isMainRequest = true;

return $response;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions Tests/ClientTest.php
Expand Up @@ -356,17 +356,19 @@ public function testFollowRedirectWithMaxRedirects()
$client->setMaxRedirects(1);
$client->setNextResponse(new Response('', 302, 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() follows a redirect if any');

$client->setNextResponse(new Response('', 302, array('Location' => 'http://www.example.com/redirected2')));

try {
$client->followRedirect();
$this->fail('->followRedirect() throws a \LogicException if the request was redirected and limit of redirections were reached');
$this->fail('->followRedirect() throws a \LogicException if the request was redirected and limit of redirections was reached');
} catch (\Exception $e) {
$this->assertInstanceof('LogicException', $e, '->followRedirect() throws a \LogicException if the request was redirected and limit of redirections were reached');
$this->assertInstanceof('LogicException', $e, '->followRedirect() throws a \LogicException if the request was redirected and limit of redirections was reached');
}

$client->setNextResponse(new Response('', 302, 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() follows a redirect if any');
}

public function testFollowRedirectWithCookies()
Expand Down

0 comments on commit 698d6aa

Please sign in to comment.