From 6e5e599a32b35c666b6129cf9016f9ed3fc43d8d Mon Sep 17 00:00:00 2001 From: Septianata Rizky Pratama Date: Wed, 4 May 2022 01:18:29 +0700 Subject: [PATCH] [8.x] Enable to modify HTTP Client request headers when using beforeSending() callback (#42244) * Fix return docblock definition * Adding reference on $request parameter and retrieve the latest value from the given callback * Create unit test to assert if http factory can add authorization header using before sending callback --- src/Illuminate/Http/Client/PendingRequest.php | 15 +++++++++---- tests/Http/HttpClientTest.php | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 6dec6c95cd5f..fdf5f06d4d8a 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -17,6 +17,7 @@ use Illuminate\Support\Traits\Conditionable; use Illuminate\Support\Traits\Macroable; use Psr\Http\Message\MessageInterface; +use Psr\Http\Message\RequestInterface; use Symfony\Component\VarDumper\VarDumper; class PendingRequest @@ -984,15 +985,21 @@ protected function sinkStubHandler($sink) * * @param \GuzzleHttp\Psr7\RequestInterface $request * @param array $options - * @return \Closure + * @return \GuzzleHttp\Psr7\RequestInterface */ public function runBeforeSendingCallbacks($request, array $options) { - return tap($request, function ($request) use ($options) { - $this->beforeSendingCallbacks->each(function ($callback) use ($request, $options) { - call_user_func( + return tap($request, function (&$request) use ($options) { + $this->beforeSendingCallbacks->each(function ($callback) use (&$request, $options) { + $callbackResult = call_user_func( $callback, (new Request($request))->withData($options['laravel_data']), $options, $this ); + + if ($callbackResult instanceof RequestInterface) { + $request = $callbackResult; + } elseif ($callbackResult instanceof Request) { + $request = $callbackResult->toPsrRequest(); + } }); }); } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 338276362425..9101750f7de8 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -1080,4 +1080,26 @@ public function testRequestIsMacroable() $this->factory->get('https://example.com'); } + + public function testItCanAddAuthorizationHeaderIntoRequestUsingBeforeSendingCallback() + { + $this->factory->fake(); + + $this->factory->beforeSending(function (Request $request) { + $requestLine = sprintf( + '%s %s HTTP/%s', + $request->toPsrRequest()->getMethod(), + $request->toPsrRequest()->getUri()->withScheme('')->withHost(''), + $request->toPsrRequest()->getProtocolVersion() + ); + + return $request->toPsrRequest()->withHeader('Authorization', 'Bearer '.$requestLine); + })->get('http://foo.com/json'); + + $this->factory->assertSent(function (Request $request) { + return + $request->url() === 'http://foo.com/json' && + $request->hasHeader('Authorization', 'Bearer GET /json HTTP/1.1'); + }); + } }