From b04f77752781066b7b5901b5162dedd3b3fb1aa8 Mon Sep 17 00:00:00 2001 From: Jan Stolle Date: Tue, 16 Jan 2024 11:43:07 +0100 Subject: [PATCH 1/2] Allow StreamInterface as raw HTTP Client body Since the body payload is passed through to Guzzle, which in turn can handle Streams as well, this extends the versatility of the HTTP Client --- src/Illuminate/Http/Client/PendingRequest.php | 4 ++-- tests/Http/HttpClientTest.php | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 78e361feffad..80b0fe3e14ef 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -78,7 +78,7 @@ class PendingRequest /** * The raw body for the request. * - * @var string + * @var string|\Psr\Http\Message\StreamInterface */ protected $pendingBody; @@ -259,7 +259,7 @@ public function baseUrl(string $url) /** * Attach a raw body to the request. * - * @param string $content + * @param string|\Psr\Http\Message\StreamInterface $content * @param string $contentType * @return $this */ diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index df4f7165373c..6b790aa06534 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -6,6 +6,7 @@ use GuzzleHttp\Middleware; use GuzzleHttp\Promise\PromiseInterface; use GuzzleHttp\Psr7\Response as Psr7Response; +use GuzzleHttp\Psr7\Utils; use GuzzleHttp\TransferStats; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Support\Arrayable; @@ -357,6 +358,26 @@ public function testSendRequestBodyWithManyAmpersands() $this->factory->withBody($body, 'text/plain')->send('post', 'http://foo.com/api'); } + public function testSendStreamRequestBody() + { + $string = 'Look at me, i am a stream!!'; + $resource = fopen('php://temp', 'w'); + fwrite($resource, $string); + rewind($resource); + $body = Utils::streamFor($resource); + + $fakeRequest = function (Request $request) use ($string) { + self::assertSame($string, $request->body()); + self::assertContains('text/plain', $request->header('Content-Type')); + + return ['my' => 'response']; + }; + + $this->factory->fake($fakeRequest); + + $this->factory->withBody($body, 'text/plain')->send('post', 'http://foo.com/api'); + } + public function testUrlsCanBeStubbedByPath() { $this->factory->fake([ From 3cdedc225e2e835933f8260b8a1634488e1bb5ac Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 16 Jan 2024 09:09:55 -0600 Subject: [PATCH 2/2] Update PendingRequest.php --- src/Illuminate/Http/Client/PendingRequest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 80b0fe3e14ef..28defb4d9607 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -78,7 +78,7 @@ class PendingRequest /** * The raw body for the request. * - * @var string|\Psr\Http\Message\StreamInterface + * @var \Psr\Http\Message\StreamInterface|string */ protected $pendingBody; @@ -259,7 +259,7 @@ public function baseUrl(string $url) /** * Attach a raw body to the request. * - * @param string|\Psr\Http\Message\StreamInterface $content + * @param \Psr\Http\Message\StreamInterface|string $content * @param string $contentType * @return $this */