Skip to content

Commit 61ae5dc

Browse files
author
Amrouche Hamza
committed
[BrowserKit] add a way to switch to ajax for one request
1 parent a522e04 commit 61ae5dc

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Symfony/Component/BrowserKit/Client.php

+12-2
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 switchToXMLHttpRequest()
154+
{
155+
$this->setServerParameter('HTTP_X-Requested-With', 'XMLHttpRequest');
156+
}
157+
158+
public function removeXMLHttpRequest()
159+
{
160+
unset($this->server['HTTP_X-Requested-With']);
161+
}
162+
153163
/**
154164
* Returns the History instance.
155165
*
@@ -256,7 +266,7 @@ public function submit(Form $form, array $values = array())
256266
{
257267
$form->setValues($values);
258268

259-
return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles());
269+
return $this->request($form->getMethod(), $form->getUri(), $form->getPhpValues(), $form->getPhpFiles(), array(), null, true);
260270
}
261271

262272
/**
@@ -354,7 +364,7 @@ protected function doRequestInProcess($request)
354364
unlink($deprecationsFile);
355365
foreach ($deprecations ? unserialize($deprecations) : array() as $deprecation) {
356366
if ($deprecation[0]) {
357-
trigger_error($deprecation[1], E_USER_DEPRECATED);
367+
@trigger_error($deprecation[1], E_USER_DEPRECATED);
358368
} else {
359369
@trigger_error($deprecation[1], E_USER_DEPRECATED);
360370
}

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 testGetRequestWithXMLHttpRequest()
98+
{
99+
$client = new TestClient();
100+
$client->switchToXMLHttpRequest();
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->removeXMLHttpRequest();
104+
$this->assertEquals('http://example.com/', $client->getRequest()->getUri(), '->getCrawler() returns the Request of the last request');
105+
}
106+
97107
public function testGetRequestWithIpAsHttpHost()
98108
{
99109
$client = new TestClient();

0 commit comments

Comments
 (0)