Skip to content

Commit 9257eb4

Browse files
committed
Added AddPathPlugin
1 parent 999cb9c commit 9257eb4

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
* Add 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 $host;
20+
21+
/**
22+
* @param UriInterface $host
23+
*/
24+
public function __construct(UriInterface $host)
25+
{
26+
if ($host->getPath() === '') {
27+
throw new \LogicException('Host path can not be empty');
28+
}
29+
30+
$this->host = $host;
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
37+
{
38+
$request = $request->withUri($request->getUri()
39+
->withPath($this->host->getPath().$request->getUri()->getPath())
40+
);
41+
42+
return $next($request);
43+
}
44+
}

0 commit comments

Comments
 (0)