8000 [BrowserKit] don't save redirects in history · symfony/symfony@637abad · GitHub
[go: up one dir, main page]

Skip to content

Commit 637abad

Browse files
committed
[BrowserKit] don't save redirects in history
1 parent 67fba7d commit 637abad

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ abstract class Client
3838
protected $crawler;
3939
protected $insulated = false;
4040
protected $redirect;
41+
protected $redirects = array();
4142
protected $followRedirects = true;
43+
protected $browserNavigation = false;
4244

4345
private $maxRedirects = -1;
4446
private $redirectCount = 0;
@@ -68,6 +70,16 @@ public function followRedirects($followRedirect = true)
6870
$this->followRedirects = (bool) $followRedirect;
6971
}
7072

73+
/**
74+
* Sets whether to emulate back/forward browser navigation (skip redirects).
75+
*
76+
* @param bool $browserNavigation Whether to emulate browser navigation
77+
*/
78+
public function browserNavigation($browserNavigation = true)
79+
{
80+
$this->browserNavigation = (bool) $browserNavigation;
81+
}
82+
7183
/**
7284
* Sets the maximum number of requests that crawler can follow.
7385
*
@@ -307,6 +319,10 @@ public function request($method, $uri, array $parameters = array(), array $files
307319
$this->redirect = null;
308320
}
309321

322+
if ($this->redirect) {
323+
$this->redirects[] = spl_object_hash($this->history->current());
324+
}
325+
310326
if ($this->followRedirects && $this->redirect) {
311327
return $this->crawler = $this->followRedirect();
312328
}
@@ -410,7 +426,11 @@ protected function createCrawlerFromContent($uri, $content, $type)
410426
*/
411427
public function back()
412428
{
413-
return $this->requestFromRequest($this->history->back(), false);
429+
do {
430+
$request = $this->history->back();
431+
} while ($this->browserNavigation && in_array(spl_object_hash($request), $this->redirects));
432+
433+
return $this->requestFromRequest($request, false);
414434
}
415435

416436
/**
@@ -420,7 +440,11 @@ public function back()
420440
*/
421441
public function forward()
422442
{
423-
return $this->requestFromRequest($this->history->forward(), false);
443+
do {
444+
$request = $this->history->forward();
445+
} while ($this->browserNavigation && in_array(spl_object_hash($request), $this->redirects));
446+
447+
return $this->requestFromRequest($request, false);
424448
}
425449

426450
/**

0 commit comments

Comments
 (0)
0