From 601adf5de715281d2f615fd82739fd192e66f465 Mon Sep 17 00:00:00 2001 From: Giso Stallenberg Date: Tue, 9 Apr 2019 23:54:10 +0200 Subject: [PATCH] [HttpClient] Do not allow setting both json and body --- src/Symfony/Component/HttpClient/HttpClientTrait.php | 4 ++++ .../Component/HttpClient/Tests/HttpClientTraitTest.php | 9 +++++++++ .../Component/HttpClient/Tests/ScopingHttpClientTest.php | 4 +++- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpClient/HttpClientTrait.php b/src/Symfony/Component/HttpClient/HttpClientTrait.php index 32e940ac73..abf7d86c80 100644 --- a/src/Symfony/Component/HttpClient/HttpClientTrait.php +++ b/src/Symfony/Component/HttpClient/HttpClientTrait.php @@ -45,7 +45,11 @@ trait HttpClientTrait $options = self::mergeDefaultOptions($options, $defaultOptions, $allowExtraOptions); if (isset($options['json'])) { + if (isset($options['body']) && '' !== $options['body']) { + throw new InvalidArgumentException('Define either the "json" or the "body" option, setting both is not supported.'); + } $options['body'] = self::jsonEncode($options['json']); + unset($options['json']); $options['headers']['content-type'] = $options['headers']['content-type'] ?? ['application/json']; } diff --git a/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php b/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php index 0a08abec30..5a55ec424e 100644 --- a/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php +++ b/src/Symfony/Component/HttpClient/Tests/HttpClientTraitTest.php @@ -199,6 +199,15 @@ class HttpClientTraitTest extends TestCase self::prepareRequest('POST', 'http://example.com', ['auth_bearer' => 'foo', 'auth_basic' => 'foo:bar'], HttpClientInterface::OPTIONS_DEFAULTS); } + /** + * @expectedException \Symfony\Component\HttpClient\Exception\InvalidArgumentException + * @expectedExceptionMessage Define either the "json" or the "body" option, setting both is not supported + */ + public function testSetJSONAndBodyOptions() + { + self::prepareRequest('POST', 'http://example.com', ['json' => ['foo' => 'bar'], 'body' => ''], HttpClientInterface::OPTIONS_DEFAULTS); + } + public function providePrepareAuthBasic() { yield ['foo:bar', 'Zm9vOmJhcg==']; diff --git a/src/Symfony/Component/HttpClient/Tests/ScopingHttpClientTest.php b/src/Symfony/Component/HttpClient/Tests/ScopingHttpClientTest.php index 7fe9104442..e4dbcf6c9a 100644 --- a/src/Symfony/Component/HttpClient/Tests/ScopingHttpClientTest.php +++ b/src/Symfony/Component/HttpClient/Tests/ScopingHttpClientTest.php @@ -73,7 +73,9 @@ class ScopingHttpClientTest extends TestCase $response = $client->request('GET', 'http://example.com/foo-bar', ['json' => ['url' => 'http://example.com']]); $requestOptions = $response->getRequestOptions(); - $this->assertEquals($requestOptions['json']['url'], 'http://example.com'); + $this->assertEquals($requestOptions['headers']['content-type'][0], 'application/json'); + $requestJson = json_decode($requestOptions['body'], true); + $this->assertEquals($requestJson['url'], 'http://example.com'); $this->assertEquals($requestOptions['headers']['x-app'][0], $defaultOptions['.*/foo-bar']['headers']['x-app']); $response = $client->request('GET', 'http://example.com/bar-foo', ['headers' => ['x-app' => 'unit-test']]);