Skip to content

Commit

Permalink
[10.x] Add newResponse method to PendingRequest (#48344)
Browse files Browse the repository at this point in the history
* Add a newResponse method to the PendingRequest

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
denniseilander and taylorotwell committed Sep 11, 2023
1 parent 27ac244 commit b8b2d9e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1001,13 +1001,13 @@ 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;
});
}

Expand Down Expand Up @@ -1193,7 +1193,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;
Expand Down Expand Up @@ -1298,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.
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Http/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}

0 comments on commit b8b2d9e

Please sign in to comment.