Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitebd9e48

Browse files
author
Amrouche Hamza
committed
[BrowserKit] add a way to switch to ajax for one request
1 parenta522e04 commitebd9e48

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

‎src/Symfony/Component/BrowserKit/Client.php‎

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ public function getServerParameter($key, $default = '')
150150
returnisset($this->server[$key]) ?$this->server[$key] :$default;
151151
}
152152

153+
publicfunctionswitchToAjax()
154+
{
155+
$this->setServerParameter('HTTP_X-Requested-With','XMLHttpRequest');
156+
}
157+
158+
publicfunctionremoveAjax()
159+
{
160+
unset($this->server['HTTP_X-Requested-With']);
161+
}
162+
153163
/**
154164
* Returns the History instance.
155165
*
@@ -249,14 +259,15 @@ public function click(Link $link)
249259
*
250260
* @param Form $form A Form instance
251261
* @param array $values An array of form field values
262+
* @param bool $isAjax Whether it's ajax or not
252263
*
253264
* @return Crawler
254265
*/
255-
publicfunctionsubmit(Form$form,array$values =array())
266+
publicfunctionsubmit(Form$form,array$values =array(),$isAjax =false)
256267
{
257268
$form->setValues($values);
258269

259-
return$this->request($form->getMethod(),$form->getUri(),$form->getPhpValues(),$form->getPhpFiles());
270+
return$this->request($form->getMethod(),$form->getUri(),$form->getPhpValues(),$form->getPhpFiles(),array(),null,true,$isAjax);
260271
}
261272

262273
/**
@@ -269,17 +280,21 @@ public function submit(Form $form, array $values = array())
269280
* @param array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
270281
* @param string $content The raw body data
271282
* @param bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())
272-
*
283+
* @param bool $isAjax Whether it's a ajax request or not
273284
* @return Crawler
274285
*/
275-
publicfunctionrequest($method,$uri,array$parameters =array(),array$files =array(),array$server =array(),$content =null,$changeHistory =true)
286+
publicfunctionrequest($method,$uri,array$parameters =array(),array$files =array(),array$server =array(),$content =null,$changeHistory =true,$isAjax =false)
276287
{
277288
if ($this->isMainRequest) {
278289
$this->redirectCount =0;
279290
}else {
280291
++$this->redirectCount;
281292
}
282293

294+
if (true ===$isAjax) {
295+
$this->switchToAjax();
296+
}
297+
283298
$uri =$this->getAbsoluteUri($uri);
284299

285300
$server =array_merge($this->server,$server);
@@ -324,6 +339,10 @@ public function request($method, $uri, array $parameters = array(), array $files
324339
$this->redirect =null;
325340
}
326341

342+
if (true ===$isAjax) {
343+
$this->removeAjax();
344+
}
345+
327346
if ($this->followRedirects &&$this->redirect) {
328347
$this->redirects[serialize($this->history->current())] =true;
329348

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

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

97+
publicfunctiontestGetRequestWithAjax()
98+
{
99+
$client =newTestClient();
100+
$client->request('GET','http://example.com/',array(),array(),array(),null,true,true);
101+
$this->assertEquals($client->getRequest()->getServer()['HTTP_X-Requested-With'],'XMLHttpRequest');
102+
$this->assertEquals('http://example.com/',$client->getRequest()->getUri(),'->getCrawler() returns the Request of the last request');
103+
}
104+
97105
publicfunctiontestGetRequestWithIpAsHttpHost()
98106
{
99107
$client =newTestClient();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp