Skip to content

Commit

Permalink
fix: 关键词冲突
Browse files Browse the repository at this point in the history
  • Loading branch information
overtrue committed Aug 30, 2023
1 parent d287bd8 commit 30ee1d9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Kernel/HttpClient/HttpClientMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,28 @@ public function post(string $url, array $options = []): Response|ResponseInterfa
/**
* @throws TransportExceptionInterface
*/
public function postJson(string $url, array $options = []): Response|ResponseInterfaceAlias
public function postJson(string $url, array $data = [], array $options = []): Response|ResponseInterfaceAlias
{
$options['headers']['Content-Type'] = 'application/json';

$options['json'] = $data;

return $this->request('POST', $url, RequestUtil::formatOptions($options, 'POST'));
}

/**
* @throws TransportExceptionInterface
*/
public function postXml(string $url, array $options = []): Response|ResponseInterfaceAlias
public function postXml(string $url, array $data = [], array $options = []): Response|ResponseInterfaceAlias
{
$options['headers']['Content-Type'] = 'text/xml';

if (array_key_exists('xml', $data)) {
$data = $data['xml'];
}

$options['xml'] = $data;

return $this->request('POST', $url, RequestUtil::formatOptions($options, 'POST'));
}

Expand Down
14 changes: 13 additions & 1 deletion tests/Kernel/HttpClient/HttpClientMethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,21 @@ public function test_post_json()
$this->assertSame('http://easywechat.com', $response->getRequestUrl());
$this->assertSame(['foo' => 'bar'], $response->getRequestOptions()['json']);
$this->assertSame('application/json', $response->getRequestOptions()['headers']['Content-Type']);

// with options keywords
$response = $client->postJson('http://easywechat.com', ['foo' => 'bar', 'query' => 'k1=v1&k2=v2']);

$this->assertSame(['foo' => 'bar', 'query' => 'k1=v1&k2=v2'], $response->getRequestOptions()['json']);
}

public function test_post_xml()
{
$client = new DummyHttpClient();

// no type
$response = $client->postXml('http://easywechat.com', ['foo' => 'bar', 'headers' => ['Accept' => 'application/xml']]);
$response = $client->postXml('http://easywechat.com', ['foo' => 'bar'], [
'headers' => ['Accept' => 'application/xml'],
]);

$this->assertSame('POST', $response->getRequestMethod());
$this->assertSame('http://easywechat.com', $response->getRequestUrl());
Expand All @@ -81,6 +88,11 @@ public function test_post_xml()
$this->assertSame('http://easywechat.com', $response->getRequestUrl());
$this->assertSame(Xml::build(['foo' => 'bar']), $response->getRequestOptions()['xml']);
$this->assertSame('text/xml', $response->getRequestOptions()['headers']['Content-Type']);

// with options keywords
$response = $client->postXml('http://easywechat.com', ['xml' => ['foo' => 'bar', 'query' => 'k1=v1&k2=v2']]);

$this->assertSame(['foo' => 'bar', 'query' => 'k1=v1&k2=v2'], $response->getRequestOptions()['xml']);
}

public function test_put()
Expand Down

1 comment on commit 30ee1d9

@vercel
Copy link

@vercel vercel bot commented on 30ee1d9 Aug 30, 2023

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-overtrue.vercel.app
easywechat.vercel.app
easywechat-git-6x-overtrue.vercel.app

Please sign in to comment.