Skip to content

Commit

Permalink
Async fix (#44179)
Browse files Browse the repository at this point in the history
* async pass

* add test

* Apply fixes from StyleCI

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
taylorotwell and StyleCIBot authored Sep 17, 2022
1 parent 6299cd8 commit b898924
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ class PendingRequest
*/
protected $client;

/**
* The Guzzle HTTP handler.
*
* @var callable
*/
protected $handler;

/**
* The base URL for the request.
*
Expand Down Expand Up @@ -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());
}

/**
Expand Down Expand Up @@ -1012,7 +1017,7 @@ public function createClient($handlerStack)
*/
public function buildHandlerStack()
{
return $this->pushHandlers(HandlerStack::create());
return $this->pushHandlers(HandlerStack::create($this->handler));
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b898924

Please sign in to comment.