Skip to content

Commit

Permalink
[8.x] Makes the retrieval of Http client transferStats safe (#37597)
Browse files Browse the repository at this point in the history
* Wraps the call to retrieve transferStats in an optional and returns an array by default.

* CS fix

* Adds support for `effectiveUri`
  • Loading branch information
lukeraymonddowning authored Jun 4, 2021
1 parent f0c6f4d commit 9448d7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/Illuminate/Http/Client/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ public function status()
/**
* Get the effective URI of the response.
*
* @return \Psr\Http\Message\UriInterface
* @return \Psr\Http\Message\UriInterface|null
*/
public function effectiveUri()
{
return $this->transferStats->getEffectiveUri();
return optional($this->transferStats)->getEffectiveUri();
}

/**
Expand Down Expand Up @@ -224,7 +224,7 @@ public function cookies()
*/
public function handlerStats()
{
return $this->transferStats->getHandlerStats();
return optional($this->transferStats)->getHandlerStats() ?? [];
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -939,4 +939,16 @@ public function testTheRequestSendingAndResponseReceivedEventsAreFiredWhenAReque

m::close();
}

public function testTheTransferStatsAreCalledSafelyWhenFakingTheRequest()
{
$this->factory->fake(['https://example.com' => $this->factory->response()]);
$stats = $this->factory->get('https://example.com')->handlerStats();
$effectiveUri = $this->factory->get('https://example.com')->effectiveUri();

$this->assertIsArray($stats);
$this->assertEmpty($stats);

$this->assertNull($effectiveUri);
}
}

0 comments on commit 9448d7e

Please sign in to comment.