Skip to content

Use client tools and new promise package #16

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
Dec 14, 2015
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
],
"require": {
"php": ">=5.4",
"php-http/httplug": "^1.0@dev",
"php-http/utils": "^0.1@dev"
"php-http/httplug": "1.0.0-alpha3",
"php-http/client-tools": "^0.1@dev"
},
"require-dev": {
"phpspec/phpspec": "^2.4-alpha",
Expand Down
3 changes: 1 addition & 2 deletions spec/AuthenticationPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace spec\Http\Client\Plugin;

use Http\Authentication\Authentication;
use Http\Client\Promise;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
Expand All @@ -29,7 +29,6 @@ function it_sends_an_authenticated_request(Authentication $authentication, Reque
{
$authentication->authenticate($notAuthedRequest)->willReturn($authedRequest);


$next = function (RequestInterface $request) use($authedRequest, $promise) {
if (Argument::is($authedRequest->getWrappedObject())->scoreArgument($request)) {
return $promise->getWrappedObject();
Expand Down
8 changes: 4 additions & 4 deletions spec/CookiePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace spec\Http\Client\Plugin;

use Http\Client\Promise;
use Http\Client\Utils\Promise\FulfilledPromise;
use Http\Client\Tools\Promise\FulfilledPromise;
use Http\Cookie\Cookie;
use Http\Cookie\CookieJar;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
Expand Down Expand Up @@ -155,7 +155,7 @@ function it_saves_cookie(CookieJar $cookieJar, RequestInterface $request, Respon
$uri->getPath()->willReturn('/');

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldHaveType('Http\Client\Promise');
$promise->getResponse()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
$promise->shouldHaveType('Http\Promise\Promise');
$promise->wait()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
}
}
2 changes: 1 addition & 1 deletion spec/DecoderPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace spec\Http\Client\Plugin;

use Http\Client\Utils\Promise\FulfilledPromise;
use Http\Client\Tools\Promise\FulfilledPromise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
Expand Down
8 changes: 4 additions & 4 deletions spec/ErrorPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace spec\Http\Client\Plugin;

use Http\Client\Utils\Promise\FulfilledPromise;
use Http\Client\Tools\Promise\FulfilledPromise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
Expand Down Expand Up @@ -30,7 +30,7 @@ function it_throw_request_exception_on_500_error(RequestInterface $request, Resp
}
};

$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
}

function it_returns_response(RequestInterface $request, ResponseInterface $response)
Expand All @@ -43,7 +43,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo
}
};

$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
}

function it_throws_request_exception_on_custom_regex(RequestInterface $request, ResponseInterface $response)
Expand All @@ -57,6 +57,6 @@ function it_throws_request_exception_on_custom_regex(RequestInterface $request,
}
};

$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
}
}
5 changes: 2 additions & 3 deletions spec/LoggerPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use Http\Client\Exception\HttpException;
use Http\Client\Exception\NetworkException;
use Http\Client\Plugin\Normalizer\Normalizer;
use Http\Client\Utils\Promise\FulfilledPromise;
use Http\Client\Utils\Promise\RejectedPromise;
use Http\Client\Tools\Promise\FulfilledPromise;
use Http\Client\Tools\Promise\RejectedPromise;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
Expand Down
2 changes: 1 addition & 1 deletion spec/PluginClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Client\Promise;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
Expand Down
43 changes: 19 additions & 24 deletions spec/RedirectPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace spec\Http\Client\Plugin;

use Http\Client\Utils\Promise\FulfilledPromise;
use Http\Client\Plugin\RedirectPlugin;
use Http\Client\Promise;
use Http\Client\Tools\Promise\FulfilledPromise;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
Expand Down Expand Up @@ -59,13 +59,12 @@ function it_redirects_on_302(
}
};

$promise->wait()->shouldBeCalled();
$promise->getState()->willReturn(Promise::FULFILLED);
$promise->getResponse()->willReturn($finalResponse);
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);

$finalPromise = $this->handleRequest($request, $next, $first);
$finalPromise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
$finalPromise->getResponse()->shouldReturn($finalResponse);
$finalPromise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
$finalPromise->wait()->shouldReturn($finalResponse);
}

function it_use_storage_on_301(UriInterface $uriRedirect, RequestInterface $request, RequestInterface $modifiedRequest)
Expand Down Expand Up @@ -127,9 +126,8 @@ function it_stores_a_301(
}
};

$promise->wait()->shouldBeCalled();
$promise->getState()->willReturn(Promise::FULFILLED);
$promise->getResponse()->willReturn($finalResponse);
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);

$this->handleRequest($request, $next, $first);
$this->hasStorage('/301-url')->shouldReturn(true);
Expand Down Expand Up @@ -175,9 +173,8 @@ function it_replace_full_url(
}
};

$promise->wait()->shouldBeCalled();
$promise->getState()->willReturn(Promise::FULFILLED);
$promise->getResponse()->willReturn($finalResponse);
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);

$this->handleRequest($request, $next, $first);
}
Expand All @@ -195,8 +192,8 @@ function it_throws_http_exception_on_no_location(RequestInterface $request, Resp
$responseRedirect->hasHeader('Location')->willReturn(false);

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Exception\HttpException');
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
$promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait();
}

function it_throws_http_exception_on_invalid_location(RequestInterface $request, ResponseInterface $responseRedirect)
Expand All @@ -214,8 +211,8 @@ function it_throws_http_exception_on_invalid_location(RequestInterface $request,
$responseRedirect->hasHeader('Location')->willReturn(true);

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Exception\HttpException');
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
$promise->shouldThrow('Http\Client\Exception\HttpException')->duringWait();
}

