From 30ee1d96f4d53170a52679ec53b69ca2a97b88e0 Mon Sep 17 00:00:00 2001 From: overtrue Date: Wed, 30 Aug 2023 16:22:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=B3=E9=94=AE=E8=AF=8D=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Kernel/HttpClient/HttpClientMethods.php | 12 ++++++++++-- tests/Kernel/HttpClient/HttpClientMethodsTest.php | 14 +++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Kernel/HttpClient/HttpClientMethods.php b/src/Kernel/HttpClient/HttpClientMethods.php index 76d2d64bb..67cd47a3c 100644 --- a/src/Kernel/HttpClient/HttpClientMethods.php +++ b/src/Kernel/HttpClient/HttpClientMethods.php @@ -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')); } diff --git a/tests/Kernel/HttpClient/HttpClientMethodsTest.php b/tests/Kernel/HttpClient/HttpClientMethodsTest.php index 0a08e0133..ea63f4a7d 100644 --- a/tests/Kernel/HttpClient/HttpClientMethodsTest.php +++ b/tests/Kernel/HttpClient/HttpClientMethodsTest.php @@ -51,6 +51,11 @@ 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() @@ -58,7 +63,9 @@ 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()); @@ -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()