Skip to content

Commit

Permalink
Add newResponse method to PendingRequest (#335)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia authored Sep 11, 2023
1 parent 57e9ecc commit 0877f67
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/http-client/src/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,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 @@ -1039,7 +1039,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 @@ -1207,6 +1207,17 @@ public function getOptions()
return $this->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);
}

/**
* Substitute the URL parameters in the given URL.
*
Expand Down Expand Up @@ -1271,13 +1282,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
43 changes: 43 additions & 0 deletions tests/HttpClient/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2461,4 +2461,47 @@ 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 0877f67

Please sign in to comment.