Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 680da44

Browse files
e-moefabpot
authored andcommitted
[BrowserKit] Emulate back/forward browser navigation
1 parent bfaf8a0 commit 680da44

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/Symfony/Component/BrowserKit/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ CHANGELOG
77
* [BC BREAK] The request method is dropped from POST to GET when the response
88
status code is 301.
99

10+
* [BC BREAK] Client will skip redirects during history navigation
11+
(back and forward calls) according to W3C Browsers recommendation
12+
1013
3.2.0
1114
-----
1215

src/Symfony/Component/BrowserKit/Client.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ abstract class Client
4242

4343
private $maxRedirects = -1;
4444
private $redirectCount = 0;
45+
private $redirects = array();
4546
private $isMainRequest = true;
4647

4748
/**
@@ -328,6 +329,8 @@ public function request($method, $uri, array $parameters = array(), array $files
328329
}
329330

330331
if ($this->followRedirects && $this->redirect) {
332+
$this->redirects[serialize($this->history->current())] = true;
333+
331334
return $this->crawler = $this->followRedirect();
332335
}
333336

@@ -430,7 +433,11 @@ protected function createCrawlerFromContent($uri, $content, $type)
430433
*/
431434
public function back()
432435
{
433-
return $this->requestFromRequest($this->history->back(), false);
436+
do {
437+
$request = $this->history->back();
438+
} while (array_key_exists(serialize($request), $this->redirects));
439+
440+
return $this->requestFromRequest($request, false);
434441
}
435442

436443
/**
@@ -440,7 +447,11 @@ public function back()
440447
*/
441448
public function forward()
442449
{
443-
return $this->requestFromRequest($this->history->forward(), false);
450+
do {
451+
$request = $this->history->forward();
452+
} while (array_key_exists(serialize($request), $this->redirects));
453+
454+
return $this->requestFromRequest($request, false);
444455
}
445456

446457
/**

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,25 @@ public function testForward()
573573
$this->assertEquals($content, $client->getRequest()->getContent(), '->forward() keeps content');
574574
}
575575

576+
public function testBackAndFrowardWithRedirects()
577+
{
578+
$client = new TestClient();
579+
580+
$client->request('GET', 'http://www.example.com/foo');
581+
$client->setNextResponse(new Response('', 301, array('Location' => 'http://www.example.com/redirected')));
582+
$client->request('GET', 'http://www.example.com/bar');
583+
584+
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), 'client followed redirect');
585+
586+
$client->back();
587+
588+
$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->back() goes back in the history skipping redirects');
589+
590+
$client->forward();
591+
592+
$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->forward() goes forward in the history skipping redirects');
593+
}
594+
576595
public function testReload()
577596
{
578597
$client = new TestClient();

0 commit comments

Comments
 (0)