Skip to content

[HttpClient] Handle requests with null body #45566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/Symfony/Component/HttpClient/HttpClientTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
trait HttpClientTrait
{
private static $CHUNK_SIZE = 16372;
private static $emptyDefaults;

/**
* Validates and normalizes method, URL and options, and merges them with defaults.
Expand All @@ -40,6 +41,16 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
}
}

if (null === self::$emptyDefaults) {
self::$emptyDefaults = [];

foreach ($defaultOptions as $k => $v) {
if (null !== $v) {
self::$emptyDefaults[$k] = $v;
}
}
}

$options = self::mergeDefaultOptions($options, $defaultOptions, $allowExtraOptions);

$buffer = $options['buffer'] ?? true;
Expand Down Expand Up @@ -189,6 +200,16 @@ private static function mergeDefaultOptions(array $options, array $defaultOption

$options += $defaultOptions;

if (null === self::$emptyDefaults) {
self::$emptyDefaults = [];
}

foreach (self::$emptyDefaults as $k => $v) {
if (!isset($options[$k])) {
$options[$k] = $v;
}
}

if (isset($defaultOptions['extra'])) {
$options['extra'] += $defaultOptions['extra'];
}
Expand Down Expand Up @@ -221,9 +242,9 @@ private static function mergeDefaultOptions(array $options, array $defaultOption

$alternatives = [];

foreach ($defaultOptions as $key => $v) {
if (levenshtein($name, $key) <= \strlen($name) / 3 || str_contains($key, $name)) {
$alternatives[] = $key;
foreach ($defaultOptions as $k => $v) {
if (levenshtein($name, $k) <= \strlen($name) / 3 || str_contains($k, $name)) {
$alternatives[] = $k;
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ public function testHandleIsReinitOnReset()
self::assertNotSame($initialShareId, $clientState->share);
}

public function testNullBody()
{
$httpClient = $this->getHttpClient(__FUNCTION__);

$httpClient->request('POST', 'http://localhost:8057/post', [
'body' => null,
]);

$this->expectNotToPerformAssertions();
}

public function testProcessAfterReset()
{
$client = $this->getHttpClient(__FUNCTION__);
Expand Down