diff --git a/src/Symfony/Component/BrowserKit/AbstractBrowser.php b/src/Symfony/Component/BrowserKit/AbstractBrowser.php index f8c8987114..a43f434c70 100644 --- a/src/Symfony/Component/BrowserKit/AbstractBrowser.php +++ b/src/Symfony/Component/BrowserKit/AbstractBrowser.php @@ -161,6 +161,24 @@ abstract class AbstractBrowser } } + /** + * Converts the request parameters into a JSON string and uses it as request content. + */ + public function jsonRequest(string $method, string $uri, array $parameters = [], array $server = [], bool $changeHistory = true): Crawler + { + $content = json_encode($parameters); + + $this->setServerParameter('CONTENT_TYPE', 'application/json'); + $this->setServerParameter('HTTP_ACCEPT', 'application/json'); + + try { + return $this->request($method, $uri, [], [], $server, $content, $changeHistory); + } finally { + unset($this->server['CONTENT_TYPE']); + unset($this->server['HTTP_ACCEPT']); + } + } + /** * Returns the History instance. * diff --git a/src/Symfony/Component/BrowserKit/CHANGELOG.md b/src/Symfony/Component/BrowserKit/CHANGELOG.md index 323166a3d6..70401267c4 100644 --- a/src/Symfony/Component/BrowserKit/CHANGELOG.md +++ b/src/Symfony/Component/BrowserKit/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.3.0 +----- + + * Added `jsonRequest` method to `AbstractBrowser` + 4.3.0 ----- diff --git a/src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php b/src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php index 63374c5014..f4394fc8f6 100644 --- a/src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php +++ b/src/Symfony/Component/BrowserKit/Tests/AbstractBrowserTest.php @@ -60,6 +60,17 @@ class AbstractBrowserTest extends TestCase $this->assertFalse($client->getServerParameter('HTTP_X_REQUESTED_WITH', false)); } + public function testJsonRequest() + { + $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->assertSame('{"param":1}', $client->getRequest()->getContent()); + } + public function testGetRequestWithIpAsHttpHost() { $client = $this->getBrowser();