From 9448d7ecfa7fa837d9a58f00762fc09de5b1f794 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Fri, 4 Jun 2021 19:26:39 +0100 Subject: [PATCH] [8.x] Makes the retrieval of Http client transferStats safe (#37597) * Wraps the call to retrieve transferStats in an optional and returns an array by default. * CS fix * Adds support for `effectiveUri` --- src/Illuminate/Http/Client/Response.php | 6 +++--- tests/Http/HttpClientTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Http/Client/Response.php b/src/Illuminate/Http/Client/Response.php index 6d5a564ffccd..4326735f7dda 100644 --- a/src/Illuminate/Http/Client/Response.php +++ b/src/Illuminate/Http/Client/Response.php @@ -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(); } /** @@ -224,7 +224,7 @@ public function cookies() */ public function handlerStats() { - return $this->transferStats->getHandlerStats(); + return optional($this->transferStats)->getHandlerStats() ?? []; } /** diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index 746e5ece7f68..bcec0e60488e 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -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); + } }