From b8989240a401628e539096ff9b18558b72515258 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 16 Sep 2022 20:41:14 -0500 Subject: [PATCH] Async fix (#44179) * async pass * add test * Apply fixes from StyleCI Co-authored-by: StyleCI Bot --- src/Illuminate/Http/Client/PendingRequest.php | 17 +++++++------ tests/Http/HttpClientTest.php | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 695f29bb6a59..32ef2a6f2b29 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -42,6 +42,13 @@ class PendingRequest */ protected $client; + /** + * The Guzzle HTTP handler. + * + * @var callable + */ + protected $handler; + /** * The base URL for the request. * @@ -966,9 +973,7 @@ protected function populateResponse(Response $response) */ public function buildClient() { - return $this->requestsReusableClient() - ? $this->getReusableClient() - : $this->createClient($this->buildHandlerStack()); + return $this->client ?? $this->createClient($this->buildHandlerStack()); } /** @@ -1012,7 +1017,7 @@ public function createClient($handlerStack) */ public function buildHandlerStack() { - return $this->pushHandlers(HandlerStack::create()); + return $this->pushHandlers(HandlerStack::create($this->handler)); } /** @@ -1278,9 +1283,7 @@ public function setClient(Client $client) */ public function setHandler($handler) { - $this->client = $this->createClient( - $this->pushHandlers(HandlerStack::create($handler)) - ); + $this->handler = $handler; return $this; } diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index ab3de3de7fce..c6fdcb5bcca2 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -1139,6 +1139,31 @@ public function testMultipleRequestsAreSentInThePoolWithKeys() $this->assertSame(500, $responses['test500']->status()); } + public function testMiddlewareRunsInPool() + { + $this->factory->fake(function (Request $request) { + return $this->factory->response('Fake'); + }); + + $history = []; + + $middleware = Middleware::history($history); + + $responses = $this->factory->pool(fn (Pool $pool) => [ + $pool->withMiddleware($middleware)->post('https://example.com', ['hyped-for' => 'laravel-movie']), + ]); + + $response = $responses[0]; + + $this->assertSame('Fake', $response->body()); + + $this->assertCount(1, $history); + + $this->assertSame('Fake', tap($history[0]['response']->getBody())->rewind()->getContents()); + + $this->assertSame(['hyped-for' => 'laravel-movie'], json_decode(tap($history[0]['request']->getBody())->rewind()->getContents(), true)); + } + public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenARequestIsSent() { $events = m::mock(Dispatcher::class);