8000 bug #31044 [HttpClient] Do not allow setting both json and body (giso… · rubenrua/symfony@b2f8f0d · GitHub
[go: up one dir, main page]

Skip to content

Commit b2f8f0d

Browse files
committed
bug symfony#31044 [HttpClient] Do not allow setting both json and body (gisostallenberg)
This PR was squashed before being merged into the 4.3-dev branch (closes symfony#31044). Discussion ---------- [HttpClient] Do not allow setting both json and body | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#30769 | License | MIT | Doc PR | n/a This will keep developers from using both the options `$options['body']` and `$options['json']`. Using both results in only json being the body of the request, which might lead to unexpected results. Commits ------- 601adf5 [HttpClient] Do not allow setting both json and body
2 parents b09dfd9 + 601adf5 commit b2f8f0d

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
4545
$options = self::mergeDefaultOptions($options, $defaultOptions, $allowExtraOptions);
4646

4747
if (isset($options['json'])) {
48+
if (isset($options['body']) && '' !== $options['body']) {
49+
throw new InvalidArgumentException('Define either the "json" or the "body" option, setting both is not supported.');
50+
}
4851
$options['body'] = self::jsonEncode($options['json']);
52+
unset($options['json']);
4953
$options['headers']['content-type'] = $options['headers']['content-type'] ?? ['application/json'];
5054
}
5155

src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ public function testSetAuthBasicAndBearerOptions()
199199
self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foo', 'auth_basic' => 'foo:bar'], HttpClientInterface::OPTIONS_DEFAULTS);
200200
}
201201

202+
/**
203+
* @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException
204+
* @expectedExceptionMessage Define either the "json" or the "body" option, setting both is not supported
205+
*/
206+
public function testSetJSONAndBodyOptions()
207+
{
208+
self::prepareRequest('POST', 'http://example.com', ['json' => ['foo' => 'bar'], 'body' => '<html/>'], HttpClientInterface::OPTIONS_DEFAULTS);
209+
}
210+
202211
public function providePrepareAuthBasic()
203212
{
204213
yield ['foo:bar', 'Zm9vOmJhcg=='];

src/Symfony/Component/HttpClient/Tests/ScopingHttpClientTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public function testMatchingUrlsAndOptions()
7373

7474
$response = $client->request('GET', 'http://example.com/foo-bar', ['json' => ['url' => 'http://example.com']]);
7575
$requestOptions = $response->getRequestOptions();
76-
$this->assertEquals($requestOptions['json']['url'], 'http://example.com');
76+
$this->assertEquals($requestOptions['headers']['content-type'][0], 'application/json');
77+
$requestJson = json_decode($requestOptions['body'], true);
78+
$this->assertEquals($requestJson['url'], 'http://example.com');
7779
$this->assertEquals($requestOptions['headers']['x-app'][0], $defaultOptions['.*/foo-bar']['headers']['x-app']);
7880

7981
$response = $client->request('GET', 'http://example.com/bar-foo', ['headers' => ['x-app' => 'unit-test']]);

0 commit comments

Comments
 (0)
0