From a10eae7d9ece0d10758044a82fce3020b7766792 Mon Sep 17 00:00:00 2001 From: Amrouche Hamza Date: Wed, 1 Nov 2017 07:23:32 +0100 Subject: [PATCH] [BrowserKit] add a way to switch to ajax for one request --- src/Symfony/Component/BrowserKit/Client.php | 10 ++++++++++ src/Symfony/Component/BrowserKit/Tests/ClientTest.php | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Symfony/Component/BrowserKit/Client.php b/src/Symfony/Component/BrowserKit/Client.php index 14eff1510c239..8b5614f075f2c 100644 --- a/src/Symfony/Component/BrowserKit/Client.php +++ b/src/Symfony/Component/BrowserKit/Client.php @@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '') return isset($this->server[$key]) ? $this->server[$key] : $default; } + public function switchToXHR() + { + $this->setServerParameter('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest'); + } + + public function removeXHR() + { + unset($this->server['HTTP_X_REQUESTED_WITH']); + } + /** * Returns the History instance. * diff --git a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php index 9c7267e83b721..eec64feb2db71 100644 --- a/src/Symfony/Component/BrowserKit/Tests/ClientTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/ClientTest.php @@ -94,6 +94,16 @@ public function testGetRequest() $this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request'); } + public function testGetRequestWithXHR() + { + $client = new TestClient(); + $client->switchToXHR(); + $client->request('GET', 'http://example.com/', array(), array(), array(), null, true, true); + $this->assertEquals($client->getRequest()->getServer()['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest'); + $client->removeXHR(); + $this->assertFalse($client->getServerParameter('HTTP_X_REQUESTED_WITH', false)); + } + public function testGetRequestWithIpAsHttpHost() { $client = new TestClient();