Skip to content
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
16 changes: 12 additions & 4 deletions spec/AuthenticationPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,30 @@

use Http\Authentication\Authentication;
use Http\Client\Promise;
use Psr\Http\Message\RequestInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;

class AuthenticationPluginSpec extends ObjectBehavior
{
function let(Authentication $authentication)
{
$this->beConstructedWith($authentication);
}

function it_is_initializable(Authentication $authentication)
{
$this->beAnInstanceOf('Http\Client\Plugin\AuthenticationPlugin', [$authentication]);
$this->shouldHaveType('Http\Client\Plugin\AuthenticationPlugin');
}

function it_is_a_plugin()
{
$this->shouldImplement('Http\Client\Plugin\Plugin');
}

function it_sends_an_authenticated_request(Authentication $authentication, RequestInterface $notAuthedRequest, RequestInterface $authedRequest, Promise $promise)
{
$this->beConstructedWith($authentication);
$authentication->authenticate($notAuthedRequest)->shouldBeCalled()->willReturn($authedRequest);
$authentication->authenticate($notAuthedRequest)->willReturn($authedRequest);


$next = function (RequestInterface $request) use($authedRequest, $promise) {
Expand Down
89 changes: 45 additions & 44 deletions spec/CookiePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,39 @@
use Http\Client\Utils\Promise\FulfilledPromise;
use Http\Cookie\Cookie;
use Http\Cookie\CookieJar;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class CookiePluginSpec extends ObjectBehavior
{
function it_is_initializable(CookieJar $cookieJar)
function let(CookieJar $cookieJar)
{
$this->beConstructedWith($cookieJar);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Plugin\CookiePlugin');
}

function it_is_a_plugin()
{
$this->beAnInstanceOf('Http\Client\Plugin\CookiePlugin', [$cookieJar]);
$this->shouldImplement('Http\Client\Plugin\Plugin');
}

function it_loads_cookie(CookieJar $cookieJar, RequestInterface $request, UriInterface $uri, Promise $promise)
{
$cookie = new Cookie('name', 'value', (new \DateTime())->modify('+1day'), 'test.com');
$this->beConstructedWith($cookieJar);

$cookieJar->getCookies()->shouldBeCalled()->willReturn([$cookie]);
$request->getUri()->shouldBeCalled()->willReturn($uri);
$uri->getHost()->shouldBeCalled()->willReturn('test.com');
$uri->getPath()->shouldBeCalled()->willReturn('/');
$cookieJar->getCookies()->willReturn([$cookie]);
$request->getUri()->willReturn($uri);
$uri->getHost()->willReturn('test.com');
$uri->getPath()->willReturn('/');

$request->withAddedHeader('Cookie', 'name=value')->shouldBeCalled()->willReturn($request);
$request->withAddedHeader('Cookie', 'name=value')->willReturn($request);

$this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) {
if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) {
Expand All @@ -42,9 +50,8 @@ function it_loads_cookie(CookieJar $cookieJar, RequestInterface $request, UriInt
function it_does_not_load_cookie_if_expired(CookieJar $cookieJar, RequestInterface $request, UriInterface $uri, Promise $promise)
{
$cookie = new Cookie('name', 'value', (new \DateTime())->modify('-1day'), 'test.com');
$this->beConstructedWith($cookieJar);

$cookieJar->getCookies()->shouldBeCalled()->willReturn([$cookie]);
$cookieJar->getCookies()->willReturn([$cookie]);
$request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled();

$this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) {
Expand All @@ -57,11 +64,10 @@ function it_does_not_load_cookie_if_expired(CookieJar $cookieJar, RequestInterfa
function it_does_not_load_cookie_if_domain_does_not_match(CookieJar $cookieJar, RequestInterface $request, UriInterface $uri, Promise $promise)
{
$cookie = new Cookie('name', 'value', (new \DateTime())->modify('+1day'), 'test2.com');
$this->beConstructedWith($cookieJar);

$cookieJar->getCookies()->shouldBeCalled()->willReturn([$cookie]);
$request->getUri()->shouldBeCalled()->willReturn($uri);
$uri->getHost()->shouldBeCalled()->willReturn('test.com');
$cookieJar->getCookies()->willReturn([$cookie]);
$request->getUri()->willReturn($uri);
$uri->getHost()->willReturn('test.com');

$request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled();

Expand All @@ -75,12 +81,11 @@ function it_does_not_load_cookie_if_domain_does_not_match(CookieJar $cookieJar,
function it_does_not_load_cookie_if_path_does_not_match(CookieJar $cookieJar, RequestInterface $request, UriInterface $uri, Promise $promise)
{
$cookie = new Cookie('name', 'value', (new \DateTime())->modify('+1day'), 'test.com', '/sub');
$this->beConstructedWith($cookieJar);

$cookieJar->getCookies()->shouldBeCalled()->willReturn([$cookie]);
$request->getUri()->shouldBeCalled()->willReturn($uri);
$uri->getHost()->shouldBeCalled()->willReturn('test.com');
$uri->getPath()->shouldBeCalled()->willReturn('/');
$cookieJar->getCookies()->willReturn([$cookie]);
$request->getUri()->willReturn($uri);
$uri->getHost()->willReturn('test.com');
$uri->getPath()->willReturn('/');

$request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled();

Expand All @@ -94,13 +99,12 @@ function it_does_not_load_cookie_if_path_does_not_match(CookieJar $cookieJar, Re
function it_does_not_load_cookie_when_cookie_is_secure(CookieJar $cookieJar, RequestInterface $request, UriInterface $uri, Promise $promise)
{
$cookie = new Cookie('name', 'value', (new \DateTime())->modify('+1day'), 'test.com', null, true);
$this->beConstructedWith($cookieJar);

$cookieJar->getCookies()->shouldBeCalled()->willReturn([$cookie]);
$request->getUri()->shouldBeCalled()->willReturn($uri);
$uri->getHost()->shouldBeCalled()->willReturn('test.com');
$uri->getPath()->shouldBeCalled()->willReturn('/');
$uri->getScheme()->shouldBeCalled()->willReturn('http');
$cookieJar->getCookies()->willReturn([$cookie]);
$request->getUri()->willReturn($uri);
$uri->getHost()->willReturn('test.com');
$uri->getPath()->willReturn('/');
$uri->getScheme()->willReturn('http');

$request->withAddedHeader('Cookie', 'name=value')->shouldNotBeCalled();

Expand All @@ -114,15 +118,14 @@ function it_does_not_load_cookie_when_cookie_is_secure(CookieJar $cookieJar, Req
function it_loads_cookie_when_cookie_is_secure(CookieJar $cookieJar, RequestInterface $request, UriInterface $uri, Promise $promise)
{
$cookie = new Cookie('name', 'value', (new \DateTime())->modify('+1day'), 'test.com', null, true);
$this->beConstructedWith($cookieJar);

$cookieJar->getCookies()->shouldBeCalled()->willReturn([$cookie]);
$request->getUri()->shouldBeCalled()->willReturn($uri);
$uri->getHost()->shouldBeCalled()->willReturn('test.com');
$uri->getPath()->shouldBeCalled()->willReturn('/');
$uri->getScheme()->shouldBeCalled()->willReturn('https');
$cookieJar->getCookies()->willReturn([$cookie]);
$request->getUri()->willReturn($uri);
$uri->getHost()->willReturn('test.com');
$uri->getPath()->willReturn('/');
$uri->getScheme()->willReturn('https');

$request->withAddedHeader('Cookie', 'name=value')->shouldBeCalled()->willReturn($request);
$request->withAddedHeader('Cookie', 'name=value')->willReturn($request);

$this->handleRequest($request, function (RequestInterface $requestReceived) use ($request, $promise) {
if (Argument::is($requestReceived)->scoreArgument($request->getWrappedObject())) {
Expand All @@ -133,28 +136,26 @@ function it_loads_cookie_when_cookie_is_secure(CookieJar $cookieJar, RequestInte

function it_saves_cookie(CookieJar $cookieJar, RequestInterface $request, ResponseInterface $response, UriInterface $uri)
{
$this->beConstructedWith($cookieJar);
$cookieJar->getCookies()->shouldBeCalled()->willReturn([]);
$cookieJar->getCookies()->willReturn([]);

$next = function () use ($response) {
return new FulfilledPromise($response->getWrappedObject());
};

$response->hasHeader('Set-Cookie')->shouldBeCalled()->willReturn(true);
$response->getHeader('Set-Cookie')->shouldBeCalled()->willReturn([
$response->hasHeader('Set-Cookie')->willReturn(true);
$response->getHeader('Set-Cookie')->willReturn([
'cookie=value',
]);

$cookie = new Cookie('cookie', 'value', 0, 'test.com');
$cookieJar->addCookie($cookie)->shouldBeCalled();

$request->getUri()->shouldBeCalled()->willReturn($uri);
$uri->getHost()->shouldBeCalled()->willReturn('test.com');
$uri->getPath()->shouldBeCalled()->willReturn('/');
$request->getUri()->willReturn($uri);
$uri->getHost()->willReturn('test.com');
$uri->getPath()->willReturn('/');

$promise = $this->handleRequest($request, $next, function () {});
$promise->shouldReturnAnInstanceOf('Http\Client\Promise');
$response = $promise->getResponse();
$response->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
$promise->shouldHaveType('Http\Client\Promise');
$promise->getResponse()->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
}
}
17 changes: 11 additions & 6 deletions spec/ErrorPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@
namespace spec\Http\Client\Plugin;

use Http\Client\Utils\Promise\FulfilledPromise;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ErrorPluginSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->beAnInstanceOf('Http\Client\Plugin\ErrorPlugin');
}

function it_is_a_plugin()
{
$this->shouldImplement('Http\Client\Plugin\Plugin');
}

function it_throw_request_exception_on_500_error(RequestInterface $request, ResponseInterface $response)
{
$response->getStatusCode()->shouldBeCalled()->willReturn('500');
$response->getStatusCode()->willReturn('500');

$next = function (RequestInterface $receivedRequest) use($request, $response) {
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
Expand All @@ -31,7 +35,7 @@ function it_throw_request_exception_on_500_error(RequestInterface $request, Resp

function it_returns_response(RequestInterface $request, ResponseInterface $response)
{
$response->getStatusCode()->shouldBeCalled()->willReturn('200');
$response->getStatusCode()->willReturn('200');

$next = function (RequestInterface $receivedRequest) use($request, $response) {
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
Expand All @@ -42,10 +46,11 @@ function it_returns_response(RequestInterface $request, ResponseInterface $respo
$this->handleRequest($request, $next, function () {})->shouldReturnAnInstanceOf('Http\Client\Utils\Promise\FulfilledPromise');
}

function it_throw_request_exception_on_custom_regex(RequestInterface $request, ResponseInterface $response)
function it_throws_request_exception_on_custom_regex(RequestInterface $request, ResponseInterface $response)
{
$this->beConstructedWith('302');
$response->getStatusCode()->shouldBeCalled()->willReturn('302');

$response->getStatusCode()->willReturn('302');
$next = function (RequestInterface $receivedRequest) use($request, $response) {
if (Argument::is($request->getWrappedObject())->scoreArgument($receivedRequest)) {
return new FulfilledPromise($response->getWrappedObject());
Expand Down
28 changes: 20 additions & 8 deletions spec/PluginClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,44 @@
use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Client\Promise;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;

class PluginClientSpec extends ObjectBehavior
{
function it_is_initializable(HttpClient $client)
function let(HttpClient $client)
{
$this->beConstructedWith($client);
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Plugin\PluginClient');
}

function it_is_an_http_client()
{
$this->beAnInstanceOf('Http\Client\Plugin\PluginClient', [$client]);
$this->shouldImplement('Http\Client\HttpClient');
}

function it_is_an_http_async_client()
{
$this->shouldImplement('Http\Client\HttpAsyncClient');
}

function it_sends_request_with_underlying_client(HttpClient $client, RequestInterface $request, ResponseInterface $response)
{
$client->sendRequest($request)->shouldBeCalled()->willReturn($response);
$client->sendRequest($request)->willReturn($response);

$this->beConstructedWith($client);
$this->sendRequest($request)->shouldReturnAnInstanceOf('Psr\Http\Message\ResponseInterface');
}

function it_sends_async_request_with_underlying_client(HttpAsyncClient $client, RequestInterface $request, Promise $promise)
function it_sends_async_request_with_underlying_client(HttpAsyncClient $asyncClient, RequestInterface $request, Promise $promise)
{
$client->sendAsyncRequest($request)->shouldBeCalled()->willReturn($promise);
$asyncClient->sendAsyncRequest($request)->willReturn($promise);

$this->beConstructedWith($client);
$this->beConstructedWith($asyncClient);
$this->sendAsyncRequest($request)->shouldReturn($promise);
}
}
Loading