Skip to content

Commit

Permalink
feature #22341 [BrowserKit] Emulate back/forward browser navigation (…
Browse files Browse the repository at this point in the history
…e-moe)

This PR was submitted for the master branch but it was merged into the 3.4 branch instead (closes #22341).

Discussion
----------

[BrowserKit] Emulate back/forward browser navigation

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #22336
| License       | MIT
| Doc PR        | CHANGELOG.md updated

Hi all, please review this code for emulating back/forward browser navigation (skip redirects). If code is ok I will add tests and docs

Commits
-------

680da44 [BrowserKit] Emulate back/forward browser navigation
  • Loading branch information
fabpot committed Jul 6, 2017
2 parents bfaf8a0 + 680da44 commit 0d06bbc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Expand Up @@ -7,6 +7,9 @@ CHANGELOG
* [BC BREAK] The request method is dropped from POST to GET when the response
status code is 301.

* [BC BREAK] Client will skip redirects during history navigation
(back and forward calls) according to W3C Browsers recommendation

3.2.0
-----

Expand Down
15 changes: 13 additions & 2 deletions src/Symfony/Component/BrowserKit/Client.php
Expand Up @@ -42,6 +42,7 @@ abstract class Client

private $maxRedirects = -1;
private $redirectCount = 0;
private $redirects = array();
private $isMainRequest = true;

/**
Expand Down Expand Up @@ -328,6 +329,8 @@ public function request($method, $uri, array $parameters = array(), array $files
}

if ($this->followRedirects && $this->redirect) {
$this->redirects[serialize($this->history->current())] = true;

return $this->crawler = $this->followRedirect();
}

Expand Down Expand Up @@ -430,7 +433,11 @@ protected function createCrawlerFromContent($uri, $content, $type)
*/
public function back()
{
return $this->requestFromRequest($this->history->back(), false);
do {
$request = $this->history->back();
} while (array_key_exists(serialize($request), $this->redirects));

return $this->requestFromRequest($request, false);
}

/**
Expand All @@ -440,7 +447,11 @@ public function back()
*/
public function forward()
{
return $this->requestFromRequest($this->history->forward(), false);
do {
$request = $this->history->forward();
} while (array_key_exists(serialize($request), $this->redirects));

return $this->requestFromRequest($request, false);
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Expand Up @@ -573,6 +573,25 @@ public function testForward()
$this->assertEquals($content, $client->getRequest()->getContent(), '->forward() keeps content');
}

public function testBackAndFrowardWithRedirects()
{
$client = new TestClient();

$client->request('GET', 'http://www.example.com/foo');
$client->setNextResponse(new Response('', 301, array('Location' => 'http://www.example.com/redirected')));
$client->request('GET', 'http://www.example.com/bar');

$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), 'client followed redirect');

$client->back();

$this->assertEquals('http://www.example.com/foo', $client->getRequest()->getUri(), '->back() goes back in the history skipping redirects');

$client->forward();

$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->forward() goes forward in the history skipping redirects');
}

public function testReload()
{
$client = new TestClient();
Expand Down

0 comments on commit 0d06bbc

Please sign in to comment.