Skip to content

Commit 2c2e86d

Browse files
committed
Add BaseUriPlugin that combines AddHostPlugin and AddPathPlugin
1 parent f4e245d commit 2c2e86d

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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
1111
- Add AddPathPlugin for API URLs with base path
12+
- Add BaseUriPlugin that combines AddHostPlugin and AddPathPlugin
1213

1314
## 1.2.1 - 2016-07-26
1415

spec/Plugin/BaseUriPluginSpec.php

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 BaseUriPluginSpec 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->getHost()->shouldBeCalled()->willReturn('example.com');
21+
$uri->getPath()->shouldBeCalled()->willReturn('/api');
22+
23+
$this->shouldHaveType('Http\Client\Common\Plugin\BaseUriPlugin');
24+
}
25+
26+
function it_is_a_plugin(UriInterface $uri)
27+
{
28+
$uri->getHost()->shouldBeCalled()->willReturn('example.com');
29+
$uri->getPath()->shouldBeCalled()->willReturn('/api');
30+
31+
$this->shouldImplement('Http\Client\Common\Plugin');
32+
}
33+
34+
function it_adds_domain_and_path(
35+
RequestInterface $request,
36+
UriInterface $host,
37+
UriInterface $uri
38+
) {
39+
$host->getScheme()->shouldBeCalled()->willReturn('http://');
40+
$host->getHost()->shouldBeCalled()->willReturn('example.com');
41+
$host->getPort()->shouldBeCalled()->willReturn(8000);
42+
$host->getPath()->shouldBeCalled()->willReturn('/api');
43+
44+
$request->getUri()->shouldBeCalled()->willReturn($uri);
45+
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
46+
47+
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
48+
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
49+
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri);
50+
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
51+
$uri->getHost()->shouldBeCalled()->willReturn('');
52+
$uri->getPath()->shouldBeCalled()->willReturn('/users');
53+
54+
$this->beConstructedWith($host);
55+
$this->handleRequest($request, function () {}, function () {});
56+
}
57+
58+
function it_adds_domain(
59+
RequestInterface $request,
60+
UriInterface $host,
61+
UriInterface $uri
62+
) {
63+
$host->getScheme()->shouldBeCalled()->willReturn('http://');
64+
$host->getHost()->shouldBeCalled()->willReturn('example.com');
65+
$host->getPort()->shouldBeCalled()->willReturn(8000);
66+
$host->getPath()->shouldBeCalled()->willReturn('/');
67+
68+
$request->getUri()->shouldBeCalled()->willReturn($uri);
69+
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
70+
71+
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
72+
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
73+
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri);
74+
$uri->getHost()->shouldBeCalled()->willReturn('');
75+
76+
$this->beConstructedWith($host);
77+
$this->handleRequest($request, function () {}, function () {});
78+
}
79+
80+
function it_replaces_domain_and_adds_path(
81+
RequestInterface $request,
82+
UriInterface $host,
83+
UriInterface $uri
84+
) {
85+
$host->getScheme()->shouldBeCalled()->willReturn('http://');
86+
$host->getHost()->shouldBeCalled()->willReturn('example.com');
87+
$host->getPort()->shouldBeCalled()->willReturn(8000);
88+
$host->getPath()->shouldBeCalled()->willReturn('/api');
89+
90+
$request->getUri()->shouldBeCalled()->willReturn($uri);
91+
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
92+
93+
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
94+
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
95+
$uri->withPort(8000)->shouldBeCalled()->willReturn($uri);
96+
$uri->withPath('/api/users')->shouldBeCalled()->willReturn($uri);
97+
$uri->getPath()->shouldBeCalled()->willReturn('/users');
98+
99+
$this->beConstructedWith($host, ['replace' => true]);
100+
$this->handleRequest($request, function () {}, function () {});
101+
}
102+
}

src/Plugin/BaseUriPlugin.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
* Combines the AddHostPlugin and AddPathPlugin.
11+
*
12+
* @author Sullivan Senechal <[email protected]>
13+
*/
14+
final class BaseUriPlugin implements Plugin
15+
{
16+
/**
17+
* @var AddHostPlugin
18+
*/
19+
private $addHostPlugin;
20+
21+
/**
22+
* @var AddPathPlugin|null
23+
*/
24+
private $addPathPlugin = null;
25+
26+
/**
27+
* @param UriInterface $uri Has to contain a host name and cans have a path.
28+
* @param array $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions
29+
*/
30+
public function __construct(UriInterface $uri, array $hostConfig = [])
31+
{
32+
$this->addHostPlugin = new AddHostPlugin($uri, $hostConfig);
33+
34+
if (rtrim($uri->getPath(), '/')) {
35+
$this->addPathPlugin = new AddPathPlugin($uri);
36+
}
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
43+
{
44+
$addHostNext = function (RequestInterface $request) use ($next, $first) {
45+
return $this->addHostPlugin->handleRequest($request, $next, $first);
46+
};
47+
48+
if ($this->addPathPlugin) {
49+
return $this->addPathPlugin->handleRequest($request, $addHostNext, $first);
50+
}
51+
52+
return $addHostNext($request);
53+
}
54+
}

0 commit comments

Comments
 (0)