Skip to content

Commit 7033dc8

Browse files
committed
Merge pull request #42 from Nyholm/base
AddHostPlugin
2 parents 70eb5f2 + b14e8d8 commit 7033dc8

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

spec/AddHostPluginSpec.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Plugin;
4+
5+
use Http\Message\StreamFactory;
6+
use Http\Message\UriFactory;
7+
use Http\Promise\FulfilledPromise;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
use Psr\Http\Message\StreamInterface;
11+
use Psr\Http\Message\UriInterface;
12+
use PhpSpec\ObjectBehavior;
13+
use Prophecy\Argument;
14+
15+
class AddHostPluginSpec extends ObjectBehavior
16+
{
17+
function let(UriInterface $uri)
18+
{
19+
$this->beConstructedWith($uri);
20+
}
21+
22+
function it_is_initializable(UriInterface $uri)
23+
{
24+
$uri->getHost()->shouldBeCalled()->willReturn('example.com');
25+
$this->beConstructedWith($uri);
26+
$this->shouldHaveType('Http\Client\Plugin\AddHostPlugin');
27+
}
28+
29+
function it_is_a_plugin(UriInterface $uri)
30+
{
31+
$uri->getHost()->shouldBeCalled()->willReturn('example.com');
32+
$this->beConstructedWith($uri);
33+
34+
$this->shouldImplement('Http\Client\Plugin\Plugin');
35+
}
36+
37+
function it_adds_domain(
38+
RequestInterface $request,
39+
UriInterface $host,
40+
UriInterface $uri
41+
) {
42+
$host->getScheme()->shouldBeCalled()->willReturn('http://');
43+
$host->getHost()->shouldBeCalled()->willReturn('example.com');
44+
45+
$request->getUri()->shouldBeCalled()->willReturn($uri);
46+
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
47+
48+
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
49+
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
50+
$uri->getHost()->shouldBeCalled()->willReturn('');
51+
52+
$this->beConstructedWith($host);
53+
$this->handleRequest($request, function () {}, function () {});
54+
}
55+
56+
function it_replaces_domain(
57+
RequestInterface $request,
58+
UriInterface $host,
59+
UriInterface $uri
60+
) {
61+
$host->getScheme()->shouldBeCalled()->willReturn('http://');
62+
$host->getHost()->shouldBeCalled()->willReturn('example.com');
63+
64+
$request->getUri()->shouldBeCalled()->willReturn($uri);
65+
$request->withUri($uri)->shouldBeCalled()->willReturn($request);
66+
67+
$uri->withScheme('http://')->shouldBeCalled()->willReturn($uri);
68+
$uri->withHost('example.com')->shouldBeCalled()->willReturn($uri);
69+
70+
71+
$this->beConstructedWith($host, ['replace'=>true]);
72+
$this->handleRequest($request, function () {}, function () {});
73+
}
74+
75+
function it_does_nothing_when_domain_exists(
76+
RequestInterface $request,
77+
UriInterface $host,
78+
UriInterface $uri
79+
) {
80+
$request->getUri()->shouldBeCalled()->willReturn($uri);
81+
$uri->getHost()->shouldBeCalled()->willReturn('default.com');
82+
83+
$this->beConstructedWith($host);
84+
$this->handleRequest($request, function () {}, function () {});
85+
}
86+
}

src/AddHostPlugin.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Http\Client\Plugin;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\UriInterface;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
8+
9+
/**
10+
* Add schema and host to a request. Can be set to overwrite the schema and host if desired.
11+
*
12+
* @author Tobias Nyholm <[email protected]>
13+
*/
14+
class AddHostPlugin implements Plugin
15+
{
16+
/**
17+
* @var UriInterface
18+
*/
19+
private $host;
20+
21+
/**
22+
* @var bool
23+
*/
24+
private $replace;
25+
26+
/**
27+
* @param UriInterface $host
28+
* @param array $config {
29+
*
30+
* @var bool $replace True will replace all hosts, false will only add host when none is specified.
31+
* }
32+
*/
33+
public function __construct(UriInterface $host, array $config = [])
34+
{
35+
if ($host->getHost() === '') {
36+
throw new \LogicException('Host can not be empty');
37+
}
38+
39+
$this->host = $host;
40+
41+
$resolver = new OptionsResolver();
42+
$this->configureOptions($resolver);
43+
$options = $resolver->resolve($config);
44+
45+
$this->replace = $options['replace'];
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
52+
{
53+
if ($this->replace || $request->getUri()->getHost() === '') {
54+
$uri = $request->getUri()->withHost($this->host->getHost());
55+
$uri = $uri->withScheme($this->host->getScheme());
56+
57+
$request = $request->withUri($uri);
58+
}
59+
60+
return $next($request);
61+
}
62+
63+
/**
64+
* @param OptionsResolver $resolver
65+
*/
66+
private function configureOptions(OptionsResolver $resolver)
67+
{
68+
$resolver->setDefaults([
69+
'replace' => false,
70+
]);
71+
$resolver->setAllowedTypes('replace', 'bool');
72+
}
73+
}

0 commit comments

Comments
 (0)