Skip to content

[BrowserKit] should not follow redirects if status code is not 30x #8025

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 4 commits 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
14 changes: 14 additions & 0 deletions UPGRADE-2.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,17 @@ Console
```
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { ... }
```

BrowserKit
----------

* If you are receiving responses with non-3xx Status Code and Location header
please be aware that you won't be able to use auto-redirects on these kind
of responses.

If you are correctly passing 3xx Status Code with Location header, you
don't have to worry about the change.

If you were using responses with Location header and non-3xx Status Code,
you have to update your code to manually create another request to URL
grabbed from the Location header.
4 changes: 4 additions & 0 deletions src/Symfony/Component/BrowserKit/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ CHANGELOG
2.3.0
-----

* [BC BREAK] `Client::followRedirect()` won't redirect responses with
a non-3xx Status Code and `Location` header anymore, as per
http://tools.ietf.org/html/rfc2616#section-14.30

* added `Client::getInternalRequest()` and `Client::getInternalResponse()` to
have access to the BrowserKit internal request and response objects

Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/BrowserKit/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,13 @@ public function request($method, $uri, array $parameters = array(), array $files

$this->cookieJar->updateFromResponse($this->internalResponse, $uri);

$this->redirect = $this->internalResponse->getHeader('Location');
$status = $this->internalResponse->getStatus();

if ($status >= 300 && $status < 400) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This not 30X but 3XX

Copy link
Author

Choose a reason for hiding this comment

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

HTTP spec says 3xx Redirection but available codes are 30x only and it's a common way of calling redirection status codes - 30x. The conditional, by the way, is a copy-paste from Guzzle.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then maybe the upgrade note should be adjusted?

Copy link
Author

Choose a reason for hiding this comment

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

Done.

$this->redirect = $this->internalResponse->getHeader('Location');
} else {
$this->redirect = null;
}

if ($this->followRedirects && $this->redirect) {
return $this->crawler = $this->followRedirect();
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/BrowserKit/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,24 @@ public function testFollowRedirect()
$client->request('GET', 'http://www.example.com/foo/foobar');

$this->assertEquals('http://www.example.com/redirected', $client->getRequest()->getUri(), '->followRedirect() automatically follows redirects if followRedirects is true');

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

$this->assertEquals('http://www.example.com/foo/foobar', $client->getRequest()->getUri(), '->followRedirect() does not follow redirect if HTTP Code is not 30x');

$client = new TestClient();
$client->setNextResponse(new Response('', 201, array('Location' => 'http://www.example.com/redirected')));
$client->followRedirects(false);
$client->request('GET', 'http://www.example.com/foo/foobar');

try {
$client->followRedirect();
$this->fail('->followRedirect() throws a \LogicException if the request did not respond with 30x HTTP Code');
} catch (\Exception $e) {
$this->assertInstanceof('LogicException', $e, '->followRedirect() throws a \LogicException if the request did not respond with 30x HTTP Code');
}
}

public function testFollowRedirectWithMaxRedirects()
Expand Down