Skip to content

Commit

Permalink
[9.x] Http client: dispatch "response received" event for every retry…
Browse files Browse the repository at this point in the history
… attempt (#41793)

* Add failing test

* Dispatch event for every retry response
  • Loading branch information
gdebrauwer authored Apr 2, 2022
1 parent f0537b5 commit 6bec179
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ public function send(string $method, string $url, array $options = [])
return tap(new Response($this->sendRequest($method, $url, $options)), function ($response) use ($attempt, &$shouldRetry) {
$this->populateResponse($response);

$this->dispatchResponseReceivedEvent($response);

if (! $response->successful()) {
$shouldRetry = $this->retryWhenCallback ? call_user_func($this->retryWhenCallback, $response->toException()) : true;

Expand All @@ -725,8 +727,6 @@ public function send(string $method, string $url, array $options = [])
$response->throw();
}
}

$this->dispatchResponseReceivedEvent($response);
});
} catch (ConnectException $e) {
$this->dispatchConnectionFailedEvent();
Expand Down
18 changes: 18 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,24 @@ public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenAReque
});
}

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

$factory = new Factory($events);
$factory->fake([
'*' => $factory->response(['error'], 403),
]);

$response = $factory->retry(2, 1000, null, false)->get('http://foo.com/get');

$this->assertTrue($response->failed());

$factory->assertSentCount(2);
}

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

0 comments on commit 6bec179

Please sign in to comment.