From 0d07af896b9d228780a064d6243ff4dec54a2f7a Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Mon, 6 May 2013 14:37:29 +0200 Subject: [PATCH 1/4] [BrowserKit] Pass headers when `followRedirect()` is called --- src/Symfony/Component/BrowserKit/Client.php | 2 +- .../Component/BrowserKit/Tests/ClientTest.php | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 8664f31ab0..c4dffdf77f 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -420,7 +420,7 @@ abstract class Client throw new \LogicException('The request was not redirected.'); } - return $this->request('get', $this->redirect); + return $this->request('get', $this->redirect, array(), array(), $this->history->current()->getServer()); } /** diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index 1bf9e5c844..26616a8888 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -342,6 +342,33 @@ class ClientTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('foo' => 'bar'), $client->getRequest()->getCookies()); } + public function testFollowRedirectWithHeaders() + { + $headers = array( + 'HTTP_HOST' => 'www.example.com', + 'HTTP_USER_AGENT' => 'Symfony2 BrowserKit', + 'CONTENT_TYPE' => 'application/vnd.custom+xml', + 'HTTPS' => false, + ); + + $client = new TestClient(); + $client->followRedirects(false); + $client->setNextResponse(new Response('', 302, array( + 'Location' => 'http://www.example.com/redirected', + ))); + $client->request('GET', 'http://www.example.com/', array(), array(), array( + 'CONTENT_TYPE' => 'application/vnd.custom+xml', + )); + + $this->assertEquals($headers, $client->getRequest()->getServer()); + + $client->followRedirect(); + + $headers['HTTP_REFERER'] = 'http://www.example.com/'; + + $this->assertEquals($headers, $client->getRequest()->getServer()); + } + public function testBack() { $client = new TestClient(); From 96a4b00c7c5e251fdc12e69e0df198ff58badefb Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 30 Aug 2013 20:36:30 +0200 Subject: [PATCH 2/4] [BrowserKit] fixed headers when redirecting if history is set to false (refs #8697) --- src/Symfony/Component/BrowserKit/Client.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index c4dffdf77f..95d51c3487 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -40,6 +40,8 @@ abstract class Client protected $redirect; protected $followRedirects; + private $internalRequest; + /** * Constructor. * @@ -250,7 +252,7 @@ abstract class Client $server['HTTP_HOST'] = parse_url($uri, PHP_URL_HOST); $server['HTTPS'] = 'https' == parse_url($uri, PHP_URL_SCHEME); - $request = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content); + $this->internalRequest = $request = new Request($uri, $method, $parameters, $files, $this->cookieJar->allValues($uri), $server, $content); $this->request = $this->filterRequest($request); @@ -420,7 +422,9 @@ abstract class Client throw new \LogicException('The request was not redirected.'); } - return $this->request('get', $this->redirect, array(), array(), $this->history->current()->getServer()); + $server = $this->internalRequest->getServer(); + + return $this->request('get', $this->redirect, array(), array(), $server); } /** From 64e16551642a5eedf9619bb591b4a1cb89329949 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 30 Aug 2013 20:37:24 +0200 Subject: [PATCH 3/4] [BrowserKit] removed some headers when redirecting a request --- src/Symfony/Component/BrowserKit/Client.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 95d51c3487..9f95da5a61 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -423,6 +423,7 @@ abstract class Client } $server = $this->internalRequest->getServer(); + unset($server['HTTP_IF_NONE_MATCH'], $server['HTTP_IF_MODIFIED_SINCE']); return $this->request('get', $this->redirect, array(), array(), $server); } From 2d34e78aafa0e170845a23d0e163b3a15569fa8e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 30 Aug 2013 20:43:05 +0200 Subject: [PATCH 4/4] [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()