8000 feature #38596 [BrowserKit] Add jsonRequest function to the browser-k… · symfony/symfony@16fb94b · GitHub
[go: up one dir, main page]

Skip to content

Commit 16fb94b

Browse files
committed
feature #38596 [BrowserKit] Add jsonRequest function to the browser-kit client (alexander-schranz)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [BrowserKit] Add jsonRequest function to the browser-kit client | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix - | License | MIT | Doc PR | symfony/symfony-docs#... If you use the FOSRestBundle for your Api's you have maybe many tests using just: ```php $client->request('POST', '/api/contacts', ['param' => 1]); ``` To test your JSON api as in the real browser request FOSRestBundle converts the json body into the `$request->request` object. I think something similar is done by ApiPlatform. If you have tests like above they will now fail as the integer is converted to string see also #38591. This PR add a new `jsonRequest` which will look like the following and will so fix the above problem: ```php $client->jsonRequest('POST', '/api/contacts', ['param' => 1]); ``` Commits ------- c2fa2cb [BrowserKit] Add jsonRequest function to the browser-kit client
2 parents 1d945b9 + c2fa2cb commit 16fb94b

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Symfony/Component/BrowserKit/AbstractBrowser.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,24 @@ public function xmlHttpRequest(string $method, string $uri, array $parameters =
161161
}
162162
}
163163

164+
/**
165+
* Converts the request parameters into a JSON string and uses it as request content.
166+
*/
167+
public function jsonRequest(string $method, string $uri, array $parameters = [], array $server = [], bool $changeHistory = true): Crawler
168+
{
169+
$content = json_encode($parameters);
170+
171+
$this->setServerParameter('CONTENT_TYPE', 'application/json');
172+
$this->setServerParameter('HTTP_ACCEPT', 'application/json');
173+
174+
try {
175+
return $this->request($method, $uri, [], [], $server, $content, $changeHistory);
176+
} finally {
177+
unset($this->server['CONTENT_TYPE']);
178+
unset($this->server['HTTP_ACCEPT']);
179+
}
180+
}
181+
164182
/**
165183
* Returns the History instance.
166184
*

src/Symfony/Component/BrowserKit/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Added `jsonRequest` method to `AbstractBrowser`
8+
49
4.3.0
510
-----
611

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->assertSame('application/json', $client->getRequest()->getServer()['CONTENT_TYPE']);
68+
$this->assertSame('application/json', $client->getRequest()->getServer()['HTTP_ACCEPT']);
69+
$this->assertFalse($client->getServerParameter('CONTENT_TYPE', false));
70+
$this->assertFalse($client->getServerParameter('HTTP_ACCEPT', false));
71+
$this->assertSame('{"param":1}', $client->getRequest()->getContent());
72+
}
73+
6374
public function testGetRequestWithIpAsHttpHost()
6475
{
6576
$client = $this->getBrowser();

0 commit comments

Comments
 (0)
0