Skip to content

Commit e57df9b

Browse files
Add jsonRequest function to the browser-kit client
1 parent abbb3d0 commit e57df9b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/Symfony/Component/BrowserKit/AbstractBrowser.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,22 @@ public function xmlHttpRequest(string $method, string $uri, array $parameters =
161161
}
162162
}
163163

164+
public function jsonRequest(string $method, string $uri, array $parameters = [], array $files = [], array $server = [], bool $changeHistory = true): Crawler
165+
{
166+
$content = json_encode($parameters);
167+
$parameters = [];
168+
169+
$this->setServerParameter('CONTENT_TYPE', 'application/json');
170+
$this->setServerParameter('HTTP_ACCEPT', 'application/json');
171+
172+
try {
173+
return $this->request($method, $uri, $parameters, $files, $server, $content, $changeHistory);
174+
} finally {
175+
unset($this->server['CONTENT_TYPE']);
176+
unset($this->server['HTTP_ACCEPT']);
177+
}
178+
}
179+
164180
/**
165181
* Returns the History instance.
166182
*
@@ -437,7 +453,7 @@ protected function doRequestInProcess($request)
437453
foreach ($deprecations ? unserialize($deprecations) : [] as $deprecation) {
438454
if ($deprecation[0]) {
439455
// unsilenced on purpose
440-
trigger_error($deprecation[1], \E_USER_DEPRECATED);
456+
@trigger_error($deprecation[1], \E_USER_DEPRECATED);
441457
} else {
442458
@trigger_error($deprecation[1], \E_USER_DEPRECATED);
443459
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,17 @@ public function testXmlHttpRequest()
6060
$this->assertFalse($client->getServerParameter('HTTP_X_REQUESTED_WITH', false));
6161
}
6262

63+
public function testJsonRequest()
64+
{
65+
$client = $this->getBrowser();
66+
$client->jsonRequest('GET', 'http://example.com/', ['param' => 1], [], [], true);
67+
$this->assertEquals($client->getRequest()->getServer()['CONTENT_TYPE'], 'application/json');
68+
$this->assertEquals($client->getRequest()->getServer()['HTTP_ACCEPT'], 'application/json');
69+
$this->assertFalse($client->getServerParameter('CONTENT_TYPE', false));
70+
$this->assertFalse($client->getServerParameter('HTTP_ACCEPT', false));
71+
$this->assertEquals('{"param":1}', $client->getRequest()->getContent());
72+
}
73+
6374
public function testGetRequestWithIpAsHttpHost()
6475
{
6576
$client = $this->getBrowser();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy