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

[8,x] Share handler instead of client between requests in pool to ensure ResponseReceived events are dispatched in async HTTP Request #38380

Merged
merged 4 commits into from
Aug 16, 2021
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
45 changes: 40 additions & 5 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,20 +790,32 @@ protected function populateResponse(Response $response)
*/
public function buildClient()
{
return $this->client = $this->client ?: new Client([
'handler' => $this->buildHandlerStack(),
return $this->client = $this->client ?: $this->createClient($this->buildHandlerStack());
}

/**
* Create new Guzzle client.
*
* @param \GuzzleHttp\HandlerStack $handlerStack
* @return \GuzzleHttp\Client
*/
public function createClient($handlerStack)
{
return new Client([
'handler' => $handlerStack,
'cookies' => true,
]);
}

/**
* Build the before sending handler stack.
* add handlers to the handler stack.
*
* @param \GuzzleHttp\HandlerStack $handlerStack
* @return \GuzzleHttp\HandlerStack
*/
public function buildHandlerStack()
public function pushHandlers($handlerStack)
{
return tap(HandlerStack::create(), function ($stack) {
return tap($handlerStack, function ($stack) {
$stack->push($this->buildBeforeSendingHandler());
$stack->push($this->buildRecorderHandler());
$stack->push($this->buildStubHandler());
Expand All @@ -814,6 +826,16 @@ public function buildHandlerStack()
});
}

/**
* Build the before sending handler stack.
*
* @return \GuzzleHttp\HandlerStack
*/
public function buildHandlerStack()
{
return $this->pushHandlers(HandlerStack::create());
}

/**
* Build the before sending handler.
*
Expand Down Expand Up @@ -1022,4 +1044,17 @@ public function setClient(Client $client)

return $this;
}

/**
* set the handler function.
*
* @param callable $handler
* @return $this
*/
public function setHandler($handler)
{
$this->client = $this->createClient($this->pushHandlers(HandlerStack::create($handler)));

return $this;
}
}
16 changes: 11 additions & 5 deletions src/Illuminate/Http/Client/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Illuminate\Http\Client;

use GuzzleHttp\Utils;

/**
* @mixin \Illuminate\Http\Client\Factory
*/
Expand All @@ -15,11 +17,11 @@ class Pool
protected $factory;

/**
* The client instance.
* The handler function for Guzzle client.
*
* @var \GuzzleHttp\Client
* @var callable
*/
protected $client;
protected $handler;

/**
* The pool of requests.
Expand All @@ -38,7 +40,11 @@ public function __construct(Factory $factory = null)
{
$this->factory = $factory ?: new Factory();

$this->client = $this->factory->buildClient();
if (method_exists(Utils::class, 'chooseHandler')) {
$this->handler = Utils::chooseHandler();
} else {
$this->handler = \GuzzleHttp\choose_handler();
}
}

/**
Expand All @@ -59,7 +65,7 @@ public function as(string $key)
*/
protected function asyncRequest()
{
return $this->factory->setClient($this->client)->async();
return $this->factory->setHandler($this->handler)->async();
}

/**
Expand Down
21 changes: 21 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,27 @@ public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenAReque
m::close();
}

public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenARequestIsSentAsync()
{
$events = m::mock(Dispatcher::class);
$events->shouldReceive('dispatch')->times(5)->with(m::type(RequestSending::class));
$events->shouldReceive('dispatch')->times(5)->with(m::type(ResponseReceived::class));

$factory = new Factory($events);
$factory->fake();
$factory->pool(function (Pool $pool) {
return [
$pool->get('https://example.com'),
$pool->head('https://example.com'),
$pool->post('https://example.com'),
$pool->patch('https://example.com'),
$pool->delete('https://example.com'),
];
});

m::close();
}

public function testTheTransferStatsAreCalledSafelyWhenFakingTheRequest()
{
$this->factory->fake(['https://example.com' => ['world' => 'Hello world']]);
Expand Down