Skip to content

Commit f4e245d

Browse files
committed
Added AddPathPlugin
1 parent 999cb9c commit f4e245d

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- Fix Emulated Trait to use Http based promise which respect the HttpAsyncClient interface
99
- Require Httplug 1.1 where we use HTTP specific promises.
1010
- RedirectPlugin: use the full URL instead of the URI to properly keep track of redirects
11-
11+
- Add AddPathPlugin for API URLs with base path
1212

1313
## 1.2.1 - 2016-07-26
1414

spec/Plugin/AddPathPluginSpec.php

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common\Plugin;
4+
5+
use Http\Message\StreamFactory;
6+
use Http\Message\UriFactory;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\UriInterface;
9+
use PhpSpec\ObjectBehavior;
10+
11+
class AddPathPluginSpec extends ObjectBehavior
12+
{
13+
function let(UriInterface $uri)
14+
{
15+
$this->beConstructedWith($uri);
16+
}
17+
18+
function it_is_initializable(UriInterface $uri)
19+
{
20+
$uri->getPath()->shouldBeCalled()->willReturn('/api');
21+
22+
$this->shouldHaveType('Http\Client\Common\Plugin\AddPathPlugin');
23+
}
24+
25+
function it_is_a_plugin(UriInterface $uri)
26+
{
27+
$uri->getPath()->shouldBeCalled()->willReturn('/api');
28+
29+
$this->shouldImplement('Http\Client\Common\Plugin');
30+
}
31+
32+
function it_adds_path(
33+
RequestInterface $request,
34+
UriInterface $host,
35+
UriInterface $uri
36+
) {
37+
$host->getPath()->shouldBeCalled()->willReturn('/api');
38+
39+
$request->getUri()->shouldBeCalled()->willReturn($uri);
40+
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
41+
42+
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
43+
$uri->getPath()->shouldBeCalled()->willReturn('/users');
44+
45+
$this->beConstructedWith($host);
46+
$this->handleRequest($request, function () {}, function () {});
47+
}
48+
49+
function it_throws_exception_on_trailing_slash(UriInterface $host)
50+
{
51+
$host->getPath()->shouldBeCalled()->willReturn('/api/');
52+
53+
$this->beConstructedWith($host);
54+
$this->shouldThrow('\LogicException')->duringInstantiation();
55+
}
56+
57+
function it_throws_exception_on_empty_path(UriInterface $host)
58+
{
59+
$host->getPath()->shouldBeCalled()->willReturn('');
60+
61+
$this->beConstructedWith($host);
62+
$this->shouldThrow('\LogicException')->duringInstantiation();
63+
}
64+
}

src/Plugin/AddPathPlugin.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace Http\Client\Common\Plugin;
4+
5+
use Http\Client\Common\Plugin;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\UriInterface;
8+
9+
/**
10+
* Prepend a base path to the request URI. Useful for base API URLs like http://domain.com/api.
11+
*
12+
* @author Sullivan Senechal <[email protected]>
13+
*/
14+
final class AddPathPlugin implements Plugin
15+
{
16+
/**
17+
* @var UriInterface
18+
*/
19+
private $uri;
20+
21+
/**
22+
* @param UriInterface $uri
23+
*/
24+
public function __construct(UriInterface $uri)
25+
{
26+
if ($uri->getPath() === '') {
27+
throw new \LogicException('URI path cannot be empty');
28+
}
29+
30+
if (substr($uri->getPath(), -1) === '/') {
31+
throw new \LogicException('URI path cannot end with a slash.');
32+
}
33+
34+
$this->uri = $uri;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
41+
{
42+
$request = $request->withUri($request->getUri()
43+
->withPath($this->uri->getPath().$request->getUri()->getPath())
44+
);
45+
46+
return $next($request);
47+
}
48+
}

0 commit comments

Comments
 (0)