Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Async fix #44179

Merged
merged 3 commits into from
Sep 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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