function it_throw_multi_redirect_exception_on_300(RequestInterface $request, ResponseInterface $responseRedirect)
Expand All @@ -230,8 +227,8 @@ function it_throw_multi_redirect_exception_on_300(RequestInterface $request, Res
$responseRedirect->getStatusCode()->willReturn('300');

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\MultipleRedirectionException');
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
$promise->shouldThrow('Http\Client\Plugin\Exception\MultipleRedirectionException')->duringWait();
}

function it_throw_multi_redirect_exception_on_300_if_no_location(RequestInterface $request, ResponseInterface $responseRedirect)
Expand All @@ -246,8 +243,8 @@ 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\Client\Utils\Promise\RejectedPromise');
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\MultipleRedirectionException');
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
$promise->shouldThrow('Http\Client\Plugin\Exception\MultipleRedirectionException')->duringWait();
}

function it_switch_method_for_302(
Expand Down Expand Up @@ -289,9 +286,8 @@ function it_switch_method_for_302(
}
};

$promise->wait()->shouldBeCalled();
$promise->getState()->willReturn(Promise::FULFILLED);
$promise->getResponse()->willReturn($finalResponse);
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);

$this->handleRequest($request, $next, $first);
}
Expand Down Expand Up @@ -338,9 +334,8 @@ function it_clears_headers(
}
};

$promise->wait()->shouldBeCalled();
$promise->getState()->willReturn(Promise::FULFILLED);
$promise->getResponse()->willReturn($finalResponse);
$promise->wait()->shouldBeCalled()->willReturn($finalResponse);

$this->handleRequest($request, $next, $first);
}
Expand Down Expand Up @@ -375,8 +370,8 @@ function it_throws_circular_redirection_exception(UriInterface $uri, UriInterfac
};

$promise = $this->handleRequest($request, $next, $first);
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
$promise->getException()->shouldReturnAnInstanceOf('Http\Client\Plugin\Exception\CircularRedirectionException');
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
$promise->shouldThrow('Http\Client\Plugin\Exception\CircularRedirectionException')->duringWait();
}
}

Expand Down
18 changes: 9 additions & 9 deletions spec/RetryPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace spec\Http\Client\Plugin;

use Http\Client\Exception;
use Http\Client\Utils\Promise\FulfilledPromise;
use Http\Client\Utils\Promise\RejectedPromise;
use Http\Client\Tools\Promise\FulfilledPromise;
use Http\Client\Tools\Promise\RejectedPromise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
Expand All @@ -30,7 +30,7 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo
}
};

$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
}

function it_throws_exception_on_multiple_exceptions(RequestInterface $request)
Expand All @@ -53,8 +53,8 @@ function it_throws_exception_on_multiple_exceptions(RequestInterface $request)
};

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\RejectedPromise');
$promise->getException()->shouldReturn($exception2);
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\RejectedPromise');
$promise->shouldThrow($exception2)->duringWait();
}

function it_returns_response_on_second_try(RequestInterface $request, ResponseInterface $response)
Expand All @@ -76,8 +76,8 @@ function it_returns_response_on_second_try(RequestInterface $request, ResponseIn
};

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
$promise->getResponse()->shouldReturn($response);
$promise->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
$promise->wait()->shouldReturn($response);
}

function it_does_not_keep_history_of_old_failure(RequestInterface $request, ResponseInterface $response)
Expand All @@ -98,7 +98,7 @@ function it_does_not_keep_history_of_old_failure(RequestInterface $request, Resp
}
};

$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Tools\Promise\FulfilledPromise');
}
}
4 changes: 2 additions & 2 deletions spec/StopwatchPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace spec\Http\Client\Plugin;

use Http\Client\Exception\NetworkException;
use Http\Client\Utils\Promise\FulfilledPromise;
use Http\Client\Utils\Promise\RejectedPromise;
use Http\Client\Tools\Promise\FulfilledPromise;
use Http\Client\Tools\Promise\RejectedPromise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Stopwatch\Stopwatch;
Expand Down
22 changes: 22 additions & 0 deletions src/EmulateAsyncClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Http\Client\Plugin;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Client\Tools\HttpAsyncClientEmulator;
use Http\Client\Tools\HttpClientDecorator;

/**
* Emulate an async client
*/
class EmulateAsyncClient implements HttpClient, HttpAsyncClient
{
use HttpClientDecorator;
use HttpAsyncClientEmulator;

public function __construct(HttpClient $httpClient)
{
$this->httpClient = $httpClient;
}
}
11 changes: 2 additions & 9 deletions src/PluginClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Client\Plugin\Exception\RebootChainException;
use Http\Client\Promise;
use Http\Client\Utils\EmulateAsyncClient;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;

/**
Expand Down Expand Up @@ -55,13 +53,8 @@ public function __construct($client, array $plugins = [])
public function sendRequest(RequestInterface $request)
{
$promise = $this->sendAsyncRequest($request);
$promise->wait();

if ($promise->getState() == Promise::REJECTED) {
throw $promise->getException();
}

return $promise->getResponse();
return $promise->wait();
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/RedirectPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Http\Client\Exception\HttpException;
use Http\Client\Plugin\Exception\CircularRedirectionException;
use Http\Client\Plugin\Exception\MultipleRedirectionException;
use Http\Client\Promise;
use Http\Promise\Promise;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -152,13 +152,8 @@ public function handleRequest(RequestInterface $request, callable $next, callabl

// Call redirect request in synchrone
$redirectPromise = $first($redirectRequest);
$redirectPromise->wait();

if ($redirectPromise->getState() == Promise::REJECTED) {
throw $redirectPromise->getException();
}

return $redirectPromise->getResponse();
return $redirectPromise->wait();
});
}

Expand Down
Loading