Skip to content

Commit 17d1054

Browse files
committed
feature #24778 [BrowserKit] add a way to switch to ajax for one request (Simperfit)
This PR was merged into the 4.1-dev branch. Discussion ---------- [BrowserKit] add a way to switch to ajax for one request | Q | A | ------------- | --- | Branch? | 4.1 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20306 | License | MIT | Doc PR | will do Follow the work on #20306. /cc @fabpot is it the right implementation ? Commits ------- a10eae7 [BrowserKit] add a way to switch to ajax for one request
2 parents 74cea1d + a10eae7 commit 17d1054

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Symfony/Component/BrowserKit/Client.php

+10
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '')
150150
return isset($this->server[$key]) ? $this->server[$key] : $default;
151151
}
152152

153+
public function switchToXHR()
154+
{
155+
$this->setServerParameter('HTTP_X_REQUESTED_WITH', 'XMLHttpRequest');
156+
}
157+
158+
public function removeXHR()
159+
{
160+
unset($this->server['HTTP_X_REQUESTED_WITH']);
161+
}
162+
153163
/**
154164
* Returns the History instance.
155165
*

src/Symfony/Component/BrowserKit/Tests/ClientTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ public function testGetRequest()
9494
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
9595
}
9696

97+
public function testGetRequestWithXHR()
98+
{
99+
$client = new TestClient();
100+
$client->switchToXHR();
101+
$client->request('GET', 'http://example.com/', array(), array(), array(), null, true, true);
102+
$this->assertEquals($client->getRequest()->getServer()['HTTP_X_REQUESTED_WITH'], 'XMLHttpRequest');
103+
$client->removeXHR();
104+
$this->assertFalse($client->getServerParameter('HTTP_X_REQUESTED_WITH', false));
105+
}
106+
97107
public function testGetRequestWithIpAsHttpHost()
98108
{
99109
$client = new TestClient();

0 commit comments

Comments
 (0)