Skip to content

[WIP] Move the plugin client to client-common (deprecate the old one) #64

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"php": ">=5.4",
"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/client-common": "^1.0",
"php-http/client-common": "dev-plugin_client",
"php-http/message": "^1.0",
"symfony/options-resolver": "^2.6|^3.0"
},
Expand Down
3 changes: 1 addition & 2 deletions spec/AddHostPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ function let(UriInterface $uri)
function it_is_initializable(UriInterface $uri)
{
$uri->getHost()->shouldBeCalled()->willReturn('example.com');
$this->beConstructedWith($uri);

$this->shouldHaveType('Http\Client\Plugin\AddHostPlugin');
}

function it_is_a_plugin(UriInterface $uri)
{
$uri->getHost()->shouldBeCalled()->willReturn('example.com');
$this->beConstructedWith($uri);

$this->shouldImplement('Http\Client\Plugin\Plugin');
}
Expand Down
8 changes: 6 additions & 2 deletions spec/ContentLengthPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@
namespace spec\Http\Client\Plugin;

use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class ContentLengthPluginSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Plugin\ContentLengthPlugin');
}

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

Expand Down
4 changes: 2 additions & 2 deletions spec/HeaderAppendPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace spec\Http\Client\Plugin;

use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class HeaderAppendPluginSpec extends ObjectBehavior
{
Expand Down
4 changes: 2 additions & 2 deletions spec/HeaderDefaultsPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace spec\Http\Client\Plugin;

use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class HeaderDefaultsPluginSpec extends ObjectBehavior
{
Expand Down
4 changes: 2 additions & 2 deletions spec/HeaderRemovePluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace spec\Http\Client\Plugin;

use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class HeaderRemovePluginSpec extends ObjectBehavior
{
Expand Down
4 changes: 2 additions & 2 deletions spec/HeaderSetPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
namespace spec\Http\Client\Plugin;

use PhpSpec\Exception\Example\SkippingException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\StreamInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class HeaderSetPluginSpec extends ObjectBehavior
{
Expand Down
14 changes: 0 additions & 14 deletions spec/LoopPlugin.php

This file was deleted.

48 changes: 13 additions & 35 deletions spec/PluginClientSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace spec\Http\Client\Plugin;

use Http\Client\HttpAsyncClient;
use Http\Client\HttpClient;
use Http\Client\Plugin\Plugin;
use Http\Promise\Promise;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class PluginClientSpec extends ObjectBehavior
{
Expand All @@ -32,40 +30,20 @@ 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)
function it_throws_loop_exception(HttpClient $client, RequestInterface $request, Plugin $plugin)
{
$client->sendRequest($request)->willReturn($response);
$plugin
->handleRequest(
$request,
Argument::type('callable'),
Argument::type('callable')
)
->will(function ($args) {
return $args[2]($args[0]);
})
;

$this->sendRequest($request)->shouldReturn($response);
}

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

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

function it_sends_async_request_if_no_send_request(HttpAsyncClient $asyncClient, RequestInterface $request, ResponseInterface $response, Promise $promise)
{
$this->beConstructedWith($asyncClient->getWrappedObject());
$asyncClient->sendAsyncRequest($request)->willReturn($promise);
$promise->wait()->willReturn($response);

$this->sendRequest($request)->shouldReturn($response);
}

function it_prefers_send_request(StubClient $client, RequestInterface $request, ResponseInterface $response)
{
$client->sendRequest($request)->willReturn($response);

$this->sendRequest($request)->shouldReturn($response);
}

function it_throws_loop_exception(HttpClient $client, RequestInterface $request)
{
$this->beConstructedWith($client, [new LoopPlugin()]);
$this->beConstructedWith($client, [$plugin]);

$this->shouldThrow('Http\Client\Plugin\Exception\LoopException')->duringSendRequest($request);
}
Expand Down
18 changes: 0 additions & 18 deletions spec/StubClient.php

This file was deleted.

67 changes: 3 additions & 64 deletions src/AddHostPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,72 +2,11 @@

namespace Http\Client\Plugin;

use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Add schema and host to a request. Can be set to overwrite the schema and host if desired.
*
* @author Tobias Nyholm <[email protected]>
*
* @deprecated since version 1.1, to be removed in 2.0. Use {@link \Http\Client\Common\Plugin\AddHostPlugin} instead.
*/
class AddHostPlugin implements Plugin
class AddHostPlugin extends \Http\Client\Common\Plugin\AddHostPlugin implements Plugin
{
/**
* @var UriInterface
*/
private $host;

/**
* @var bool
*/
private $replace;

/**
* @param UriInterface $host
* @param array $config {
*
* @var bool $replace True will replace all hosts, false will only add host when none is specified.
* }
*/
public function __construct(UriInterface $host, array $config = [])
{
if ($host->getHost() === '') {
throw new \LogicException('Host can not be empty');
}

$this->host = $host;

$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$options = $resolver->resolve($config);

$this->replace = $options['replace'];
}

/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
if ($this->replace || $request->getUri()->getHost() === '') {
$uri = $request->getUri()->withHost($this->host->getHost());
$uri = $uri->withScheme($this->host->getScheme());

$request = $request->withUri($uri);
}

return $next($request);
}

/**
* @param OptionsResolver $resolver
*/
private function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'replace' => false,
]);
$resolver->setAllowedTypes('replace', 'bool');
}
}
31 changes: 3 additions & 28 deletions src/AuthenticationPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,11 @@

namespace Http\Client\Plugin;

use Http\Message\Authentication;
use Psr\Http\Message\RequestInterface;

/**
* Send an authenticated request.
*
* @author Joel Wurtz <[email protected]>
*
* @deprecated since version 1.1, to be removed in 2.0. Use {@link \Http\Client\Common\Plugin\AuthenticationPlugin} instead.
*/
class AuthenticationPlugin implements Plugin
class AuthenticationPlugin extends \Http\Client\Common\Plugin\AuthenticationPlugin implements Plugin
{
/**
* @var Authentication An authentication system
*/
private $authentication;

/**
* @param Authentication $authentication
*/
public function __construct(Authentication $authentication)
{
$this->authentication = $authentication;
}

/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$request = $this->authentication->authenticate($request);

return $next($request);
}
}
29 changes: 3 additions & 26 deletions src/ContentLengthPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,11 @@

namespace Http\Client\Plugin;

use Http\Message\Encoding\ChunkStream;
use Psr\Http\Message\RequestInterface;

/**
* Allow to set the correct content length header on the request or to transfer it as a chunk if not possible.
*
* @author Joel Wurtz <[email protected]>
*
* @deprecated since version 1.1, to be removed in 2.0. Use {@link \Http\Client\Common\Plugin\ContentLengthPlugin} instead.
*/
class ContentLengthPlugin implements Plugin
class ContentLengthPlugin extends \Http\Client\Common\Plugin\ContentLengthPlugin implements Plugin
{
/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
if (!$request->hasHeader('Content-Length')) {
$stream = $request->getBody();

// Cannot determine the size so we use a chunk stream
if (null === $stream->getSize()) {
$stream = new ChunkStream($stream);
$request = $request->withBody($stream);
$request = $request->withAddedHeader('Transfer-Encoding', 'chunked');
} else {
$request = $request->withHeader('Content-Length', $stream->getSize());
}
}

return $next($request);
}
}
Loading