From c60e2cb439592523ac521f4d174b63a6284f8a83 Mon Sep 17 00:00:00 2001 From: Dennis Eilander Date: Fri, 8 Sep 2023 23:57:07 +0200 Subject: [PATCH 1/2] Add a newResponse method to the PendingRequest --- src/Illuminate/Http/Client/PendingRequest.php | 19 ++++++-- tests/Http/HttpClientTest.php | 44 +++++++++++++++++++ 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 246ac950797a..2ae31b70ccb9 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -889,7 +889,7 @@ public function send(string $method, string $url, array $options = []) return retry($this->tries ?? 1, function ($attempt) use ($method, $url, $options, &$shouldRetry) { try { - return tap(new Response($this->sendRequest($method, $url, $options)), function ($response) use ($attempt, &$shouldRetry) { + return tap($this->newResponse($this->sendRequest($method, $url, $options)), function ($response) use ($attempt, &$shouldRetry) { $this->populateResponse($response); $this->dispatchResponseReceivedEvent($response); @@ -1001,16 +1001,27 @@ protected function makePromise(string $method, string $url, array $options = []) { return $this->promise = $this->sendRequest($method, $url, $options) ->then(function (MessageInterface $message) { - return tap(new Response($message), function ($response) { + return tap($this->newResponse($message), function ($response) { $this->populateResponse($response); $this->dispatchResponseReceivedEvent($response); }); }) ->otherwise(function (TransferException $e) { - return $e instanceof RequestException && $e->hasResponse() ? $this->populateResponse(new Response($e->getResponse())) : $e; + return $e instanceof RequestException && $e->hasResponse() ? $this->populateResponse($this->newResponse($e->getResponse())) : $e; }); } + /** + * Return a new response instance with the response. + * + * @param MessageInterface $response + * @return Response + */ + protected function newResponse($response) + { + return new Response($response); + } + /** * Send a request either synchronously or asynchronously. * @@ -1193,7 +1204,7 @@ public function buildRecorderHandler() return $promise->then(function ($response) use ($request, $options) { $this->factory?->recordRequestResponsePair( (new Request($request))->withData($options['laravel_data']), - new Response($response) + $this->newResponse($response) ); return $response; diff --git a/tests/Http/HttpClientTest.php b/tests/Http/HttpClientTest.php index b8b4ca22554b..6d8ab2867ee4 100644 --- a/tests/Http/HttpClientTest.php +++ b/tests/Http/HttpClientTest.php @@ -2523,4 +2523,48 @@ public function testItCanAddResponseMiddleware() $this->assertSame('Bar', $responses[0]->header('X-Foo')); $this->assertSame('', $responses[1]->header('X-Foo')); } + + public function testItReturnsResponse(): void + { + $this->factory->fake([ + '*' => $this->factory::response('expected content'), + ]); + + $response = $this->factory->get('http://laravel.com'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertSame('expected content', $response->body()); + } + + public function testItCanReturnCustomResponseClass(): void + { + $factory = new CustomFactory; + + $factory->fake([ + '*' => $factory::response('expected content'), + ]); + + $response = $factory->get('http://laravel.fake'); + + $this->assertInstanceOf(TestResponse::class, $response); + $this->assertSame('expected content', $response->body()); + } +} + +class CustomFactory extends Factory +{ + protected function newPendingRequest() + { + return new class extends PendingRequest + { + protected function newResponse($response) + { + return new TestResponse($response); + } + }; + } +} + +class TestResponse extends Response +{ } From 117658f65b11a333f181a08a1510e324bcdd3a46 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 11 Sep 2023 09:23:03 -0500 Subject: [PATCH 2/2] formatting --- src/Illuminate/Http/Client/PendingRequest.php | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Illuminate/Http/Client/PendingRequest.php b/src/Illuminate/Http/Client/PendingRequest.php index 2ae31b70ccb9..a3a022c352f3 100644 --- a/src/Illuminate/Http/Client/PendingRequest.php +++ b/src/Illuminate/Http/Client/PendingRequest.php @@ -1011,17 +1011,6 @@ protected function makePromise(string $method, string $url, array $options = []) }); } - /** - * Return a new response instance with the response. - * - * @param MessageInterface $response - * @return Response - */ - protected function newResponse($response) - { - return new Response($response); - } - /** * Send a request either synchronously or asynchronously. * @@ -1309,6 +1298,17 @@ public function mergeOptions(...$options) ); } + /** + * Create a new response instance using the given PSR response. + * + * @param \Psr\Http\Message\MessageInterface $response + * @return Response + */ + protected function newResponse($response) + { + return new Response($response); + } + /** * Register a stub callable that will intercept requests and be able to return stub responses. *