Skip to content

Commit 1316c26

Browse files
committed
Merge branch 'send_received_event_in_async' into 8.x
2 parents 73bf339 + dd3848a commit 1316c26

File tree

3 files changed

+73
-9
lines changed

3 files changed

+73
-9
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -790,20 +790,42 @@ protected function populateResponse(Response $response)
790790
*/
791791
public function buildClient()
792792
{
793-
return $this->client = $this->client ?: new Client([
794-
'handler' => $this->buildHandlerStack(),
793+
return $this->client = $this->client ?: $this->createClient($this->buildHandlerStack());
794+
}
795+
796+
/**
797+
* Create new Guzzle client.
798+
*
799+
* @param \GuzzleHttp\HandlerStack $handlerStack
800+
* @return \GuzzleHttp\Client
801+
*/
802+
public function createClient($handlerStack)
803+
{
804+
return new Client([
805+
'handler' => $handlerStack,
795806
'cookies' => true,
796807
]);
797808
}
798809

799810
/**
800-
* Build the before sending handler stack.
811+
* Build the Guzzle client handler stack.
801812
*
802813
* @return \GuzzleHttp\HandlerStack
803814
*/
804815
public function buildHandlerStack()
805816
{
806-
return tap(HandlerStack::create(), function ($stack) {
817+
return $this->pushHandlers(HandlerStack::create());
818+
}
819+
820+
/**
821+
* Add the necessary handlers to the given handler stack.
822+
*
823+
* @param \GuzzleHttp\HandlerStack $handlerStack
824+
* @return \GuzzleHttp\HandlerStack
825+
*/
826+
public function pushHandlers($handlerStack)
827+
{
828+
return tap($handlerStack, function ($stack) {
807829
$stack->push($this->buildBeforeSendingHandler());
808830
$stack->push($this->buildRecorderHandler());
809831
$stack->push($this->buildStubHandler());
@@ -1022,4 +1044,19 @@ public function setClient(Client $client)
10221044

10231045
return $this;
10241046
}
1047+
1048+
/**
1049+
* Create a new client instance using the given handler.
1050+
*
1051+
* @param callable $handler
1052+
* @return $this
1053+
*/
1054+
public function setHandler($handler)
1055+
{
1056+
$this->client = $this->createClient(
1057+
$this->pushHandlers(HandlerStack::create($handler))
1058+
);
1059+
1060+
return $this;
1061+
}
10251062
}

src/Illuminate/Http/Client/Pool.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Illuminate\Http\Client;
44

5+
use GuzzleHttp\Utils;
6+
57
/**
68
* @mixin \Illuminate\Http\Client\Factory
79
*/
@@ -15,11 +17,11 @@ class Pool
1517
protected $factory;
1618

1719
/**
18-
* The client instance.
20+
* The handler function for the Guzzle client.
1921
*
20-
* @var \GuzzleHttp\Client
22+
* @var callable
2123
*/
22-
protected $client;
24+
protected $handler;
2325

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

41-
$this->client = $this->factory->buildClient();
43+
if (method_exists(Utils::class, 'chooseHandler')) {
44+
$this->handler = Utils::chooseHandler();
45+
} else {
46+
$this->handler = \GuzzleHttp\choose_handler();
47+
}
4248
}
4349

4450
/**
@@ -59,7 +65,7 @@ public function as(string $key)
5965
*/
6066
protected function asyncRequest()
6167
{
62-
return $this->factory->setClient($this->client)->async();
68+
return $this->factory->setHandler($this->handler)->async();
6369
}
6470

6571
/**

tests/Http/HttpClientTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,27 @@ public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenAReque
940940
m::close();
941941
}
942942

943+
public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenARequestIsSentAsync()
944+
{
945+
$events = m::mock(Dispatcher::class);
946+
$events->shouldReceive('dispatch')->times(5)->with(m::type(RequestSending::class));
947+
$events->shouldReceive('dispatch')->times(5)->with(m::type(ResponseReceived::class));
948+
949+
$factory = new Factory($events);
950+
$factory->fake();
951+
$factory->pool(function (Pool $pool) {
952+
return [
953+
$pool->get('https://example.com'),
954+
$pool->head('https://example.com'),
955+
$pool->post('https://example.com'),
956+
$pool->patch('https://example.com'),
957+
$pool->delete('https://example.com'),
958+
];
959+
});
960+
961+
m::close();
962+
}
963+
943964
public function testTheTransferStatsAreCalledSafelyWhenFakingTheRequest()
944965
{
945966
$this->factory->fake(['https://example.com' => ['world' => 'Hello world']]);

0 commit comments

Comments
 (0)