8000 Handle requests with null body · symfony/symfony@6561f9e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6561f9e

Browse files
committed
Handle requests with null body
1 parent 65e6faa commit 6561f9e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
trait HttpClientTrait
2424
{
2525
private static $CHUNK_SIZE = 16372;
26+
private static $emptyDefaults;
2627

2728
/**
2829
* Validates and normalizes method, URL and options, and merges them with defaults.
@@ -31,6 +32,15 @@ trait HttpClientTrait
3 10000 132
*/
3233
private static function prepareRequest(?string $method, ?string $url, array $options, array $defaultOptions = [], bool $allowExtraOptions = false): array
3334
{
35+
if (null === self::$emptyDefaults) {
36+
self::$emptyDefaults = [];
37+
foreach ($defaultOptions as $key => $v) {
38+
if (isset($v)) {
39+
self::$emptyDefaults[$key] = $v;
40+
}
41+
}
42+
}
43+
3444
if (null !== $method) {
3545
if (\strlen($method) !== strspn($method, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')) {
3646
throw new InvalidArgumentException(sprintf('Invalid HTTP method "%s", only uppercase letters are accepted.', $method));
@@ -188,6 +198,11 @@ private static function mergeDefaultOptions(array $options, array $defaultOption
188198
$options['query'] = $options['query'] ?? [];
189199

190200
$options += $defaultOptions;
201+
foreach (self::$emptyDefaults as $key => $v) {
202+
if (!isset($options[$key])) {
203+
$options[$key] = $v;
204+
}
205+
}
191206

192207
if (isset($defaultOptions['extra'])) {
193208
$options['extra'] += $defaultOptions['extra'];

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ public function testHandleIsReinitOnReset()
148148
self::assertNotSame($initialShareId, $clientState->share);
149149
}
150150

151+
public function testNullBody()
152+
{
153+
$httpClient = $this->getHttpClient(__FUNCTION__);
154+
155+
$httpClient->request('POST', 'http://localhost:8057/post', [
156+
'body' => null,
157+
]);
158+
159+
$this->expectNotToPerformAssertions();
160+
}
161+
151162
public function testProcessAfterReset()
152163
{
153164
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)
0