-
Notifications
You must be signed in to change notification settings - Fork 53
Add AddPathPlugin and BaseUriPlugin #48
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common\Plugin; | ||
|
||
use Http\Message\StreamFactory; | ||
use Http\Message\UriFactory; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class AddPathPluginSpec extends ObjectBehavior | ||
{ | ||
function let(UriInterface $uri) | ||
{ | ||
$this->beConstructedWith($uri); | ||
} | ||
|
||
function it_is_initializable(UriInterface $uri) | ||
{ | ||
$uri->getPath()->shouldBeCalled()->willReturn('/api'); | ||
|
||
$this->shouldHaveType('Http\Client\Common\Plugin\AddPathPlugin'); | ||
} | ||
|
||
function it_is_a_plugin(UriInterface $uri) | ||
{ | ||
$uri->getPath()->shouldBeCalled()->willReturn('/api'); | ||
|
||
$this->shouldImplement('Http\Client\Common\Plugin'); | ||
} | ||
|
||
function it_adds_path( | ||
RequestInterface $request, | ||
UriInterface $host, | ||
UriInterface $uri | ||
) { | ||
$host->getPath()->shouldBeCalled()->willReturn('/api'); | ||
|
||
$request->getUri()->shouldBeCalled()->willReturn($uri); | ||
$request->withUri($uri)->shouldBeCalled()->willReturn($request); | ||
|
||
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri); | ||
$uri->getPath()->shouldBeCalled()->willReturn('/users'); | ||
|
||
$this->beConstructedWith($host); | ||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_throws_exception_on_trailing_slash(UriInterface $host) | ||
{ | ||
$host->getPath()->shouldBeCalled()->willReturn('/api/'); | ||
|
||
$this->beConstructedWith($host); | ||
$this->shouldThrow('\LogicException')->duringInstantiation(); | ||
} | ||
|
||
function it_throws_exception_on_empty_path(UriInterface $host) | ||
{ | ||
$host->getPath()->shouldBeCalled()->willReturn(''); | ||
|
||
$this->beConstructedWith($host); | ||
$this->shouldThrow('\LogicException')->duringInstantiation(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common\Plugin; | ||
|
||
use Http\Message\StreamFactory; | ||
use Http\Message\UriFactory; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\UriInterface; | ||
use PhpSpec\ObjectBehavior; | ||
|
||
class BaseUriPluginSpec extends ObjectBehavior | ||
{ | ||
function let(UriInterface $uri) | ||
{ | ||
$this->beConstructedWith($uri); | ||
} | ||
|
||
function it_is_initializable(UriInterface $uri) | ||
{ | ||
$uri->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here: can these be moved to let? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll look at this, I'm more familiar with PHPUnit than PHPSpec. 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried this: function let(UriInterface $uri)
{
$uri->getPath()->shouldBeCalled()->willReturn('/api');
$this->beConstructedWith($uri);
} But got this error:
I don't think this can be refactored on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, let will set default config, so you can set the default input in the |
||
$uri->getPath()->shouldBeCalled()->willReturn('/api'); | ||
|
||
$this->shouldHaveType('Http\Client\Common\Plugin\BaseUriPlugin'); | ||
} | ||
|
||
function it_is_a_plugin(UriInterface $uri) | ||
{ | ||
$uri->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
$uri->getPath()->shouldBeCalled()->willReturn('/api'); | ||
|
||
$this->shouldImplement('Http\Client\Common\Plugin'); | ||
} | ||
|
||
function it_adds_domain_and_path( | ||
RequestInterface $request, | ||
UriInterface $host, | ||
UriInterface $uri | ||
) { | ||
$host->getScheme()->shouldBeCalled()->willReturn('http://'); | ||
$host->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
$host->getPort()->shouldBeCalled()->willReturn(8000); | ||
$host->getPath()->shouldBeCalled()->willReturn('/api'); | ||
|
||
$request->getUri()->shouldBeCalled()->willReturn($uri); | ||
$request->withUri($uri)->shouldBeCalled()->willReturn($request); | ||
|
||
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri); | ||
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri); | ||
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri); | ||
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri); | ||
$uri->getHost()->shouldBeCalled()->willReturn(''); | ||
$uri->getPath()->shouldBeCalled()->willReturn('/users'); | ||
|
||
$this->beConstructedWith($host); | ||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_adds_domain( | ||
RequestInterface $request, | ||
UriInterface $host, | ||
UriInterface $uri | ||
) { | ||
$host->getScheme()->shouldBeCalled()->willReturn('http://'); | ||
$host->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
$host->getPort()->shouldBeCalled()->willReturn(8000); | ||
$host->getPath()->shouldBeCalled()->willReturn('/'); | ||
|
||
$request->getUri()->shouldBeCalled()->willReturn($uri); | ||
$request->withUri($uri)->shouldBeCalled()->willReturn($request); | ||
|
||
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri); | ||
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri); | ||
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri); | ||
$uri->getHost()->shouldBeCalled()->willReturn(''); | ||
|
||
$this->beConstructedWith($host); | ||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_replaces_domain_and_adds_path( | ||
RequestInterface $request, | ||
UriInterface $host, | ||
UriInterface $uri | ||
) { | ||
$host->getScheme()->shouldBeCalled()->willReturn('http://'); | ||
$host->getHost()->shouldBeCalled()->willReturn('example.com'); | ||
$host->getPort()->shouldBeCalled()->willReturn(8000); | ||
$host->getPath()->shouldBeCalled()->willReturn('/api'); | ||
|
||
$request->getUri()->shouldBeCalled()->willReturn($uri); | ||
$request->withUri($uri)->shouldBeCalled()->willReturn($request); | ||
|
||
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri); | ||
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri); | ||
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri); | ||
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri); | ||
$uri->getPath()->shouldBeCalled()->willReturn('/users'); | ||
|
||
$this->beConstructedWith($host, ['replace' => true]); | ||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add test when there is no path ? (with my previous comment it should have failed :) ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common\Plugin; | ||
|
||
use Http\Client\Common\Plugin; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
/** | ||
* Prepend a base path to the request URI. Useful for base API URLs like http://domain.com/api. | ||
* | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class AddPathPlugin implements Plugin | ||
{ | ||
/** | ||
* @var UriInterface | ||
*/ | ||
private $uri; | ||
|
||
/** | ||
* @param UriInterface $uri | ||
*/ | ||
public function __construct(UriInterface $uri) | ||
{ | ||
if ($uri->getPath() === '') { | ||
throw new \LogicException('URI path cannot be empty'); | ||
} | ||
|
||
if (substr($uri->getPath(), -1) === '/') { | ||
throw new \LogicException('URI path cannot end with a slash.'); | ||
} | ||
|
||
$this->uri = $uri; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
$request = $request->withUri($request->getUri() | ||
->withPath($this->uri->getPath().$request->getUri()->getPath()) | ||
); | ||
|
||
return $next($request); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common\Plugin; | ||
|
||
use Http\Client\Common\Plugin; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\UriInterface; | ||
|
||
/** | ||
* Combines the AddHostPlugin and AddPathPlugin. | ||
* | ||
* @author Sullivan Senechal <[email protected]> | ||
*/ | ||
final class BaseUriPlugin implements Plugin | ||
{ | ||
/** | ||
* @var AddHostPlugin | ||
*/ | ||
private $addHostPlugin; | ||
|
||
/** | ||
* @var AddPathPlugin|null | ||
*/ | ||
private $addPathPlugin = null; | ||
|
||
/** | ||
* @param UriInterface $uri Has to contain a host name and cans have a path. | ||
* @param array $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions | ||
*/ | ||
public function __construct(UriInterface $uri, array $hostConfig = []) | ||
{ | ||
$this->addHostPlugin = new AddHostPlugin($uri, $hostConfig); | ||
|
||
if (rtrim($uri->getPath(), '/')) { | ||
$this->addPathPlugin = new AddPathPlugin($uri); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
$addHostNext = function (RequestInterface $request) use ($next, $first) { | ||
return $this->addHostPlugin->handleRequest($request, $next, $first); | ||
}; | ||
|
||
if ($this->addPathPlugin) { | ||
return $this->addPathPlugin->handleRequest($request, $addHostNext, $first); | ||
} | ||
|
||
return $addHostNext($request); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be moved to the let method. That would simplify things.