Skip to content

Commit

Permalink
fix: 关键字冲突处理
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Jun 9, 2022
1 parent 157a349 commit 8feb208
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/Kernel/HttpClient/RequestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,11 @@ public static function formatDefaultOptions(array $options): array

public static function formatOptions(array $options, string $method): array
{
if (array_key_exists('query', $options)
|| array_key_exists('body', $options)
if (array_key_exists('query', $options) && is_array($options['query']) && empty($options['query'])) {
return $options;
}

if (array_key_exists('body', $options)
|| array_key_exists('json', $options)
|| array_key_exists('xml', $options)
) {
Expand All @@ -87,7 +90,7 @@ public static function formatOptions(array $options, string $method): array

foreach ($options as $key => $value) {
if (!array_key_exists($key, HttpClientInterface::OPTIONS_DEFAULTS)) {
$options[$name][$key] = $value;
$options[$name][trim($key, '"')] = $value;
unset($options[$key]);
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/Kernel/HttpClient/RequestUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ public function test_format_options()
$this->assertArrayHasKey('body', $formatted);
$this->assertSame('true', $formatted['body']['overtrue']);
$this->assertSame('world', $formatted['body']['hello']);


// POST with `query`
$options = ['overtrue' => 'true', 'hello' => 'world', '"query"' => 'id=1'];
$formatted = RequestUtil::formatOptions($options, 'POST');

$this->assertArrayNotHasKey('overtrue', $formatted);
$this->assertArrayNotHasKey('hello', $formatted);
$this->assertArrayHasKey('body', $formatted);
$this->assertArrayNotHasKey('query', $formatted);
$this->assertArrayHasKey('query', $formatted['body']);
$this->assertSame('id=1', $formatted['body']['query']);
$this->assertSame('true', $formatted['body']['overtrue']);
$this->assertSame('world', $formatted['body']['hello']);
}

public function test_format_xml_body()
Expand Down

1 comment on commit 8feb208

@vercel
Copy link

@vercel vercel bot commented on 8feb208 Jun 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

easywechat – ./

easywechat-git-6x-overtrue.vercel.app
easywechat.vercel.app
easywechat-overtrue.vercel.app

Please sign in to comment.