From 2d34e78aafa0e170845a23d0e163b3a15569fa8e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 30 Aug 2013 20:43:05 +0200 Subject: [PATCH] [BrowserKit] fixed method/files/content when redirecting a request --- src/Symfony/Component/BrowserKit/Client.php | 19 ++++++++++++++++--- .../Component/BrowserKit/Tests/ClientTest.php | 6 ++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 9f95da5a61..2c5dd26d5f 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -41,6 +41,7 @@ abstract class Client protected $followRedirects; private $internalRequest; + private $internalResponse; /** * Constructor. @@ -266,7 +267,7 @@ abstract class Client $this->response = $this->doRequest($this->request); } - $response = $this->filterResponse($this->response); + $this->internalResponse = $response = $this->filterResponse($this->response); $this->cookieJar->updateFromResponse($response, $uri); @@ -422,10 +423,22 @@ abstract class Client throw new \LogicException('The request was not redirected.'); } - $server = $this->internalRequest->getServer(); + $request = $this->internalRequest; + + if (in_array($this->internalResponse->getStatus(), array(302, 303))) { + $method = 'get'; + $files = array(); + $content = null; + } else { + $method = $request->getMethod(); + $files = $request->getFiles(); + $content = $request->getContent(); + } + + $server = $request->getServer(); unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']); - return $this->request('get', $this->redirect, array(), array(), $server); + return $this->request($method, $this->redirect, $request->getParameters(), $files, $server, $content); } /** diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index 26616a8888..7c9d18d81a 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -326,6 +326,12 @@ class ClientTest extends \PHPUnit_Framework_TestCase $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('', 302, array('Location' => 'http://www.example.com/redirected'))); + $client->request('POST', 'http://www.example.com/foo/foobar'); + + $this->assertEquals('get', $client->getRequest()->getMethod(), '->followRedirect() uses a get for 302'); } public function testFollowRedirectWithCookies()