From 8f54da73ddf900222247dc5719c278e21182fa64 Mon Sep 17 00:00:00 2001 From: "m.chwedziak" Date: Mon, 13 May 2013 14:57:53 +0200 Subject: [PATCH] [BrowserKit] should not follow redirects if status code is not 30x --- UPGRADE-2.3.md | 14 ++++++++++++++ src/Symfony/Component/BrowserKit/CHANGELOG.md | 4 ++++ src/Symfony/Component/BrowserKit/Client.php | 8 +++++++- .../Component/BrowserKit/Tests/ClientTest.php | 18 ++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/UPGRADE-2.3.md b/UPGRADE-2.3.md index 9bb123e32afd..02b36e66f241 100644 --- a/UPGRADE-2.3.md +++ b/UPGRADE-2.3.md @@ -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. \ No newline at end of file diff --git a/src/Symfony/Component/BrowserKit/CHANGELOG.md b/src/Symfony/Component/BrowserKit/CHANGELOG.md index f67099bdbe98..d2b1074029fd 100644 --- a/src/Symfony/Component/BrowserKit/CHANGELOG.md +++ b/src/Symfony/Component/BrowserKit/CHANGELOG.md @@ -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 diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 825cd42a052b..dcffdd6e132d 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -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) { + $this->redirect = $this->internalResponse->getHeader('Location'); + } else { + $this->redirect = null; + } if ($this->followRedirects && $this->redirect) { return $this->crawler = $this->followRedirect(); diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index 3f96091c1290..d0609a64a33d 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -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()