8000 Fix jsonRequest in AbstractBrowser for GET Methods · symfony/symfony@cc1747f · GitHub
[go: up one dir, main page]

Skip to content

Commit cc1747f

Browse files
Fix jsonRequest in AbstractBrowser for GET Methods
1 parent e97f9e7 commit cc1747f

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/Symfony/Component/BrowserKit/AbstractBrowser.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,16 +166,42 @@ public function xmlHttpRequest(string $method, string $uri, array $parameters =
166166
*/
167167
public function jsonRequest(string $method, string $uri, array $parameters = [], array $server = [], bool $changeHistory = true): Crawler
168168
{
169-
$content = json_encode($parameters);
169+
// Based on https://github.com/symfony/symfony/blob/v5.2.0/src/Symfony/Component/HttpFoundation/Request.php#L388-L404
170+
// the logic in symfony/http-foundation we convert parameters to json content.
171+
switch (\strtoupper($method)) {
172+
case 'POST':
173+
case 'PUT':
174+
case 'DELETE':
175+
case 'PATCH':
176+
$content = \json_encode($parameters, \defined('JSON_THROW_ON_ERROR') ? \JSON_THROW_ON_ERROR : 0);
177+
$query = [];
178+
break;
179+
default:
180+
$content = null;
181+
$query = $parameters;
182+
break;
183+
}
184+
185+
$serverContentType = $this->getServerParameter('CONTENT_TYPE', null);
186+
$serverHttpAccept = $this->getServerParameter('HTTP_ACCEPT', null);
170187

171188
$this->setServerParameter('CONTENT_TYPE', 'application/json');
172189
$this->setServerParameter('HTTP_ACCEPT', 'application/json');
173190

174191
try {
175-
return $this->request($method, $uri, [], [], $server, $content, $changeHistory);
192+
return $this->request($method, $uri, $query, [], $server, $content, $changeHistory);
176193
} finally {
177-
unset($this->server['CONTENT_TYPE']);
178-
unset($this->server['HTTP_ACCEPT']);
194+
if (null === $serverContentType) {
195+
unset($this->server['CONTENT_TYPE']);
196+
} else {
197+
$this->setServerParameter('CONTENT_TYPE', $serverContentType);
198+
}
199+
200+
if (null === $serverHttpAccept) {
201+
unset($this->server['HTTP_ACCEPT']);
202+
} else {
203+
$this->setServerParameter('HTTP_ACCEPT', $serverHttpAccept);
204+
}
179205
}
180206
}
181207

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,34 @@ public function testXmlHttpRequest()
6363
public function testJsonRequest()
6464
{
6565
$client = $this->getBrowser();
66-
$client->jsonRequest('GET', 'http://example.com/', ['param' => 1], [], true);
66+
$client->jsonRequest('POST', 'http://example.com/', ['param' => 1], [], true);
6767
$this->assertSame('application/json', $client->getRequest()->getServer()['CONTENT_TYPE']);
6868
$this->assertSame('application/json', $client->getRequest()->getServer()['HTTP_ACCEPT']);
6969
$this->assertFalse($client->getServerParameter('CONTENT_TYPE', false));
7070
$this->assertFalse($client->getServerParameter('HTTP_ACCEPT', false));
7171
$this->assertSame('{"param":1}', $client->getRequest()->getContent());
7272
}
7373

74+
public function testJsonRequestPredefinedServerVariables()
75+
{
76+
$client = $this->getBrowser(['HTTP_ACCEPT' => 'application/xml', 'CONTENT_TYPE' => 'application/xml']);
77+
$client->jsonRequest('POST', 'http://example.com/', ['param' => 1], [], true);
78+
$this->assertSame('application/xml', $client->getServerParameter('HTTP_ACCEPT'));
79+
$this->assertSame('application/xml', $client->getServerParameter('CONTENT_TYPE'));
80+
}
81+
82+
public function testJsonRequestGet()
83+
{
84+
$client = $this->getBrowser();
85+
$client->jsonRequest('GET', 'http://example.com/', ['param' => 1], [], true);
86+
$this->assertSame('application/json', $client->getRequest()->getServer()['CONTENT_TYPE']);
87+
$this->assertSame('application/json', $client->getRequest()->getServer()['HTTP_ACCEPT']);
88+
$this->assertFalse($client->getServerParameter('CONTENT_TYPE', false));
89+
$this->assertFalse($client->getServerParameter('HTTP_ACCEPT', false));
90+
$this->assertSame(null, $client->getRequest()->getContent());
91+
$this->assertSame(['param' => '1'], $client->getRequest()->getParameters());
92+
}
93+
7494
public function testGetRequestWithIpAsHttpHost()
7595
{
7696
$client = $this->getBrowser();

0 commit comments

Comments
 (0)
0