Skip to content

Use http based promise in emulated trait and specs #42

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 4 commits into from
Sep 1, 2016
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions spec/EmulatedHttpAsyncClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
12 changes: 6 additions & 6 deletions spec/HttpClientPoolItemSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions spec/Plugin/AddHostPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 4 additions & 4 deletions spec/Plugin/CookiePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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();
}
}
12 changes: 6 additions & 6 deletions spec/Plugin/DecoderPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions spec/Plugin/ErrorPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand All @@ -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();
}

Expand All @@ -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');
}
}
8 changes: 4 additions & 4 deletions spec/Plugin/HistoryPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
};

Expand All @@ -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);
}
};

Expand Down
Loading