Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fix jsonRequest in AbstractBrowser for GET methods #39281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions src/Symfony/Component/BrowserKit/AbstractBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,42 @@ public function xmlHttpRequest(string $method, string $uri, array $parameters =
*/
public function jsonRequest(string $method, string $uri, array $parameters = [], array $server = [], bool $changeHistory = true): Crawler
{
$content = json_encode($parameters);
// Based on https://github.com/symfony/symfony/blob/v5.2.0/src/Symfony/Component/HttpFoundation/Request.php#L388-L404
// the logic in symfony/http-foundation we convert parameters to json content.
switch (strtoupper($method)) {
case 'POST':
case 'PUT':
case 'DELETE':
case 'PATCH':
$content = json_encode($parameters);
$query = [];
break;
default:
$content = null;
$query = $parameters;
Copy link
Member

@nicolas-grekas nicolas-grekas Jan 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait, I'm not sure anymore that this makes sense: $parameters doesn't go through the processing of json_encode().
That means objects (that do or don't implement JsonSerializable) are going to be passed differently when using GET vs other verbs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah based on the method we set it a request body or not. This is based on the linked logic which symfony currently use here: https://github.com/symfony/symfony/blob/v5.2.0/src/Symfony/Component/HttpFoundation/Request.php#L388-L404
It does not make sense to convert query parameters to JSON as nobody will converted it back. Which is in the FosRestBundle and ApiPlatform converted into the $request->request attributes by the BodyListener.

break;
}

$serverContentType = $this->getServerParameter('CONTENT_TYPE', null);
$serverHttpAccept = $this->getServerParameter('HTTP_ACCEPT', null);

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

try {
return $this->request($method, $uri, [], [], $server, $content, $changeHistory);
return $this->request($method, $uri, $query, [], $server, $content, $changeHistory);
} finally {
unset($this->server['CONTENT_TYPE']);
unset($this->server['HTTP_ACCEPT']);
if (null === $serverContentType) {
unset($this->server['CONTENT_TYPE']);
} else {
$this->setServerParameter('CONTENT_TYPE', $serverContentType);
}

if (null === $serverHttpAccept) {
unset($this->server['HTTP_ACCEPT']);
} else {
$this->setServerParameter('HTTP_ACCEPT', $serverHttpAccept);
}
}
}

Expand Down
24 changes: 23 additions & 1 deletion src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,36 @@ public function testXmlHttpRequest()
public function testJsonRequest()
{
$client = $this->getBrowser();
$client->jsonRequest('GET', 'http://example.com/', ['param' => 1], [], true);
$client->jsonRequest('POST', 'http://example.com/', ['param' => 1], [], true);
$this->assertSame('application/json', $client->getRequest()->getServer()['CONTENT_TYPE']);
$this->assertSame('application/json', $client->getRequest()->getServer()['HTTP_ACCEPT']);
$this->assertFalse($client->getServerParameter('CONTENT_TYPE', false));
$this->assertFalse($client->getServerParameter('HTTP_ACCEPT', false));
$this->assertSame('{"param":1}', $client->getRequest()->getContent());
}

public function testJsonRequestPredefinedServerVariables()
{
$client = $this->getBrowser(['HTTP_ACCEPT' => 'application/xml', 'CONTENT_TYPE' => 'application/xml']);
$client->jsonRequest('POST', 'http://example.com/', ['param' => 1], [], true);
$this->assertSame('application/json', $client->getRequest()->getServer()['CONTENT_TYPE']);
$this->assertSame('application/json', $client->getRequest()->getServer()['HTTP_ACCEPT']);
$this->assertSame('application/xml', $client->getServerParameter('HTTP_ACCEPT'));
$this->assertSame('application/xml', $client->getServerParameter('CONTENT_TYPE'));
}

public function testJsonRequestGet()
{
$client = $this->getBrowser();
$client->jsonRequest('GET', 'http://example.com/', ['param' => 1], [], true);
$this->assertSame('application/json', $client->getRequest()->getServer()['CONTENT_TYPE']);
$this->assertSame('application/json', $client->getRequest()->getServer()['HTTP_ACCEPT']);
$this->assertFalse($client->getServerParameter('CONTENT_TYPE', false));
$this->assertFalse($client->getServerParameter('HTTP_ACCEPT', false));
$this->assertNull($client->getRequest()->getContent());
$this->assertSame(['param' => '1'], $client->getRequest()->getParameters());
}

public function testGetRequestWithIpAsHttpHost()
{
$client = $this->getBrowser();
Expand Down