Skip to content

Commit b898924

Browse files
Async fix (#44179)
* async pass * add test * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <[email protected]>
1 parent 6299cd8 commit b898924

File tree

2 files changed

+35
-7
lines changed

2 files changed

+35
-7
lines changed

src/Illuminate/Http/Client/PendingRequest.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ class PendingRequest
4242
*/
4343
protected $client;
4444

45+
/**
46+
* The Guzzle HTTP handler.
47+
*
48+
* @var callable
49+
*/
50+
protected $handler;
51+
4552
/**
4653
* The base URL for the request.
4754
*
@@ -966,9 +973,7 @@ protected function populateResponse(Response $response)
966973
*/
967974
public function buildClient()
968975
{
969-
return $this->requestsReusableClient()
970-
? $this->getReusableClient()
971-
: $this->createClient($this->buildHandlerStack());
976+
return $this->client ?? $this->createClient($this->buildHandlerStack());
972977
}
973978

974979
/**
@@ -1012,7 +1017,7 @@ public function createClient($handlerStack)
10121017
*/
10131018
public function buildHandlerStack()
10141019
{
1015-
return $this->pushHandlers(HandlerStack::create());
1020+
return $this->pushHandlers(HandlerStack::create($this->handler));
10161021
}
10171022

10181023
/**
@@ -1278,9 +1283,7 @@ public function setClient(Client $client)
12781283
*/
12791284
public function setHandler($handler)
12801285
{
1281-
$this->client = $this->createClient(
1282-
$this->pushHandlers(HandlerStack::create($handler))
1283-
);
1286+
$this->handler = $handler;
12841287

12851288
return $this;
12861289
}

tests/Http/HttpClientTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,31 @@ public function testMultipleRequestsAreSentInThePoolWithKeys()
11391139
$this->assertSame(500, $responses['test500']->status());
11401140
}
11411141

1142+
public function testMiddlewareRunsInPool()
1143+
{
1144+
$this->factory->fake(function (Request $request) {
1145+
return $this->factory->response('Fake');
1146+
});
1147+
1148+
$history = [];
1149+
1150+
$middleware = Middleware::history($history);
1151+
1152+
$responses = $this->factory->pool(fn (Pool $pool) => [
1153+
$pool->withMiddleware($middleware)->post('https://example.com', ['hyped-for' => 'laravel-movie']),
1154+
]);
1155+
1156+
$response = $responses[0];
1157+
1158+
$this->assertSame('Fake', $response->body());
1159+
1160+
$this->assertCount(1, $history);
1161+
1162+
$this->assertSame('Fake', tap($history[0]['response']->getBody())->rewind()->getContents());
1163+
1164+
$this->assertSame(['hyped-for' => 'laravel-movie'], json_decode(tap($history[0]['request']->getBody())->rewind()->getContents(), true));
1165+
}
1166+
11421167
public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenARequestIsSent()
11431168
{
11441169
$events = m::mock(Dispatcher::class);

0 commit comments

Comments
 (0)