Skip to content

Commit b8b2d9e

Browse files
[10.x] Add newResponse method to PendingRequest (#48344)
* Add a newResponse method to the PendingRequest * formatting --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent 27ac244 commit b8b2d9e

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ public function send(string $method, string $url, array $options = [])
889889

890890
return retry($this->tries ?? 1, function ($attempt) use ($method, $url, $options, &$shouldRetry) {
891891
try {
892-
return tap(new Response($this->sendRequest($method, $url, $options)), function ($response) use ($attempt, &$shouldRetry) {
892+
return tap($this->newResponse($this->sendRequest($method, $url, $options)), function ($response) use ($attempt, &$shouldRetry) {
893893
$this->populateResponse($response);
894894

895895
$this->dispatchResponseReceivedEvent($response);
@@ -1001,13 +1001,13 @@ protected function makePromise(string $method, string $url, array $options = [])
10011001
{
10021002
return $this->promise = $this->sendRequest($method, $url, $options)
10031003
->then(function (MessageInterface $message) {
1004-
return tap(new Response($message), function ($response) {
1004+
return tap($this->newResponse($message), function ($response) {
10051005
$this->populateResponse($response);
10061006
$this->dispatchResponseReceivedEvent($response);
10071007
});
10081008
})
10091009
->otherwise(function (TransferException $e) {
1010-
return $e instanceof RequestException && $e->hasResponse() ? $this->populateResponse(new Response($e->getResponse())) : $e;
1010+
return $e instanceof RequestException && $e->hasResponse() ? $this->populateResponse($this->newResponse($e->getResponse())) : $e;
10111011
});
10121012
}
10131013

@@ -1193,7 +1193,7 @@ public function buildRecorderHandler()
11931193
return $promise->then(function ($response) use ($request, $options) {
11941194
$this->factory?->recordRequestResponsePair(
11951195
(new Request($request))->withData($options['laravel_data']),
1196-
new Response($response)
1196+
$this->newResponse($response)
11971197
);
11981198

11991199
return $response;
@@ -1298,6 +1298,17 @@ public function mergeOptions(...$options)
12981298
);
12991299
}
13001300

1301+
/**
1302+
* Create a new response instance using the given PSR response.
1303+
*
1304+
* @param \Psr\Http\Message\MessageInterface $response
1305+
* @return Response
1306+
*/
1307+
protected function newResponse($response)
1308+
{
1309+
return new Response($response);
1310+
}
1311+
13011312
/**
13021313
* Register a stub callable that will intercept requests and be able to return stub responses.
13031314
*

tests/Http/HttpClientTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,4 +2523,48 @@ public function testItCanAddResponseMiddleware()
25232523
$this->assertSame('Bar', $responses[0]->header('X-Foo'));
25242524
$this->assertSame('', $responses[1]->header('X-Foo'));
25252525
}
2526+
2527+
public function testItReturnsResponse(): void
2528+
{
2529+
$this->factory->fake([
2530+
'*' => $this->factory::response('expected content'),
2531+
]);
2532+
2533+
$response = $this->factory->get('http://laravel.com');
2534+
2535+
$this->assertInstanceOf(Response::class, $response);
2536+
$this->assertSame('expected content', $response->body());
2537+
}
2538+
2539+
public function testItCanReturnCustomResponseClass(): void
2540+
{
2541+
$factory = new CustomFactory;
2542+
2543+
$factory->fake([
2544+
'*' => $factory::response('expected content'),
2545+
]);
2546+
2547+
$response = $factory->get('http://laravel.fake');
2548+
2549+
$this->assertInstanceOf(TestResponse::class, $response);
2550+
$this->assertSame('expected content', $response->body());
2551+
}
2552+
}
2553+
2554+
class CustomFactory extends Factory
2555+
{
2556+
protected function newPendingRequest()
2557+
{
2558+
return new class extends PendingRequest
2559+
{
2560+
protected function newResponse($response)
2561+
{
2562+
return new TestResponse($response);
2563+
}
2564+
};
2565+
}
2566+
}
2567+
2568+
class TestResponse extends Response
2569+
{
25262570
}

0 commit comments

Comments
 (0)