Skip to content

Commit c9c070b

Browse files
committed
Added AddPathPlugin
1 parent 999cb9c commit c9c070b

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

src/Plugin/AddPathPlugin.php

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

0 commit comments

Comments
 (0)