Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] Fix promise behavior in HttplugClient #37491

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpClient/HttplugClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\HttpClient;

use GuzzleHttp\Promise\Promise as GuzzlePromise;
use GuzzleHttp\Promise\RejectedPromise;
use Http\Client\Exception\NetworkException;
use Http\Client\Exception\RequestException;
use Http\Client\HttpAsyncClient;
Expand All @@ -22,7 +23,6 @@
use Http\Message\StreamFactory;
use Http\Message\UriFactory;
use Http\Promise\Promise;
use Http\Promise\RejectedPromise;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Request;
use Nyholm\Psr7\Uri;
Expand Down Expand Up @@ -114,7 +114,7 @@ public function sendAsyncRequest(RequestInterface $request): Promise
try {
$response = $this->sendPsr7Request($request, true);
} catch (NetworkException $e) {
return new RejectedPromise($e);
return new HttplugPromise(new RejectedPromise($e));
}

$waitLoop = $this->waitLoop;
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/HttpClient/Response/HttplugPromise.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public function getState(): string
*/
public function wait($unwrap = true)
{
return $this->promise->wait($unwrap);
$result = $this->promise->wait($unwrap);

while ($result instanceof HttplugPromiseInterface || $result instanceof GuzzlePromiseInterface) {
$result = $result->wait($unwrap);
}

return $result;
}
}
115 changes: 115 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttplugClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,18 @@

namespace Symfony\Component\HttpClient\Tests;

use GuzzleHttp\Promise\FulfilledPromise as GuzzleFulfilledPromise;
use Http\Client\Exception\NetworkException;
use Http\Client\Exception\RequestException;
use Http\Promise\FulfilledPromise;
use Http\Promise\Promise;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\HttplugClient;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\NativeHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Contracts\HttpClient\Test\TestHttpServer;

class HttplugClientTest extends TestCase
Expand Down Expand Up @@ -152,4 +157,114 @@ public function testRequestException()
$this->expectException(RequestException::class);
$client->sendRequest($client->createRequest('BAD.METHOD', 'http://localhost:8057'));
}

public function testRetry404()
{
$client = new HttplugClient(new NativeHttpClient());

$successCallableCalled = false;
$failureCallableCalled = false;

$promise = $client
->sendAsyncRequest($client->createRequest('GET', 'http://localhost:8057/404'))
->then(
function (ResponseInterface $response) use (&$successCallableCalled, $client) {
$this->assertSame(404, $response->getStatusCode());
$successCallableCalled = true;

return $client->sendAsyncRequest($client->createRequest('GET', 'http://localhost:8057'));
},
function (\Exception $exception) use (&$failureCallableCalled) {
$failureCallableCalled = true;

throw $exception;
}
)
;

$response = $promise->wait(true);

$this->assertTrue($successCallableCalled);
$this->assertFalse($failureCallableCalled);
$this->assertSame(200, $response->getStatusCode());
}

public function testRetryNetworkError()
{
$client = new HttplugClient(new NativeHttpClient());

$successCallableCalled = false;
$failureCallableCalled = false;

$promise = $client
->sendAsyncRequest($client->createRequest('GET', 'http://localhost:8057/chunked-broken'))
->then(function (ResponseInterface $response) use (&$successCallableCalled) {
$successCallableCalled = true;

return $response;
}, function (\Exception $exception) use (&$failureCallableCalled, $client) {
$this->assertSame(NetworkException::class, \get_class($exception));
$this->assertSame(TransportException::class, \get_class($exception->getPrevious()));
$failureCallableCalled = true;

return $client->sendAsyncRequest($client->createRequest('GET', 'http://localhost:8057'));
})
;

$response = $promise->wait(true);

$this->assertFalse($successCallableCalled);
$this->assertTrue($failureCallableCalled);
$this->assertSame(200, $response->getStatusCode());
}

public function testRetryEarlierError()
{
$isFirstRequest = true;
$errorMessage = 'Error occurred before making the actual request.';

$client = new HttplugClient(new MockHttpClient(function () use (&$isFirstRequest, $errorMessage) {
if ($isFirstRequest) {
$isFirstRequest = false;
throw new TransportException($errorMessage);
}

return new MockResponse('OK', ['http_code' => 200]);
}));

$request = $client->createRequest('GET', 'http://test');

$successCallableCalled = false;
$failureCallableCalled = false;

$promise = $client
->sendAsyncRequest($request)
->then(
function (ResponseInterface $response) use (&$successCallableCalled) {
$successCallableCalled = true;

return $response;
},
function (\Exception $exception) use ($errorMessage, &$failureCallableCalled, $client, $request) {
$this->assertSame(NetworkException::class, \get_class($exception));
$this->assertSame($errorMessage, $exception->getMessage());
$failureCallableCalled = true;

// Ensure arbitrary levels of promises work.
return (new FulfilledPromise(null))->then(function () use ($client, $request) {
return (new GuzzleFulfilledPromise(null))->then(function () use ($client, $request) {
return $client->sendAsyncRequest($request);
});
});
}
)
;

$response = $promise->wait(true);

$this->assertFalse($successCallableCalled);
$this->assertTrue($failureCallableCalled);
$this->assertSame(200, $response->getStatusCode());
$this->assertSame('OK', (string) $response->getBody());
}
}