From 50d61a1dd67691bf99c3257dddf063d49b9efb01 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Wed, 31 Aug 2016 10:29:12 +0200 Subject: [PATCH 1/4] Use http based promise in emulated trait and specs --- CHANGELOG.md | 1 + composer.json | 2 +- spec/EmulatedHttpAsyncClientSpec.php | 4 ++-- spec/HttpClientPoolItemSpec.php | 12 +++++----- spec/Plugin/AddHostPluginSpec.php | 3 --- spec/Plugin/CookiePluginSpec.php | 8 +++---- spec/Plugin/DecoderPluginSpec.php | 12 +++++----- spec/Plugin/ErrorPluginSpec.php | 14 ++++++------ spec/Plugin/HistoryPluginSpec.php | 8 +++---- spec/Plugin/RedirectPluginSpec.php | 34 ++++++++++++++-------------- spec/Plugin/RetryPluginSpec.php | 28 +++++++++++------------ spec/PluginClientSpec.php | 3 ++- src/HttpAsyncClientEmulator.php | 6 ++--- src/PluginClient.php | 8 +++---- 14 files changed, 71 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a39ccf..e4588ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Add HttpClientPool client to leverage load balancing and fallback mechanism [see the documentation](http://docs.php-http.org/en/latest/components/client-common.html) for more details ### Changed +- Fix Emulated Trait to use Http based promise which respect the HttpAsyncClient interface - RedirectPlugin: use the full URL instead of the URI to properly keep track of redirects ## 1.2.1 - 2016-07-26 diff --git a/composer.json b/composer.json index c44509e..35e7f23 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "php": ">=5.4", - "php-http/httplug": "^1.0", + "php-http/httplug": "^1.1", "php-http/message-factory": "^1.0", "php-http/message": "^1.2", "symfony/options-resolver": "^2.6 || ^3.0" diff --git a/spec/EmulatedHttpAsyncClientSpec.php b/spec/EmulatedHttpAsyncClientSpec.php index 1ea8763..b7a9edd 100644 --- a/spec/EmulatedHttpAsyncClientSpec.php +++ b/spec/EmulatedHttpAsyncClientSpec.php @@ -36,14 +36,14 @@ function it_emulates_a_successful_request( ) { $httpClient->sendRequest($request)->willReturn($response); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); } function it_emulates_a_failed_request(HttpClient $httpClient, RequestInterface $request) { $httpClient->sendRequest($request)->willThrow('Http\Client\Exception\TransferException'); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); } function it_decorates_the_underlying_client( diff --git a/spec/HttpClientPoolItemSpec.php b/spec/HttpClientPoolItemSpec.php index 17b1bc4..059ec8d 100644 --- a/spec/HttpClientPoolItemSpec.php +++ b/spec/HttpClientPoolItemSpec.php @@ -6,8 +6,8 @@ use Http\Client\Exception\TransferException; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; +use Http\Client\Promise\HttpRejectedPromise; use Http\Promise\Promise; -use Http\Promise\RejectedPromise; use PhpSpec\ObjectBehavior; use Prophecy\Argument; use Psr\Http\Message\RequestInterface; @@ -60,10 +60,10 @@ public function it_disable_himself_on_send_async_request(HttpAsyncClient $httpAs { $this->beConstructedWith($httpAsyncClient); - $promise = new RejectedPromise(new TransferException()); + $promise = new HttpRejectedPromise(new TransferException()); $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $this->isDisabled()->shouldReturn(true); $this->shouldThrow('Http\Client\Exception\RequestException')->duringSendAsyncRequest($request); } @@ -84,12 +84,12 @@ public function it_reactivate_himself_on_send_async_request(HttpAsyncClient $htt { $this->beConstructedWith($httpAsyncClient, 0); - $promise = new RejectedPromise(new TransferException()); + $promise = new HttpRejectedPromise(new TransferException()); $httpAsyncClient->sendAsyncRequest($request)->willReturn($promise); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $this->isDisabled()->shouldReturn(false); - $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $this->sendAsyncRequest($request)->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); } public function it_increments_request_count(HttpAsyncClient $httpAsyncClient, RequestInterface $request, ResponseInterface $response) diff --git a/spec/Plugin/AddHostPluginSpec.php b/spec/Plugin/AddHostPluginSpec.php index a3fb2d6..caf4f21 100644 --- a/spec/Plugin/AddHostPluginSpec.php +++ b/spec/Plugin/AddHostPluginSpec.php @@ -4,10 +4,7 @@ use Http\Message\StreamFactory; use Http\Message\UriFactory; -use Http\Promise\FulfilledPromise; use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; -use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; use PhpSpec\ObjectBehavior; diff --git a/spec/Plugin/CookiePluginSpec.php b/spec/Plugin/CookiePluginSpec.php index 0ae5500..1851876 100644 --- a/spec/Plugin/CookiePluginSpec.php +++ b/spec/Plugin/CookiePluginSpec.php @@ -2,7 +2,7 @@ namespace spec\Http\Client\Common\Plugin; -use Http\Promise\FulfilledPromise; +use Http\Client\Promise\HttpFulfilledPromise; use Http\Message\Cookie; use Http\Message\CookieJar; use Http\Promise\Promise; @@ -141,7 +141,7 @@ function it_loads_cookie_when_cookie_is_secure(RequestInterface $request, UriInt function it_saves_cookie(RequestInterface $request, ResponseInterface $response, UriInterface $uri) { $next = function () use ($response) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); }; $response->hasHeader('Set-Cookie')->willReturn(true); @@ -164,7 +164,7 @@ function it_throws_exception_on_invalid_expires_date( UriInterface $uri ) { $next = function () use ($response) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); }; $response->hasHeader('Set-Cookie')->willReturn(true); @@ -177,7 +177,7 @@ function it_throws_exception_on_invalid_expires_date( $uri->getPath()->willReturn('/'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow('Http\Client\Exception\TransferException')->duringWait(); } } diff --git a/spec/Plugin/DecoderPluginSpec.php b/spec/Plugin/DecoderPluginSpec.php index bde3cf9..a4f8d7d 100644 --- a/spec/Plugin/DecoderPluginSpec.php +++ b/spec/Plugin/DecoderPluginSpec.php @@ -2,7 +2,7 @@ namespace spec\Http\Client\Common\Plugin; -use Http\Promise\FulfilledPromise; +use Http\Client\Promise\HttpFulfilledPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; @@ -31,7 +31,7 @@ function it_decodes(RequestInterface $request, ResponseInterface $response, Stre $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request); $next = function () use($response) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); }; $response->hasHeader('Transfer-Encoding')->willReturn(true); @@ -53,7 +53,7 @@ function it_decodes_gzip(RequestInterface $request, ResponseInterface $response, $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request); $next = function () use($response) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); }; $response->hasHeader('Transfer-Encoding')->willReturn(false); @@ -75,7 +75,7 @@ function it_decodes_deflate(RequestInterface $request, ResponseInterface $respon $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request); $next = function () use($response) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); }; $response->hasHeader('Transfer-Encoding')->willReturn(false); @@ -97,7 +97,7 @@ function it_decodes_inflate(RequestInterface $request, ResponseInterface $respon $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldBeCalled()->willReturn($request); $next = function () use($response) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); }; $response->hasHeader('Transfer-Encoding')->willReturn(false); @@ -121,7 +121,7 @@ function it_does_not_decode_with_content_encoding(RequestInterface $request, Res $request->withHeader('TE', ['gzip', 'deflate', 'compress', 'chunked'])->shouldBeCalled()->willReturn($request); $request->withHeader('Accept-Encoding', ['gzip', 'deflate', 'compress'])->shouldNotBeCalled(); $next = function () use($response) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); }; $response->hasHeader('Transfer-Encoding')->willReturn(false); diff --git a/spec/Plugin/ErrorPluginSpec.php b/spec/Plugin/ErrorPluginSpec.php index e9130d3..de17ca4 100644 --- a/spec/Plugin/ErrorPluginSpec.php +++ b/spec/Plugin/ErrorPluginSpec.php @@ -2,7 +2,7 @@ namespace spec\Http\Client\Common\Plugin; -use Http\Promise\FulfilledPromise; +use Http\Client\Promise\HttpFulfilledPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; @@ -27,12 +27,12 @@ function it_throw_client_error_exception_on_4xx_error(RequestInterface $request, $next = function (RequestInterface $receivedRequest) use($request, $response) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); } }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow('Http\Client\Common\Exception\ClientErrorException')->duringWait(); } @@ -43,12 +43,12 @@ function it_throw_server_error_exception_on_5xx_error(RequestInterface $request, $next = function (RequestInterface $receivedRequest) use($request, $response) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); } }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow('Http\Client\Common\Exception\ServerErrorException')->duringWait(); } @@ -58,10 +58,10 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo $next = function (RequestInterface $receivedRequest) use($request, $response) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); } } diff --git a/spec/Plugin/HistoryPluginSpec.php b/spec/Plugin/HistoryPluginSpec.php index b15742b..24e7f51 100644 --- a/spec/Plugin/HistoryPluginSpec.php +++ b/spec/Plugin/HistoryPluginSpec.php @@ -4,8 +4,8 @@ use Http\Client\Exception\TransferException; use Http\Client\Common\Plugin\Journal; -use Http\Promise\FulfilledPromise; -use Http\Promise\RejectedPromise; +use Http\Client\Promise\HttpFulfilledPromise; +use Http\Client\Promise\HttpRejectedPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; @@ -32,7 +32,7 @@ function it_records_success(Journal $journal, RequestInterface $request, Respons { $next = function (RequestInterface $receivedRequest) use($request, $response) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); } }; @@ -46,7 +46,7 @@ function it_records_failure(Journal $journal, RequestInterface $request) $exception = new TransferException(); $next = function (RequestInterface $receivedRequest) use($request, $exception) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new RejectedPromise($exception); + return new HttpRejectedPromise($exception); } }; diff --git a/spec/Plugin/RedirectPluginSpec.php b/spec/Plugin/RedirectPluginSpec.php index 7f2df64..bfb1375 100644 --- a/spec/Plugin/RedirectPluginSpec.php +++ b/spec/Plugin/RedirectPluginSpec.php @@ -3,7 +3,7 @@ namespace spec\Http\Client\Common\Plugin; use Http\Client\Common\Plugin\RedirectPlugin; -use Http\Promise\FulfilledPromise; +use Http\Client\Promise\HttpFulfilledPromise; use Http\Promise\Promise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -51,7 +51,7 @@ function it_redirects_on_302( $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -65,7 +65,7 @@ function it_redirects_on_302( $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $finalPromise = $this->handleRequest($request, $next, $first); - $finalPromise->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); + $finalPromise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); $finalPromise->wait()->shouldReturn($finalResponse); } @@ -123,7 +123,7 @@ function it_stores_a_301( $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -173,7 +173,7 @@ function it_replace_full_url( $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -193,7 +193,7 @@ function it_throws_http_exception_on_no_location(RequestInterface $request, UriI { $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -203,7 +203,7 @@ function it_throws_http_exception_on_no_location(RequestInterface $request, UriI $responseRedirect->hasHeader('Location')->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait(); } @@ -211,7 +211,7 @@ function it_throws_http_exception_on_invalid_location(RequestInterface $request, { $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -223,7 +223,7 @@ function it_throws_http_exception_on_invalid_location(RequestInterface $request, $responseRedirect->hasHeader('Location')->willReturn(true); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait(); } @@ -231,7 +231,7 @@ function it_throw_multi_redirect_exception_on_300(RequestInterface $request, Res { $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -239,7 +239,7 @@ function it_throw_multi_redirect_exception_on_300(RequestInterface $request, Res $responseRedirect->getStatusCode()->willReturn('300'); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow('Http\Client\Common\Exception\MultipleRedirectionException')->duringWait(); } @@ -247,7 +247,7 @@ function it_throw_multi_redirect_exception_on_300_if_no_location(RequestInterfac { $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -255,7 +255,7 @@ function it_throw_multi_redirect_exception_on_300_if_no_location(RequestInterfac $responseRedirect->hasHeader('Location')->willReturn(false); $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow('Http\Client\Common\Exception\MultipleRedirectionException')->duringWait(); } @@ -290,7 +290,7 @@ function it_switch_method_for_302( $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -340,7 +340,7 @@ function it_clears_headers( $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -381,12 +381,12 @@ function it_throws_circular_redirection_exception(UriInterface $uri, UriInterfac $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; $promise = $this->handleRequest($request, $next, $first); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow('Http\Client\Common\Exception\CircularRedirectionException')->duringWait(); } diff --git a/spec/Plugin/RetryPluginSpec.php b/spec/Plugin/RetryPluginSpec.php index ee1d001..981c16d 100644 --- a/spec/Plugin/RetryPluginSpec.php +++ b/spec/Plugin/RetryPluginSpec.php @@ -3,8 +3,8 @@ namespace spec\Http\Client\Common\Plugin; use Http\Client\Exception; -use Http\Promise\FulfilledPromise; -use Http\Promise\RejectedPromise; +use Http\Client\Promise\HttpFulfilledPromise; +use Http\Client\Promise\HttpRejectedPromise; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; use PhpSpec\ObjectBehavior; @@ -26,11 +26,11 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo { $next = function (RequestInterface $receivedRequest) use($request, $response) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); } function it_throws_exception_on_multiple_exceptions(RequestInterface $request) @@ -43,17 +43,17 @@ function it_throws_exception_on_multiple_exceptions(RequestInterface $request) $count++; if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { if ($count == 1) { - return new RejectedPromise($exception1); + return new HttpRejectedPromise($exception1); } if ($count == 2) { - return new RejectedPromise($exception2); + return new HttpRejectedPromise($exception2); } } }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\RejectedPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpRejectedPromise'); $promise->shouldThrow($exception2)->duringWait(); } @@ -66,17 +66,17 @@ function it_returns_response_on_second_try(RequestInterface $request, ResponseIn $count++; if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { if ($count == 1) { - return new RejectedPromise($exception); + return new HttpRejectedPromise($exception); } if ($count == 2) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); } } }; $promise = $this->handleRequest($request, $next, function () {}); - $promise->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); + $promise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); $promise->wait()->shouldReturn($response); } @@ -89,16 +89,16 @@ function it_does_not_keep_history_of_old_failure(RequestInterface $request, Resp $count++; if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { if ($count % 2 == 1) { - return new RejectedPromise($exception); + return new HttpRejectedPromise($exception); } if ($count % 2 == 0) { - return new FulfilledPromise($response->getWrappedObject()); + return new HttpFulfilledPromise($response->getWrappedObject()); } } }; - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); - $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); + $this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); } } diff --git a/spec/PluginClientSpec.php b/spec/PluginClientSpec.php index 27b4068..addb2e8 100644 --- a/spec/PluginClientSpec.php +++ b/spec/PluginClientSpec.php @@ -88,7 +88,7 @@ function it_throws_loop_exception(HttpClient $httpClient, RequestInterface $requ $this->shouldThrow('Http\Client\Common\Exception\LoopException')->duringSendRequest($request); } - function it_injects_debug_plugins(HttpClient $httpClient, RequestInterface $request, Plugin $plugin0, Plugin $plugin1, Plugin $debugPlugin) + function it_injects_debug_plugins(HttpClient $httpClient, ResponseInterface $response, RequestInterface $request, Plugin $plugin0, Plugin $plugin1, Plugin $debugPlugin) { $plugin0 ->handleRequest( @@ -125,6 +125,7 @@ function it_injects_debug_plugins(HttpClient $httpClient, RequestInterface $requ }) ; + $httpClient->sendRequest($request)->willReturn($response); $this->beConstructedWith($httpClient, [$plugin0, $plugin1], ['debug_plugins'=>[$debugPlugin]]); $this->sendRequest($request); diff --git a/src/HttpAsyncClientEmulator.php b/src/HttpAsyncClientEmulator.php index 9f527f4..c0ba354 100644 --- a/src/HttpAsyncClientEmulator.php +++ b/src/HttpAsyncClientEmulator.php @@ -3,7 +3,7 @@ namespace Http\Client\Common; use Http\Client\Exception; -use Http\Promise; +use Http\Client\Promise; use Psr\Http\Message\RequestInterface; /** @@ -28,9 +28,9 @@ abstract public function sendRequest(RequestInterface $request); public function sendAsyncRequest(RequestInterface $request) { try { - return new Promise\FulfilledPromise($this->sendRequest($request)); + return new Promise\HttpFulfilledPromise($this->sendRequest($request)); } catch (Exception $e) { - return new Promise\RejectedPromise($e); + return new Promise\HttpRejectedPromise($e); } } } diff --git a/src/PluginClient.php b/src/PluginClient.php index 2e69ecf..e11cfeb 100644 --- a/src/PluginClient.php +++ b/src/PluginClient.php @@ -6,8 +6,8 @@ use Http\Client\Exception as HttplugException; use Http\Client\HttpAsyncClient; use Http\Client\HttpClient; -use Http\Promise\FulfilledPromise; -use Http\Promise\RejectedPromise; +use Http\Client\Promise\HttpFulfilledPromise; +use Http\Client\Promise\HttpRejectedPromise; use Psr\Http\Message\RequestInterface; use Symfony\Component\OptionsResolver\OptionsResolver; @@ -78,9 +78,9 @@ public function sendRequest(RequestInterface $request) // we have both an async and sync call $pluginChain = $this->createPluginChain($this->plugins, function (RequestInterface $request) { try { - return new FulfilledPromise($this->client->sendRequest($request)); + return new HttpFulfilledPromise($this->client->sendRequest($request)); } catch (HttplugException $exception) { - return new RejectedPromise($exception); + return new HttpRejectedPromise($exception); } }); From cabbf75ce675ac8eda39ed94d867cc6c8955cf12 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Wed, 31 Aug 2016 10:44:29 +0200 Subject: [PATCH 2/4] Reflect the real intention on the promise returned by the Plugin --- src/Plugin.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugin.php b/src/Plugin.php index d53a670..38477df 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -24,7 +24,7 @@ interface Plugin * @param callable $next Next middleware in the chain, the request is passed as the first argument * @param callable $first First middleware in the chain, used to to restart a request * - * @return Promise + * @return Promise Resolves a PSR-7 Response or fails with an Http\Client\Exception (The same as HttpAsyncClient). */ public function handleRequest(RequestInterface $request, callable $next, callable $first); } From 4058410b998c85f0664c42557b35bb17615f434f Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Wed, 31 Aug 2016 14:48:34 +0200 Subject: [PATCH 3/4] Update tests with last changes on client-common --- spec/Plugin/RedirectPluginSpec.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/Plugin/RedirectPluginSpec.php b/spec/Plugin/RedirectPluginSpec.php index bfb1375..97197e1 100644 --- a/spec/Plugin/RedirectPluginSpec.php +++ b/spec/Plugin/RedirectPluginSpec.php @@ -419,7 +419,7 @@ function it_redirects_http_to_https( $next = function (RequestInterface $receivedRequest) use($request, $responseRedirect) { if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) { - return new FulfilledPromise($responseRedirect->getWrappedObject()); + return new HttpFulfilledPromise($responseRedirect->getWrappedObject()); } }; @@ -433,7 +433,7 @@ function it_redirects_http_to_https( $promise->wait()->shouldBeCalled()->willReturn($finalResponse); $finalPromise = $this->handleRequest($request, $next, $first); - $finalPromise->shouldReturnAnInstanceOf('Http\Promise\FulfilledPromise'); + $finalPromise->shouldReturnAnInstanceOf('Http\Client\Promise\HttpFulfilledPromise'); $finalPromise->wait()->shouldReturn($finalResponse); } } From e83dcce4f0134462a6ffa08ec240da81dea6d272 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Thu, 1 Sep 2016 15:48:42 +0200 Subject: [PATCH 4/4] Add link to documentation in plugin interface --- src/Plugin.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Plugin.php b/src/Plugin.php index 38477df..89a2a62 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -20,6 +20,8 @@ interface Plugin /** * Handle the request and return the response coming from the next callable. * + * @see http://docs.php-http.org/en/latest/plugins/build-your-own.html + * * @param RequestInterface $request * @param callable $next Next middleware in the chain, the request is passed as the first argument * @param callable $first First middleware in the chain, used to to restart a request