Skip to content

[BrowserKit] Emulate back/forward browser navigation #22341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you consider spl_object_hash instead of serialize? It should be way faster and less memory intensive.

http://php.net/manual/en/function.spl-object-hash.php

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above done by @e-moe explaining why it was changed.


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
Original file line number Diff line number Diff line change
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